11007421 | asia2562219 | 1308 | Accepted | 1028K | 0MS | C++ | 1441B | 2012-11-11 20:08:11 |
hdu WA;………… 受不了这题,hdu 案例都过不了的代码能AC——!
View Code
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <iostream> using namespace std; #define MAXN 111111 int deg[MAXN],tot,father[MAXN],totnum,tote; bool flag=true; int find(int x) { int temp=x; while(temp!=father[temp]) temp=father[temp]; int i=x,j; while(i!=temp) { j=father[i]; father[i]=temp; i=j; } return temp; } bool judge() { if(!flag)return false; int odd=0,rot; if(tote==0&&totnum==0)return true; if(tote!=totnum-1)return false; rot=find(tot); for(int i=1;i<=tot;i++) if(deg[i]!=0&&rot!=find(i))return false; return true; } int main() { int x,y,i; int cases=1; tot=0;memset(deg,0,sizeof(deg)); for(i=0;i<=MAXN;i++)father[i]=i; totnum=tote=0; while(scanf("%d%d",&x,&y),x>=0) { if(x==0&&y==0) {if(judge())printf("Case %d is a tree.\n",cases); else printf("Case %d is not a tree.\n",cases); cases++; tot=0;memset(deg,0,sizeof(deg)); flag=true; for(i=0;i<=MAXN;i++)father[i]=i; totnum=tote=0;continue;} tot=max(max(tot,x),y); if(deg[x]==0)totnum++; if(deg[y]==0)totnum++; tote++; deg[x]++;deg[y]++; if(find(x)!=find(y)) father[find(x)]=find(y); else flag=false; } return 0; }
坑爹的hdu 巨坑也不填……