POJ 1308 Is It A Tree?
2013-11-06 13:40
253 查看
并查集题型。
就是检查他们是否是一棵树。
明确树的构成。是否有多个点接在一个点上。
POJ AC代码:
就是检查他们是否是一棵树。
明确树的构成。是否有多个点接在一个点上。
POJ AC代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a[10010],c[10010],v[10010]; void intt(int n) { for(int i=1;i<=n;i++) a[i]=i,c[i]=0,v[i]=0; } int findset(int x) { if(a[x]!=x) a[x]=findset(a[x]); return a[x]; } void unionset(int x,int y) { x=findset(x),y=findset(y); if(x==y)return ; a[y]=x; } int main() { int n,m,tmp=0,cont=1;bool ok=1; intt(10010); while(~scanf("%d%d",&n,&m),n>=0||m>=0) { tmp=max(tmp,max(n,m)); if(n==0&&m==0) { int sum=0; for(int i=1;i<=tmp;i++) { if(v[i]==1&&findset(i)==i) sum++; if(c[i]>1){ok=0;break;} } if(sum>1)ok=0; if(ok)printf("Case %d is a tree.\n",cont++); else printf("Case %d is not a tree.\n",cont++); intt(10010),tmp=0,ok=1; continue; } if(m!=n&&findset(n)==findset(m)||m==n) ok=0; else { c[m]++; v =v[m]=1; unionset(n,m); } } }
相关文章推荐
- poj 1308 Is it a Tree?
- POJ 1308 Is it a tree??
- poj 1308 Is It A Tree? (并查集)
- POJ ~ 1308 ~ Is It A Tree? (并查集 + set)
- Is It A Tree?(POJ_1308)
- POJ 1308 Is It A Tree?
- poj 1308 Is It A Tree?
- POJ1308 Is It A Tree?
- POJ1308——Is It A Tree?
- poj 1308 Is It A Tree?
- POJ 1308 Is It A Tree?(判断是否是一棵树)
- POJ 1308 Is it a tree
- POJ 1308 && HDU 1325 Is It A Tree? (并查集)
- poj 1308 Is It A Tree?
- hdu 1325/poj 1308 Is It A Tree? 并查集
- poj 1308 Is It A Tree? (并查集)
- POJ1308(Is It A Tree?)
- POJ 1308 Is It A Tree?(并查集)
- poj1308——Is It A Tree?(判断是否为树)
- HDU 1325 POJ 1308 Is It A Tree? (并查集)