http://acm.hdu.edu.cn/showproblem.php?pid=1811
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <iostream>5#include <queue>6 using namespacestd;7 #defineN 100108 intx[n*2],y[n*2];9 Charch[n*2][3];Ten intFa[n],deg[n]; One intF1,f2,tot; A structnode - { - intnext,to; the}edge[n*2]; - inthead[n],n,m,cnt; - /* - and check set + topology sorting + because there is an equal condition, put the equal condition in a set, when the topological sort - sort all the elements in the collection using only one root in that set. + The rest is the topological sort of content: A If there is a loop, then the number of points in the queue (0 after the update) is <n, at It is important to note that in the case of equals, it is also considered a point. - The case of relationship determination is that every time you join a team, it will only enter a zero-in point. - */ - voidInit () - { -F1=f2=1;//f1 = confict, F2 = Uncertain intot=0; -Cnt=0; tomemset (deg,0,sizeof(deg)); +memset (head,-1,sizeof(head)); - for(intI=0; i<n;i++){ thefa[i]=i; * } $ }Panax Notoginseng - intFind (intx) the { + if(X==fa[x])returnx; A returnfa[x]=Find (fa[x]); the } + - voidMerge (intXinty) $ { $ intFx=find (x), fy=Find (y); - if(fx!=FY) { -fa[fx]=fy; the } - }Wuyi the voidAddintUintv) - { Wuedge[tot].to=v; -edge[tot].next=Head[u]; Abouthead[u]=tot++; $ } - - voidTopper () - { Aqueue<int>que; + while(!que.empty ()) Que.pop (); the for(intI=0; i<n;i++){ - if(deg[i]==0&&fa[find (i)]==i) { $ Que.push (i); thecnt++; the } the } the while(!Que.empty ()) { - inttop=Que.front (); Que.pop (); in if(!que.empty ()) f2=0; the for(intk=head[top];~k;k=Edge[k].next) { the intv=edge[k].to; Aboutdeg[v]--; the if(deg[v]==0){ the Que.push (v); thecnt++; + } - } the }Bayi if(cnt<n) f1=0; the } the - intMain () - { the while(~SCANF ("%d%d",&n,&m)) { the init (); the for(intI=0; i<m;i++){ thescanf"%d%s%d",&x[i],ch[i],&y[i]); - if(ch[i][0]=='='){ thecnt++; the Merge (X[i],y[i]); the }94 } the for(intI=0; i<m;i++){ the if(ch[i][0]=='=')Continue; the intFx=find (X[i]), fy=Find (Y[i]);98 if(fx==fy) f1=0; About if(ch[i][0]=='<'){ - Add (fx,fy);101deg[fy]++;102 }103 Else{104 Add (FY,FX); thedeg[fx]++;106 }107 }108 topper ();109 if(f1==1&&f2==1) cout<<"OK"<<Endl; the Else if(f1==0|| f1==0&&f2==0) cout<<"CONFLICT"<<Endl;111 Elsecout<<"uncertain"<<Endl; the }113 return 0; the}
2016-06-27
HDU 1811:rank of Tetris (and set + topology sort)