hdu1856&&poj1308(基础并查集)
来源:互联网 发布:淘宝助理导入csv失败 编辑:程序博客网 时间:2024/05/18 10:58
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26363 Accepted Submission(s): 6028
Total Submission(s): 26363 Accepted Submission(s): 6028
Problem 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.并查集学习:http://blog.csdn.net/tribleave/article/details/72878239题意:大慨就是给你一些有向边,让你判断他们能不能构成一棵树。这里树的定义不能有环,空树也是树,每个节点的入度不能大于1,还有就是只有一个根节点,不然就是森林了。思路:直接并查集合并判断即可,具体见代码注释。代码:#include<bits/stdc++.h>using namespace std;const int maxn=1e5+5;int father[maxn],vis[maxn];int in[maxn];bool ans=true;/*int findset(int x){ if(x!=father[x]) father[x]=findset(father[x]); return father[x];}*///递归版本路径压缩有时候可能会爆栈,造成RE。
int findset(int x){ int k, j, r; r = x; while(r != father[r]) //查找跟节点 r = father[r]; //找到跟节点,用r记录下 k = x; while(k != r) //非递归路径压缩操作 { j = father[k]; //用j暂存parent[k]的父节点 father[k] = r; //parent[x]指向跟节点 k = j; //k移到父节点 } return r; //返回根节点的值}void Union(int a,int b){ int fa=findset(a); int fb=findset(b); if(fa==fb) ans=false;//判断是否形成环 else father[fa]=fb;}void init(){ ans=true; for(int i=0;i<maxn;i++) { in[i]=0; vis[i]=0; father[i]=i;//并查集初始化 }}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { if(n<0&&m<0) break; init(); static int t=1; if(n==0&&m==0) { printf("Case %d is a tree.\n",t++); continue; } vis[n]=vis[m]=1; in[m]++; Union(n,m); while(scanf("%d%d",&n,&m)&&(n+m)) { //if(n==0&&m==0) break; vis[n]=vis[m]=1; in[m]++; Union(n,m); } int cnt=0; for(int i=1;i<maxn;i++) { if(in[i]>1) ans=false;//某个点入度大于1也是不满足的 if(father[i]==i&&vis[i]) cnt++;//cnt用于记录根节点数目,大于1就是森林了 if(cnt>1||!ans) break; } if(!ans) { printf("Case %d is not a tree.\n",t++); continue; } if(cnt>1) printf("Case %d is not a tree.\n",t++); else printf("Case %d is a tree.\n",t++); } return 0;}
阅读全文
0 0
- hdu1856&&poj1308(基础并查集)
- hdu1856(基础并查集)
- HDU1325 &&poj1308 基础并查集
- POJ1308-----并查集基础题
- hdu1856 查并集
- hdu1856(并查集)
- POJ1308-并查集
- Poj1308 并查集
- 并查集POJ1308
- poj1308【并查集】
- poj1308 并查集
- 并查集之POJ1308
- 并查集 poj1308 hd1272
- hdu1325&&poj1308 Is It A Tree? 基础并查集
- POJ1308 Is it a tree? 基础并查集
- HDU1856(More is better)—并查集+树
- hdu1856 More is better(并查集)
- hdu1856 More is better(并查集 )
- 图像滤波之高斯滤波介绍
- C#回顾学习笔记三十:虚方法的重写
- python selenium 元素操作
- 18-用于大型程序的工具
- 十分钟读懂AES加密算法
- hdu1856&&poj1308(基础并查集)
- Leetcode之Search Insert Position 问题
- Android夜间模式实践
- 电脑快捷键
- 最小的K个数
- js如何往数组Array(list)中添加元素
- tensorflow 学习资料收集
- CSS样式重置
- 1课:单片机简叙