Two points, topological sort.
Two-point answer, and then the topological sorting check, if a discovery exists two or more than two in the $0$ node, then it is not OK.
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<iostream>using namespaceStd;typedefLong LongLL;Const DoublePi=acos (-1.0), eps=1e-6;voidFile () {freopen ("D:\\in.txt","R", stdin); Freopen ("D:\\out.txt","W", stdout);} Template<classT>inlinevoidRead (T &x) { CharC=getchar (); x=0; while(!isdigit (c)) c=GetChar (); while(IsDigit (c)) {x=x*Ten+c-'0'; C=GetChar ();}}Const intmaxn=100010;intN,M,U[MAXN],V[MAXN];intH[MAXN],SZ,R[MAXN];structedge{intU,v,nx;} E[MAXN];voidAddintAintb) {e[sz].u=a; E[sz].v=b; e[sz].nx=H[a]; H[a]=sz++;}BOOLCheckintx) {memset (H,-1,sizeofh); sz=0; Memset (R,0,sizeofR); for(intI=1; i<=x;i++) Add (U[i],v[i]), r[v[i]]++; Queue<int>p; intnum=0; for(intI=1; i<=n;i++)if(r[i]==0) Q.push (i), num++; if(num!=1)return 0; while(!Q.empty ()) { inttop=Q.front (); Q.pop (); Num=0; for(inti=h[top];i!=-1; i=e[i].nx) {R[E[I].V]--; if(r[e[i].v]==0) {num++; Q.push (E[I].V); } } if(num>1)return 0; } return 1;}intMain () {scanf ("%d%d",&n,&m); for(intI=1; i<=m;i++) scanf ("%d%d",&u[i],&V[i]); Check (4); intL=1, r=m,ans=-1; while(l<=R) {intM= (L+R)/2; if(check (m)) ans=m,r=m-1; Elsel=m+1; } printf ("%d\n", ans); return 0;}
Codeforces 645D Robot rapping Results report