多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > Hdoj 5195 DZY Loves Topological Sorting 【拓撲】+【線段樹】

Hdoj 5195 DZY Loves Topological Sorting 【拓撲】+【線段樹】

來源:程序員人生   發布時間:2015-06-05 09:26:13 閱讀次數:2579次

DZY Loves Topological Sorting

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 922 Accepted Submission(s): 269

Problem Description
A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge (u→v) from vertex u to vertex v, u comes before v in the ordering.
Now, DZY has a directed acyclic graph(DAG). You should find the lexicographically largest topological ordering after erasing at most k edges from the graph.

Input
The input consists several test cases. (TestCase≤5)
The first line, three integers n,m,k(1≤n,m≤105,0≤k≤m).
Each of the next m lines has two integers: u,v(u≠v,1≤u,v≤n), representing a direct edge(u→v).

Output
For each test case, output the lexicographically largest topological ordering.

Sample Input
5 5 2
1 2
4 5
2 4
3 4
2 3
3 2 0
1 2
1 3

Sample Output
5 3 1 2 4
1 3 2

Hint
Case 1.
Erase the edge (2->3),(4->5).
And the lexicographically largest topological ordering is (5,3,1,2,4).

題意:給你n條邊,刪去不多于K條邊,使輸出的字典序最大!!
策略:我們每次都找小于等于當前K的較大的數輸出就行了,
需明白:1,每減去1個入度都是減去1條邊。
2:找到1個點以后,1定要將對應點的入度變成最大值,以防后面還有可能被找到。
代碼:

#include <cstdio> #include <cstring> #include <vector> #include <iostream> #include <algorithm> const int M = 1e5+5; const int INF = 0x3f3f3f3f; using namespace std; int c[M<<2], in[M]; vector<int > m[M]; vector<int > ans; int n, mm, k; void update(int p, int x, int l, int r, int pos){ if(l == r){ c[pos] = x; return ; } int mid = (l+r)>>1; if(p <= mid) update(p, x, l, mid, pos<<1); //left和right都是代表的對應的點。 else update(p, x, mid+1, r, pos<<1|1); c[pos] = min(c[pos<<1], c[pos<<1|1]); } int query(int l, int r, int pos){ if(l == r) return l; int mid = (l+r)>>1; if(c[pos<<1|1] <= k) return query(mid+1, r, pos<<1|1);//每次都是盡可能選比較大的點 return query(l, mid,pos<<1); } void topo(){ for(int i = 0; i < n; ++ i){ int temp = query(1, n, 1); k -= in[temp];//表示去掉幾個點 ans.push_back(temp); update(temp, INF, 1, n, 1); //找到后就要更新 in[temp] = INF; //1定要變成正無窮 for(int i = 0; i < m[temp].size(); ++ i){ int v = m[temp][i]; --in[v]; update(v, in[v], 1, n, 1); } } } int main(){ while(scanf("%d%d%d", &n, &mm, &k) == 3){ for(int i = 0; i <= n; ++ i){ m[i].clear(); in[i] = 0; } int u, v; for(int i = 0; i < mm; ++ i){ scanf("%d%d", &u, &v); ++in[v]; m[u].push_back(v); } for(int i = 1; i <= n; ++ i){ update(i, in[i], 1, n, 1); } ans.clear(); topo(); printf("%d", ans[0]); for(int i = 1; i < n; ++ i) printf(" %d", ans[i]); printf(" "); } return 0; }
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 鸥美性 | 亚洲国产精品影院 | 国产精品三区四区 | 国产成人吃奶一区 | 自拍视频国产 | 午夜欧美成人久久久久久 | 日本a一级毛片免费观看 | 亚洲欧美精品一中文字幕 | 欧美日本一道高清免费3区 欧美日本一道免费一区三区 | 亚洲欧美精品 | 伊人99| 日韩视频一区二区三区 | 欧美freesex呦交hd | 国产精品久久成人影院 | 高清一区二区 | 亚洲欧美一区二区三区久久 | 日本中文字幕在线播放 | 中文字幕亚洲无线码高清 | 一本大道高清香蕉中文大在线 | 日本午夜视频在线观看 | 久久久精品成人免费看 | 最新日韩欧美不卡一二三区 | 久久综合久久久久 | 日韩中文字幕精品一区在线 | 国产一区二区视频在线观看 | 免费播放观看在线视频 | 一级看片免费视频 | 免费区欧美一级毛片精品 | 男女免费在线视频 | 99久久中文字幕伊人 | 亚洲精品不卡视频 | 国产精品第1页在线观看 | 一二三四视频观看中文在线看 | 国产成人一区二区 | 在线看国产视频 | 久久综合九色综合欧美就去吻 | 亚洲综合在线观看视频 | 欧美一级毛片高清免费观看 | 欧美成a人片在线观看 | 亚洲三级黄色 | 成年人的天堂 |