hdu 1272
808075 | asia2562219 | B | Accepted | 1172 KB | 31 ms | C++ | 1299 B | 2012-11-10 20:19:31 |
水爆了 Yes 一直写成 YES————!
并查集~
#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; 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!=-1) { if(x==0&&y==0) {if(judge())cout<<"Yes"<<endl; else cout<<"No"<<endl; 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; }