POJ-1308-Is It A Tree?
2013-07-25 11:03
260 查看
这个题要求你判断所给的点集是否是一棵树,不算难吧,用并查集可以做,注意细节即可,0 0空树也要算树,
代码:
代码:
#include<cstdio> #include<cstring> #include<iostream> using namespace std; const int maxn=10011; int p[maxn]; bool vis[maxn]; int find(int x) { if(p[x]==-1) return x; return p[x]=find(p[x]); } void Union(int x,int y) { p[min(x,y)]=max(x,y); } int main() { int a,b,cas=1; while(scanf("%d%d",&a,&b)!=EOF) { if(a==-1&&b==-1) break; if(a==0&&b==0) { printf("Case %d is a tree.\n",cas++); continue; } memset(p,-1,sizeof(p)); memset(vis,0,sizeof(vis)); int n=0,ans=0; n=max(a,max(n,b)); int x=find(a); int y=find(b); vis[a]=vis[b]=1; if(x==y) ans++; else Union(x,y); while(scanf("%d%d",&a,&b)&&(a+b)) { n=max(a,max(n,b)); vis[a]=vis[b]=1; x=find(a); y=find(b); if(x==y) ans++; else Union(x,y); } for(int i=1;i<=n;i++) if(vis[i]&&p[i]==-1) ans++; if(ans==1) printf("Case %d is a tree.\n",cas++); else printf("Case %d is not a tree.\n",cas++); } return 0; }
相关文章推荐
- poj - 1308 - Is It A Tree?(Uva - 615, hdu - 1325)
- POJ 1308 Is It A Tree?(判断是否是一棵树)
- POJ 1308 Is It A Tree?
- poj 1308 Is It A Tree?
- HDU 1325 POJ 1308 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?(并查集)
- poj1308——Is It A Tree?(判断是否为树)
- poj 1308 is it a tree
- POJ 1308 Is It A Tree?
- hdu 1325/poj 1308 Is It A Tree? 并查集
- poj 1308 Is It A Tree?
- POJ 1308 Is It A Tree?
- Poj 1308 Is It A tree
- 【并查集】hdu 1325 Is It A Tree? 或 poj 1308 Is It A Tree?
- POJ 1308 Is it a tree(并查集)
- POJ-1308 Is It A Tree?
- POJ 1308 Is It A Tree?(并查集)