POJ 1308 Is It A Tree?
来源:互联网 发布:直角坐标机械手编程 编辑:程序博客网 时间:2024/06/08 05:12
并查集题型。
就是检查他们是否是一棵树。
明确树的构成。是否有多个点接在一个点上。
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[n]=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??
- poj 1308 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?
- POJ 1308 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?
- POJ 1308 Is It A Tree?
- POJ-1308-Is It A Tree?
- POJ 1308 Is It A Tree?
- POJ 1308 Is It A Tree?
- spring @component的作用
- mongoDB 技巧
- R语言学习
- 跟我一起写makefile
- C#飞机大战(仿微信)
- POJ 1308 Is It A Tree?
- Java学习系列(九)Java面向对象之异常机制详解
- C++ 排序方法集锦
- 面试题目
- Activity 变成对话框,然后再隐藏?
- 【Java TCP/IP Socket】应用程序协议中消息的成帧与解析(含代码)
- C++创建对象的三种方式
- script 编译成 asset 解决方案
- iPhone iPad 各种控件默认高度