Hdoj 5195 Dzy Loves topological sorting "topology" + "line segment tree"

Source: Internet
Author: User

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 it vertices such that for every Di Rected Edge (u→v) from vertex U-vertex v, U comes before v in the ordering.
Now, the 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 a 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).

Test instructions: give you n-side, delete not more than the K-bar, so that the output of the dictionary order the largest!!
Strategy: We always look for a larger number than the current K output is good,
Need to understand: 1, each minus one entry is subtracted from an edge.
2: After finding a point, be sure to change the entry of the corresponding point to the maximum value, in case there is a possibility of being found later.
Code:

#include <cstdio>#include <cstring>#include <vector>#include <iostream>#include <algorithm>Const intM =1e5+5;Const intINF =0x3f3f3f3f;using namespace STD;intc[m<<2], in[m]; vector<int >M[M]; vector<int >AnsintN, MM, K;voidUpdateintPintXintLintRintPOS) {if(L = = r) {C[pos] = x;return; }intMid = (l+r) >>1;if(P <= mid) Update (p, X, L, Mid, pos<<1);//left and right are the corresponding points of the representation.     ElseUpdate (P, X, mid+1, R, pos<<1|1); C[pos] = min (c[pos<<1], c[pos<<1|1]);}intQueryintLintRintPOS) {if(L = = r)returnLintMid = (l+r) >>1;if(c[pos<<1|1] <= k)returnQuery (mid+1, R, pos<<1|1);//Always try to choose the bigger point    returnQuery (L, mid,pos<<1);}voidTopo () { for(inti =0; I < n; + + i) {inttemp = Query (1N1); K-= in[temp];//means to remove several pointsAns.push_back (temp); Update (temp, INF,1N1);//To update when foundIn[temp] = INF;//must become positive infinity         for(inti =0; I < m[temp].size (); + + i) {intv = m[temp][i];             --IN[V]; Update (V, In[v],1N1); }    }}intMain () { while(scanf("%d%d%d", &n, &mm, &k) = =3){ for(inti =0; I <= N; + + i) {m[i].clear (); In[i] =0; }intU, v; for(inti =0; I < mm; + + i) {scanf("%d%d", &u, &v);            ++IN[V];        M[u].push_back (v); } for(inti =1; I <= N; + + i) {update (I, In[i],1N1);        } ans.clear (); Topo ();printf("%d", ans[0]); for(inti =1; I < n; + + i)printf("%d", Ans[i]);printf("\ n"); }return 0;}

Hdoj 5195 Dzy Loves topological sorting "topology" + "line segment tree"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.