Croc 2016-elimination Round (rated unofficial Edition) d. Robot rapping Results report topology sort + dichotomy

Source: Internet
Author: User

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

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.