hdu1272 and check Set

Source: Internet
Author: User

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&AMP;&AMP;F) printf ("yes\n"); the         Elseprintf"no\n"); the     } the     return 0; -}
View Code

hdu1272 and check Set

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: info-contact@alibabacloud.com 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.