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" <<endl;} return 0; }
HDU ACM 1272 Little Greek maze-and-check