HDU 1325 Is It A Tree?(并查集简单题)
来源:互联网 发布:新软件发布 编辑:程序博客网 时间:2024/06/06 01:07
题目链接
并查集简单题,利用并查集来判断无向图的环
#include<iostream>using namespace std;const int maxn=1005;int fa[maxn],in[maxn],out[maxn],vis[maxn];int Find(int x){ if(fa[x]==x) return x; return fa[x]=Find(fa[x]);}void Merge(int x,int y){ x=Find(x); y=Find(y); if(x!=y) fa[y]=x;}int main(){ int x,y,Case=1; cin.sync_with_stdio(false); while(cin>>x>>y&&x+y>=0) { int Max=-1; if(x+y==0) { cout<<"Case "<<Case++<<" is a tree."<<endl; continue; } for(int i=0;i<maxn;i++) fa[i]=i,in[i]=0,out[i]=0,vis[i]=0; Merge(x,y); in[y]++; out[x]++; vis[x]=1; vis[y]=1; Max=max(Max,max(x,y)); while(cin>>x>>y,x+y) { Merge(x,y); in[y]++; out[x]++; vis[x]=1; vis[y]=1; Max=max(Max,max(x,y)); } int cnt=0; for(int i=0;i<=Max;i++) if(vis[i]&&fa[i]==i) cnt++; if(cnt!=1) { cout<<"Case "<<Case++<<" is not a tree."<<endl; continue; } int cnt1=0,cnt2=0; for(int i=0;i<=Max;i++) if(vis[i]&&in[i]==0) cnt1++; else if(vis[i]&&in[i]>1) cnt2++; if(cnt1!=1||cnt2>0) { cout<<"Case "<<Case++<<" is not a tree."<<endl; continue; } cout<<"Case "<<Case++<<" is a tree."<<endl; } return 0;}
0 0
- 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 Problem 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? (并查集)
- Is It A Tree? HDU 1325 并查集
- 【并查集】hdu 1325 Is It A Tree? 或 poj 1308 Is It A Tree?
- pdf转换成txt的操作方法分享
- MySQL大数据量表中删除重复记录
- C++11新特性
- Linux下获取硬盘相关信息
- java设计模式----建造者模式
- HDU 1325 Is It A Tree?(并查集简单题)
- vector的clear操作的内部过程
- 二叉树平衡检查(递归)
- Android学习笔记:Andorid网络请求框架Volley的使用(上)
- 信号--SIGCLD语义
- java RSA加密解密
- NumPy矩阵运算
- POJ 3126 Prime Path
- Java读写Properties属性文件