Test instructions: Give a picture without a direction, determine whether there is a ring, determine whether it is a tree;
Ideas: And check the set of the ring, the only ancestor;
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineM 0x3f3f3f3fintfa[500010],mark[500010];voidinit () { for(intI=0; i<=500010; i++) Fa[i]=i;}intFinintx) { while(X!=fa[x]) x=Fa[x]; returnx;}voidCombineintAintb) { intt1=Fin (a); intT2=fin (b); if(t1!=T2) {FA[T1]=T2; }}intMain () {intN,m,i,j,k,flag,cnt,num,ans,amin,amax; while(SCANF ("%d%d", &n,&m)! =EOF) { if(n==-1&&m==-1) Break; if(n==0&&m==0) {printf ("yes\n");Continue; } init (); memset (Mark,0,sizeof(Mark)); Flag=0; amax=0; amin=M; while(1) { if(n==0&&m==0) Break; if(amax<n) amax=N; if(amax<m) amax=m; if(amin>n) amin=N; if(amin>m) amin=L; if(Fin (n) ==fin (m))//determine if there is a ring{flag=1; } combine (n,m); Mark[n]=1; mark[m]=1; scanf ("%d%d",&n,&m); } if(flag) {printf ("no\n");Continue; } ans=-1; cnt=0; for(i=amin;i<=amax;i++)//determine if it is a tree { if(Mark[i]&&fin (i)! =ans) {ans=fin (i); CNT++; } } if(cnt==1) printf ("yes\n"); Elseprintf"no\n"); } return 0;}
HDU 1272 Little Greek Maze (no-map-ring)