POJ1308——并查集
来源:互联网 发布:青岛少儿编程培训班 编辑:程序博客网 时间:2024/06/08 14:05
1.题目:
Is It A Tree?
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 29735 Accepted: 10173
Description
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.
There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.
In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.
In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
Input
The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
Output
For each test case display the line "Case k is a tree." or the line "Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).
Sample Input
6 8 5 3 5 2 6 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 3 5 6 5 2 0 0-1 -1
Sample Output
Case 1 is a tree.Case 2 is a tree.Case 3 is not a tree.2.题目大意:我们给你小于100个数的集合,然后我们让你判断我们输入的这些边集是否构成一棵树
3.思路:
简单并查集:
我们开辟并查集之后,首先要用记录数组记录哪些点出现过,我们根据出现过的点在进行计数操作,森林是不成立的
其次,我们要知道空时也算一棵树
最后,我们一旦出现练得两个节点出现在一个集合中的话,那么我们在进行连边操纵必然导致环路的生成,就不是树了
就这三点记住就好
4.AC代码 0Ms水过
#include"iostream"#include"cstdio"#include"cstdlib"#include"cstring"#define N 100using namespace std;int fa[N];int deep[N];int n;int x,y; bool flag=0;bool kright=0;bool book[N];void init(){for(int i=1;i<N;i++){fa[i]=i;deep[i]=1;}memset(book,0,sizeof(book));}int find(int x){if(x==fa[x]) return x;else return fa[x]=find(fa[x]);}void unit(int x,int y){x=find(x);y=find(y);if(x==y) return ;else{if(deep[x]>deep[y]) fa[y]=x;else{if(deep[x]==deep[y]) deep[y]++;fa[x]=y; }}}bool same(int x,int y){return find(x)==find(y); }int main(){int t=0;while(1){kright=true; t++;init();while(scanf("%d%d",&x,&y)&&x!=0&&y!=0){if(x==-1&&y==-1){flag=1;break;}if(find(x)==find(y)) kright=0;unit(x,y);book[x]=book[y]=1;} if(flag==1) break;int sum=0;for(int i=1;i<N;i++){if(fa[i]==i&&book[i]==1) sum++;}if(sum!=1&&sum!=0) kright=0;if(kright) printf("Case %d is a tree.\n",t);else printf("Case %d is not a tree.\n",t);}return 0;}
0 0
- POJ1308——并查集
- POJ1308-并查集
- Poj1308 并查集
- 并查集POJ1308
- poj1308【并查集】
- poj1308 并查集
- 并查集之POJ1308
- 并查集 poj1308 hd1272
- HDU1325 &&poj1308 基础并查集
- 【POJ1308】【坑点过多】【并查集】
- POJ1308-----并查集基础题
- hdu1856&&poj1308(基础并查集)
- POJ1308--并查集--Is It A Tree?
- Is It A Tree?--POJ1308--并查集
- poj1308 (并查集)Is It A Tree?
- hdu1325&&poj1308 Is It A Tree? 基础并查集
- POJ1308 - Is It A Tree? (并查集)
- POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!
- Android Studio进行单元测试
- Hadoop、Spark、Storm相关组件安装
- Web数据库SQL命令,数据库创建_增删改查_表与表之间的关联
- git commit 运行时提示错误
- 控制器之间的数据传递——storyboard中控制器的数据传递
- POJ1308——并查集
- 先序后序中序非递归算法
- Mysql Long类型时间 当日数据查询
- OpenTSDB安装与使用
- 第二周项目0-宣告“主权”
- 企业战略管理笔记
- redis3.0.0 集群安装详细步骤
- 阅读Android官方教程 Google Training 1.3 ----- Manage the Activity Lifecycle
- 欢迎使用Markdown编辑器写博客