标签:
题目大意:和HDU1272-小希的迷宫题目一样,
Input
#include<cstdio> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<queue> #define INF 0x3f3f3f3f #define MAX 1000005 using namespace std; int father[MAX],vis[MAX]; int Find(int x) { while(x!=father[x]) { x=father[x]; } return x; } int main() { int a,b,ok,i,k,cnt=1; while(scanf("%d%d",&a,&b),a!=-1 || b!=-1) { k=0; memset(vis,0,sizeof(vis)); vis[a]=1; vis[b]=1; ok=1; if(!a && !b) { printf("Case %d is a tree.\n",cnt++); continue; } for(i=0; i<MAX; i++) father[i]=i; int x=Find(a); int y=Find(b); if(x!=y) { father[x]=y; } else ok=0; while(scanf("%d%d",&a,&b),a||b) { vis[a]=1; vis[b]=1; int x=Find(a); int y=Find(b); if(x!=y) { father[x]=y; } else ok=0;//标记一下 } if(ok)//当ok=0时就不进入了,节省下时间 { for(i=0;i<MAX;i++) { if(vis[i] && father[i]==i) k++; } } if(k>1) ok=0; if(ok) printf("Case %d is a tree.\n",cnt++); else printf("Case %d is not a tree.\n",cnt++); } return 0; }
标签:
原文地址:http://www.cnblogs.com/alan-W/p/5746529.html