Test instructions: Give an out-of-the-way graph and ask if there is only one route between every two points in this picture.
In addition to input annoying a little ... Not the standard input is bullying ... In short is to ask whether this picture is a tree, in fact, or compare the water, with and check the connection, for each side, if the connecting two points have been connected, then this side will cause the two points between the second road, so you can judge not set, merge when the number of records merged, if the last is the point-1 The merger will indicate compliance with the requirements.
1#include <stdio.h>2#include <string.h>3 4 intfa[100005],n,vi[100005],cnt;5 6 intMmax (intAintb) {7     returnA>b?a:b;8 }9 Ten voidinit () { One      for(intI=1; i<=100004; i++) fa[i]=i; A } -  - intFindintx) { the     intR=x,t; -      while(R!=fa[r]) r=Fa[r]; -      while(x!=R) { -t=Fa[x]; +fa[x]=R; -x=T; +     } A     returnR; at } -  - intMain () { -     intb; -      while(SCANF ("%d%d", &a,&b)!=eof&&a!=-1|| b!=-1){ -         if(a==0&&b==0){ inprintf"yes\n"); -             Continue; to         } + init (); -Cnt=0; theMemset (VI,0,sizeof(vi)); *         if(!Vi[a]) { $cnt++;Panax Notoginsengvi[a]=1; -         } the         if(!Vi[b]) { +cnt++; Avi[b]=1; the         } +         intX=find (a), Y=find (b), ans=0; -         BOOLf=1; $         if(x!=y) { $fa[x]=y; -ans++; -         } the         Elsef=0; -          while(SCANF ("%d%d", &a,&b) &&a!=0|| b!=0){Wuyi             if(!Vi[a]) { thecnt++; -vi[a]=1; Wu             } -             if(!Vi[b]) { Aboutcnt++; $vi[b]=1; -             } -X=find (a), y=find (b); -             if(x!=y) { Afa[x]=y; +ans++; the             } -             Elsef=0; $         } the         if(ans==cnt-1&&F) printf ("yes\n"); the         Elseprintf"no\n"); the     } the     return 0; -}View Code
hdu1272 and check Set