标签:style blog os io 数据 for 2014 问题
和上一道小希的迷宫差不多,但是在HDU上提交一直WA,POJ过了
HDU的数据太强了吧,强的我感觉数据有问题
题意:输入若干对点,判断是否是一颗树,转化过来也就是是否存在环
点数-边数=1,还要判断每个点的入度都<=1
POJ AC代码
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> const int INF = 1e8; using namespace std; int father[1010]; bool vis[1010]; int rudu[1010]; 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<1010;i++) { father[i]=i; vis[i] = 0 ; rudu[i] = 0; } } int main() { int a,b,c = 0; while(scanf("%d%d",&a,&b)!=EOF) { c++; if(a<0&&b<0) break; int flag=1,t=0; if(a==0 && b==0) { printf("Case %d ",c); puts("is a tree."); continue; } init(); int num = 0; while(1) { if(a==0&&b==0) break; if(flag==1) { if(!vis[a]) {num++; } if(!vis[b]) {num++; rudu[b]++;} if(rudu[b]>1) flag = 0; vis[a]=1; vis[b]=1; if(Merge(a,b)==1) { t++; } else flag = 0; } scanf("%d%d",&a,&b); } printf("Case %d ",c); if(num-t==1 &&flag == 1) puts("is a tree."); else puts("is not a tree."); } return 0; }
HDU1325 &&poj1308 基础并查集,布布扣,bubuko.com
标签:style blog os io 数据 for 2014 问题
原文地址:http://blog.csdn.net/wjw0130/article/details/38380599