Topic Links:
Http://www.codeforces.com/contest/655/problem/D
Test instructions
The problem is that the first K-sessions are required to determine the unique topological order, and the minimum k to satisfy the condition is obtained.
Exercises
The value of the binary K, do the topological sort as long as only one element at a time without a precursor can be unique.
#include <iostream>#include<cstring>#include<cstdio>#include<vector>#include<queue>#include<utility>using namespacestd;Const intMAXN =101010;intN, M;structEdge {intV, NE; Edge (intVintne): V (v), NE (NE) {} Edge () {}}EGS[MAXN*2];intHEAD[MAXN], tot;voidAddedge (intUintv) {Egs[tot]=Edge (V, Head[u]); Head[u]= tot++;}intIND[MAXN];BOOLOkintm) {memset (Ind,0,sizeof(Ind)); for(inti =0; I <= m; i++) {IND[EGS[I].V]++; } Queue<int>Q; for(inti =0; I < n; i++) { if(Ind[i] = =0) {Q.push (i); } } while(!Q.empty ()) { if(Q.size () >1)return false; intU =Q.front (); Q.pop (); intp =Head[u]; while(P! =-1) {Edge& e =Egs[p]; if(P <=m) {IND[E.V]--; if(IND[E.V] = =0) {Q.push (E.V); }} P=e.ne; } } return true;}voidinit () {memset (head,-1,sizeof(head)); Tot=0;}intMain () { while(SCANF ("%d%d", &n, &m) = =2&&N) {init (); for(inti =0; I < m; i++) { intu, v; scanf ("%d%d", &u, &v); U--, v--; Addedge (U, v); } intL =-1, R = tot-1; while(L +1<r) {intMID = L + (r-l)/2; if(!ok (mid)) L =mid; ElseR =mid; } //printf ("l:%d\n", l); if(!ok (R)) printf ("-1\n"); Elseprintf"%d\n", R +1); } return 0;}
Croc 2016-elimination Round (rated unofficial Edition) d. Robot rapping Results report topology sort + dichotomy