Codeforces 645D Robot rapping Results report

Source: Internet
Author: User

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

Related Article

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: 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.