标签:style os io for html amp htm size
杭电的图论题目列表,共计500题,努力刷吧
AC 64ms
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> const int INF = 1e8; using namespace std; int father[100010]; bool vis[100010]; int findx(int r) { int i = r,j; while(father[r]!=r) { r=father[r]; } while(father[i]!=r) { j = father[i]; father[i] = r; i = j; } return r; } bool Merge(int x,int y) { int fx,fy; fx=findx(x); fy=findx(y); if(fx!=fy) { father[fx]=fy; return 1; } else return 0; } void init() { for(int i=0;i<100010;i++) { father[i]=i; vis[i]=0; } } int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) { if(a==-1&&b==-1) break; int flag=1,t=0; if(a==0 && b==0) { puts("Yes"); continue; } init(); int num = 0; while(1) { if(a==0&&b==0) break; if(flag) { if(!vis[a]) num++; //num记录点数 if(!vis[b]) num++; vis[a]=1; vis[b]=1; if(Merge(a,b)==1) t++; //t记录边数 else flag = 0; } scanf("%d%d",&a,&b); } if(num-t==1 &&flag == 1)//满足题意的只能是 点数-边数==1 puts("Yes") else puts("No"); } return 0; }
HDU1272 小希的迷宫(基础并查集),布布扣,bubuko.com
标签:style os io for html amp htm size
原文地址:http://blog.csdn.net/wjw0130/article/details/38380161