Is It A Tree?,判断是否是一棵树。(题目来源:九度OJ 1481,2012年北京大学计算机研究生机试真题)
2013-11-13 17:27
567 查看
题目描述:
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.
输入:
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 and less than 10000.
输出:
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).
样例输入:
样例输出:
来源:
2012年北京大学计算机研究生机试真题
我的思路:
并查集的使用。
需要注意一下的情况:
1.空树
2.不同路径之间出现通路
3.出现环路
4.重复路径(在我下面的代码中没有体现出来,当时我估计数据应该不会这么恶心,但是pdu还是poj上面还真的有重复的路径的case)
以下是AC代码:
附另一种思路:
DFS判断,DFS的过程中,可以判断是否出现环路。我感觉这题的数据量假如设int G[10000][10000]会偏大,使用邻接矩阵有可能内存溢出。
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.
输入:
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 and less than 10000.
输出:
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).
样例输入:
6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0 -1 -1
样例输出:
Case 1 is a tree. Case 2 is a tree. Case 3 is not a tree.
来源:
2012年北京大学计算机研究生机试真题
我的思路:
并查集的使用。
需要注意一下的情况:
1.空树
2.不同路径之间出现通路
3.出现环路
4.重复路径(在我下面的代码中没有体现出来,当时我估计数据应该不会这么恶心,但是pdu还是poj上面还真的有重复的路径的case)
以下是AC代码:
#include"stdio.h" #include"string.h" int s,e; int node[10000]; int root[10000]; int flag,num = 1; int empty; void initial() { int i; flag = 0; empty = 1;//判断是否为空树的标记 memset(node,0,sizeof(node)); for(i = 0 ; i < 10000 ; ++i) root[i] = i; } int getRoot(int n,int m) { if(root != n){ if(root == m) return 0;//如果在查找父节点过程中(s,e)路径中的e,即是出现环路。 n = root ; } return root ; } void join(int s,int e) { int a = getRoot(s,e);//查找父结点的时候,有没有出现环路。 int b = getRoot(e,0); if(!a){//出现环路 flag = 1; return ; } empty = 0;//只要有路径出现,就不可能是空树 node[s] = node[e] = 1; if((b != a && b != e) || s == e) flag = 1;//flag标记指向自己的路径或者不同路径之间存在一条通路的错误 else root[e] = a; } int judge() { int r = 0; int i; if(empty) return 1;//空树也是树 if(flag == 1) return 0; for(i = 1; i < 10000 ; ++i)//获得树根结点 { if(node[i]) { r = getRoot(i,0); break; } } for(; i < 10000 ; ++i) { if(!node[i]) continue; if(r != getRoot(i,0)) return 0; } return 1; } int main() { initial(); while(~scanf("%d%d",&s,&e)) { if(s && e) join(s,e); else if(!s && !e) { if(judge()) printf("Case %d is a tree.\n",num++); else printf("Case %d is not a tree.\n",num++); initial(); } else if(s == -1 && e == -1) break; } return 0; }
附另一种思路:
DFS判断,DFS的过程中,可以判断是否出现环路。我感觉这题的数据量假如设int G[10000][10000]会偏大,使用邻接矩阵有可能内存溢出。
相关文章推荐
- 2012年北京大学计算机研究生机试真题 -Is It A Tree?
- 九度题目1113:二叉树 2007年北京大学计算机研究生机试真题
- 九度 题目1112:拦截导弹 2007年北京大学计算机研究生机试真题 动态规划
- 九度题目1446:Head of a Gang 2012年浙江大学计算机及软件工程研究生机试真题
- 九度机试 题目1165:字符串匹配 2008年北京航空航天大学计算机研究生机试真题
- 题目1153:括号匹配问题(2010年北京大学计算机研究生机试真题,堆栈的使用)
- 九度机试 题目1165:字符串匹配 2008年北京航空航天大学计算机研究生机试真题
- 题目1144:Freckles(2009年北京大学计算机研究生机试真题)
- 九度题目1012:畅通工程 2005年浙江大学计算机及软件工程研究生机试真题
- 九度题目1042:Coincidence 2008年上海交通大学计算机研究生机试真题
- 九度题目1024:畅通工程 2007年浙江大学计算机及软件工程研究生机试真题
- 九度题目1020:最小长方形 2007年浙江大学计算机及软件工程研究生机试真题
- 九度 题目1057:众数 2011年 哈尔滨工业大学 计算机 研究生 机试真题
- 九度题目1003:A+B && 2010年浙江大学计算机及软件工程研究生机试真题
- 九度题目1088:剩下的树 2011年清华大学计算机研究生机试真题
- 九度题目1014:排名 && 2005年浙江大学计算机及软件工程研究生机试真题
- 2005年浙江大学计算机及软件工程研究生机试真题 -九度题目1013:开门人和关门人
- 题目1118:数制转换(2008年北京大学图形实验室计算机研究生机试真题)
- 九度题目1154:Jungle Roads Kruskal算法 2010年北京大学计算机研究生机试真题
- 九度题目1015:还是A+B && 2006年浙江大学计算机及软件工程研究生机试真题