POJ 1308 hdu 1325 Is It A Tree?【并查集+入度 判断一个有向图是树】
来源:互联网 发布:maven构建java项目 编辑:程序博客网 时间:2024/06/05 02:37
POJ 1308
hdu 1325
Is It A Tree?
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 17384 Accepted: 5956
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.
Source
North Central North America 1997
题意:判断一个有向图是否是树
算法:并查集+入度
注意:判断点是否使用。
开始我选的是记录最大值和最小值,但是居然连样例都过不了,后来输出了中间变量才发现,中间有些点没有用。
所以还要开一个数组 used[] 判断下标是否使用过。
树的定义:1.所有使用过的下标全在一个连通分量中。
2.根节点的入度为 0 ,其它所有节点的入度为 1.
奇怪的地方:没有给出下标范围,1000居然过了。
/*BAccepted240 KB0 msC++1875 B2013-04-07 19:47:27*/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1000;int p[maxn]; //存父亲节点int r[maxn]; //存入度bool used[maxn]; //判断下标是否使用int Max, Min; //判断树中使用的下标的最值void set() //初始化{ for(int x = 0; x < maxn; x++) { p[x] = x; //自己为自己的父亲节点 r[x] = 0; //各点独立 入度为 0 used[x] = false; //各点均未使用 } Min = maxn; Max = -maxn;}int find(int x) //找根节点{ return x == p[x] ? x : p[x] = find(p[x]);}void Union(int x, int y) //合并两点所在的树{ int fx = find(x); int fy = find(y); p[fy] = fx;}int main(){ int a, b; int C = 0; //记录是第几组数据 while(scanf("%d%d", &a, &b) != EOF) { if(a == -1 && b == -1) break; set(); //初始化 p[b] = a; r[b]++; //处理第一对数据 Min = min(a, b); Max = max(a, b); used[a] = true; //标记使用 used[b] = true; int x, y; bool flag = true; while(scanf("%d%d", &x, &y) != EOF) { if(x == 0 && y == 0) { int root = 0, index; //记录根节点个数 和下标 for(int i = Min; i <= Max; i++) { if(used[i] && i == p[i]) //判断有几个根 { root++; index = i; } } if(root != 1) flag = false; //一棵树只能有一个根 if(flag) { for(int i = Min; i <= Max; i++) //判断入度情况,除了根节点,其它节点入度均为1 { if(i != index && used[i] && r[i] != 1) flag = false; } } if(flag) printf("Case %d is a tree.\n", ++C); else printf("Case %d is not a tree.\n", ++C); //printf("%d\n", root); //开始有些小错误 输出中间变量,找错误 break; //注意:易忘记 } if(find(x) != find(y)) Union(x, y); //如果不在同一棵树中,则合并 r[y]++; //入度+1 used[x] = true; //标记使用 used[y] = true; Min = min(Min, x); Min = min(Min, y); //更新最值 Max = max(Max, x); Max = max(Max, y); } } return 0;}
- POJ 1308 hdu 1325 Is It A Tree?【并查集+入度 判断一个有向图是树】
- poj 1308 Is It A Tree? 并查集,有向树的判断
- [POJ 1308]Is It A Tree?[并查集][判断有向树]
- HDU 1325 Is It A Tree?(并查集/有向树的判断)
- HDU:1325 Is It A Tree?(并查集+有向图树的判断)
- hdu 1325 Is It A Tree?(利用入度判断有向树)
- 【并查集】hdu 1325 Is It A Tree? 或 poj 1308 Is It A Tree?
- HDOJ 1325 Is It A Tree?(并查集+入度判断)
- HDU 1325&&POJ 1308 Is It A Tree? 并查集判断能否树(能否形成环)
- TOJ 1856 POJ 1308 HDU 1325 Is It A Tree? / 并查集
- POJ 1308 && HDU 1325 Is It A Tree? (并查集)
- poj 1308 &&HDU 1325 Is It A Tree?(并查集、树的定义)
- HDU 1325 POJ 1308 Is It A Tree? (并查集)
- POJ 1308 && HDU 1325 Is It A Tree?(并查集)
- HDU 1325&POJ 1308 Is it A tree ? [并查集+树判定]
- hdu 1325/poj 1308 Is It A Tree? 并查集
- POJ 1308 && HDU 1325 Is It A Tree?(并查集)
- 【解题报告】 POJ 1308 Is It A Tree? 并查集判断一棵树
- wp7激活码注册教程
- Problem A Jason的特殊爱好
- CSS3特效之转换transform
- WP7新手必知 注册一个可用的Windows Live ID
- linux API函数总结
- POJ 1308 hdu 1325 Is It A Tree?【并查集+入度 判断一个有向图是树】
- Redis数据备份与恢复
- CSS3特效之渐变linear-gradient
- js 复制文本框内容到剪贴板
- Xubuntu 12.04 搭建Eclipse Android 开发环境 adb
- cvDilate()膨胀 cvErode()腐蚀函数
- Hadoop 键值对的MapReduce过程剖析
- opencv-图像仿射变换
- 八种主流NoSQL数据库系统对比