"cf883b" Berland Army topological ordering

Source: Internet
Author: User

"Cf883b" Berland Army

Test Instructions : given n points, M-Bar has a forward edge, some points of the right to know, the rest of the unknown, the point right in the 1-k. First of all, I want you to determine the point of all points to meet:

Point weights for all edge a->b,a points >b
For I=1..K, at least one point has a point right of I

n,m,k<=100000

Solution: Like the food production as strange topological sorting problem, directly on the method, will not be proven.

The lower bound Li and the upper bound ri of each point point are obtained by the topological sort on both sides of the front and reverse runs.

Sort all points from small to large, and then enumerate the weights I. Throw all the points on the upper bound to the heap, then remove the largest point of the nether from the heap and assign the weights to I. Then find all the points in the nether as I, and assign their weights to I.

#include <cstdio> #include <cstring> #include <utility> #include <queue> #include <vector > #define MP (A, b) Make_pair (A, B) using namespace Std;const int maxn=200010;typedef pair<int,int> pii;int n,m,k, Cnt,flag;int To[maxn],nxt[maxn],head[maxn],pa[maxn],pb[maxn],v[maxn],l[maxn],r[maxn],d[maxn],ans[maxn];vector <int> p[maxn];vector<int>::iterator it;queue<int> q;priority_queue<pii> pq;inline Int Rd () { int Ret=0,f=1;char Gc=getchar (); while (gc< ' 0 ' | | Gc> ' 9 ') {if (gc== '-') F=-f;gc=getchar ();} while (gc>= ' 0 ' &&gc<= ' 9 ') ret=ret*10+ (gc^ ' 0 '), Gc=getchar (); return ret*f;} inline void Add (int a,int b) {to[cnt]=b,nxt[cnt]=head[a],head[a]=cnt++;} int main () {n=rd (), M=rd (), K=RD (), int i,u;for (i=1;i<=n;i++) {v[i]=rd (); if (!v[i]) l[i]=1,r[i]=k;elsel[i]=r[i]=v[i] ;} memset (head,-1,sizeof (head)), Cnt=0;for (i=1;i<=m;i++) pa[i]=rd (), Pb[i]=rd (), D[pb[i]]++,add (Pa[i],pb[i]); for (i =1;i<=n;i++) if (!d[i]) Q.push (i), while (!q.empty ()) {U=q.front (), Q.pop (); for (i=head[u];i!=-1;i=nxt[i]) {D[to[i]]--, r[to[i]]=min (r[to[i]],r[u]-1), if (!d[to[i]]) Q.push (To[i]);}} for (i=1;i<=n;i++) if (D[i]) return puts ("1"), 0;memset (Head,-1,sizeof (head)), Cnt=0;for (i=1;i<=m;i++) D[pa[i]] ++,add (Pb[i],pa[i]); for (i=1;i<=n;i++) if (!d[i]) Q.push (i); while (!q.empty ()) {U=q.front (), Q.pop (); for (I=head[u] ; I!=-1;i=nxt[i]) {D[to[i]]--, L[to[i]]=max (l[to[i]],l[u]+1), if (!d[to[i]]) Q.push (To[i]);}} for (i=1;i<=n;i++) if (d[i]| | L[i]>r[i]) return puts ("1"), 0;for (i=1;i<=n;i++) p[r[i]].push_back (i); for (i=k;i>=1;i--) {for (It=p[i]. Begin (); It!=p[i].end (); it++) Pq.push (MP (L[*it],*it)), if (Pq.empty ()) return puts ("1"), 0;u=pq.top (). Second,pq.pop ( ), Ans[u]=i;while (!pq.empty ()) {u=pq.top (). Second;if (L[u]<i) Break;pq.pop (), Ans[u]=i;}} for (i=1;i<=n;i++) printf ("%d", ans[i]); return 0;}

"cf883b" Berland Army topological ordering

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.