hdu 1325 Is It A Tree?
2012-12-22 15:52
471 查看
/* 构成树的条件: 1.只有一个根节点,即有且只有一个点的出度不为0,入度为0; 2.除了根节点,所有节点的入度为1. 3.只能有一棵树,即只能有一个父亲结点(根节点), */ #include<stdio.h> #include<string.h> int p[1000001],a[1000001],b[1000001],in[1000001],out[1000001]; int find(int y) { if(p[y]!=y) return find(p[y]); return y; } int main() { int k=1; while(~scanf("%d%d",&a[0],&b[0])) { if(a[0]<0&&b[0]<0) break; else if(a[0]==0&&b[0]==0) { printf("Case %d is a tree.\n",k++); } else { int i; for(i=0;i<=1000000;i++) p[i]=i; memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); int t=0; in[b[0]]++; out[a[0]]++; p[b[0]]=find(p[a[0]]); while(1) { t++; scanf("%d%d",&a[t],&b[t]); if(a[t]==0&&b[t]==0) break; else { out[a[t]]++; in[b[t]]++; p[b[t]]=find(p[a[t]]); } } int z=find(p[a[0]]),flag=1,x=0,y=0; for(i=0;i<=1000000;i++) { if(find(p[i])!=z&&(in[i]!=0||out[i]!=0)) { flag=0; } if(out[i]>0&&in[i]==0) x++; if(in[i]>1) y++; } //printf("%d %d %d~~\n",flag,x,y); if(flag==0||y>0||x>1||x==0) printf("Case %d is not a tree.\n",k++); else printf("Case %d is a tree.\n",k++); } } return 0; }
相关文章推荐
- hdu1325 Is It A Tree?(有向图并查集)
- hdu 1325 Is It A Tree?
- HDU 1325 Is It A Tree?
- HDU 1325 Is It a Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?
- hdu 1325 Is It A Tree?(并查集)
- hdu 1325 Is It A Tree?(并查集)
- hdu 1325 Is It A Tree?
- 【并查集】HDU 1325 Is It A Tree?
- HDU 1325 Is It A Tree?(判断是否为树)
- hdu1325 Is It A Tree?并查集
- hdu1325 Is It A Tree? (并查集+森林)
- hdu-1325 & poj-1308 Is It A Tree?
- HDU 1325 Is It A Tree?
- 【HDU - 1325】Is It A Tree?
- 【并查集】HDU 1325 Is It A Tree?
- TOJ 1856 POJ 1308 HDU 1325 Is It A Tree? / 并查集
- POJ 1308 && HDU 1325 Is It A Tree?(并查集)