hdu1325 Is It A Tree? 判断是否为树 并查集
2017-04-22 19:47
465 查看
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24034 Accepted Submission(s): 5491
[align=left]Problem Description[/align]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.
[align=left]Input[/align]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.
输入将包含一系列描述(测试用例),后跟一对负整数。 每个测试用例将包括一系列边缘描述,后跟一对零每个边缘描述将由一对整数组成; 第一个整数标识边缘开始的节点,第二个整数标识边缘所针对的节点。 节点号将始终大于零。
[align=left]Output[/align]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).
[align=left]Sample Input[/align]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 [align=left]Sample Output[/align]Case 1 is a tree.Case 2 is a tree.Case 3 is not a tree.
/* 1,无环 2,除了根,所有的入度为1,根入度为0 3,这个结构只有一个根,不然是森林了 注意:空树也是树 */ #include<stdio.h> const int max_num=100000+10; typedef struct { int num,root,conn;//数据,根结点,入度 }Node; Node node[max_num]; void init()//初始化 { for(int i=0;i<max_num;i++) { node[i].conn=0;//入度初始化为0 node[i].root=i;//根结点为自身 node[i].num=0;//标记数字是否用过。0-没用过,1-用过 } } int find(int a)//寻找它的根节点 { if(node[a].root!=a) return node[a].root=find(node[a].root); else return node[a].root; } void merge(int a,int b)//合并 { a=find(a),b=find(b); if(a==b)//同一个根,说明在同一棵树里 return; node[b].root=a;//把b的根结点赋给a的根结点,此时a已经是根结点 } int main() { int n,m,i=1; bool flag=true; init(); while(scanf("%d%d",&n,&m)!=EOF&&n>=0&&m>=0) { if(!flag&&n!=0&&m!=0)continue;//判断是否是树 if(n==0&&m==0)//说明输入结束,开始处理吧 { int root_num=0; for(int j=1;j<max_num;j++) { //判断是否为森林。如果是,用root_num来记录根的数目 if(node[j].num&&find(j)==j) root_num++; if(node[j].conn>1)//如果出现某个节点的入度超过1,则不是树 { flag=false; break; } } if(root_num>1)//连通分支大于1,是森林,不是树 flag=false; if(flag) printf("Case %d is a tree.\n",i++); else printf("Case %d is not a tree.\n",i++); init(); continue; } if(m!=n&&find(n)==find(m)||m==n) flag=false; else { //将m,n记为结点 node[m].num=1; node .num=1; node[m].conn++;//入度加一 merge(n,m); } } return 0; }
判断是否为树
相关文章推荐
- [POJ 1308]Is It A Tree?(并查集判断图是否为一棵有根树)
- 并查集之判断图是否成环—小希的迷宫and is it a tree
- 并查集 HDU1325-Is It A Tree?
- POJ——1308Is It A Tree?(模拟拓扑排序判断有向图是否为树)
- 判断是否是树(Is It A Tree?)
- hdu1325 Is It A Tree?(并查集)
- HDU 1325 Is It A Tree?(判断是否为树)
- HDU1325 Is It A Tree? 并查集
- poj 1308 Is It A Tree? 并查集,有向树的判断
- Is it a tree(判断是否是树)
- PAT (Advanced Level) 1043. Is It a Binary Search Tree (25) 判断序列是否为BST的先序遍历,递归
- POJ 1308 Is It A Tree?(判断是否是一棵树)
- PAT 1043. Is It a Binary Search Tree (25)(判断是否是搜索树并构建和后序输出)(待修改)
- HDU 1325&&POJ 1308 Is It A Tree? 并查集判断能否树(能否形成环)
- HDU 1325 Is It A Tree? POJ 1308 Is It A Tree? (并查集+入度,判断一个有向图是树)
- HDU 1325 Is It A Tree? 判断是否为一棵树
- [并查集] hdu1325 Is it a tree ?
- hdu 1325 Is It A Tree? 判断是否是树 巨坑
- poj1308——Is It A Tree?(判断是否为树)
- HDU1325:Is It A Tree?(并查集)