HDU ACM 1272 Little Greek maze-and-check

Source: Internet
Author: User

Analysis: Test instructions is to determine whether the diagram is connected without a ring, use and check set can be a good solution.

1, judge whether to ring, just judge the input side of the two points. Have a common ancestor, then these two points are ring.
2, determine whether connectivity, as long as the number of root nodes to determine 1.
3, note: When the input data is only 0 0 o'clock, also satisfies the condition, should output "Yes".


#include <iostream> #include <algorithm>using namespace std;int p[100001];bool Init (int n)//Open Start by pointing to yourself {for (int i=0;i<=n;i++) P[i]=i;return true;}      int find (int x)//Find root node {int r,i,j;r=x;while (r!=p[r]) r=p[r]; Path compression ready, find root node I=x;while (I!=p[i]) {j=p[i];p [I]=r;i=j;}                 return i; Returns the root node}bool merge (int x,int y)//Returns whether it is necessary to merge {int tx,ty;tx=find (x); Ty=find (y); if (tx==ty) return False;p[tx]=ty;return TR UE;} int main () {bool V[100001],flag;int a,b,i,k;while (cin>>a>>b) {if (a==-1 && b==-1) break;if (a==0 && b==0)//Only 0 0 also output yes{cout<< "Yes" <<endl;continue;} Init (100000); memset (V,0,sizeof (v)); Flag=0;if (! Merge (A, b)) flag=1; Do not need to merge then ring V[a]=v[b]=1;while (cin>>a>>b) {if (a==0 && b==0) break;if (! Merge (A, b)) flag=1; Do not need to merge into the ring v[a]=v[b]=1;} K=0;for (i=1;i<=100000;i++) if (V[i] && p[i]==i)//Find root node k++;if (k!=1 | | flag) cout<< "No" <<endl; else cout<< "Yes" &LT;&LT;endl;}  return 0; }


HDU ACM 1272 Little Greek maze-and-check

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.