判断是否是树(Is It A Tree?)

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.

判断是否是树(Is It A Tree?)判断是否是树(Is It A Tree?)判断是否是树(Is It A Tree?)

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 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
 

Sample Output

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
 
 
 
问题描述:
输入多组测试案例,每组测试案例以-1,-1结束
每组案例中输入多组数据n,和m, 其中n指向m, 输入以0,0结束
判断上述输入得到的图是否是树
 
解题思路:
   满足树的条件:
        1:只能有一个根节点
        2:不能有环
        3:根节点入度为0, 其余节点入度为1(判断的时候直接判断是否所有节点的入度都小于等于1即可)
 
  一直wrong answer的原因:
              1:刚开始一直忘记删除重定向语句,我哩个去,一直纠结,够了,够了,这都发现不了
              2:判断树的时候只用了两个条件 
                 a:只能有一个树根
                 b:不能有环
               我原来以为满足这两个条件,所有节点的入度就都满足条件了,结果发现也可能存在节点入度大于1的情况
#include <stdio.h>
#define max_num 100000+10
typedef struct
{
int vis, root, conn;
}Node;
Node node[max_num];
void init()
{
for(int i = ; i < max_num; i++)
{
node[i].vis = ;
node[i].root = i;
node[i].conn = ;
}
}
int find_root(int x)
{
while(x != node[x].root)
x = node[x].root;
return x;
}
void union_set(int x, int y)
{
x = find_root(x);
y = find_root(y);
if(x != y)
node[y].root = x;
}
int main()
{
int n, m;
bool flag = true;
int case_num = ;
init();
//freopen("input.txt", "r", stdin);
while(scanf("%d%d", &n, &m), n >= && m >= )
{
if(!flag && (n != && m != ))
continue;
if(n == && m == )
{
int root_num = ;
for(int i = ; i < max_num; i++)
{
if(find_root(i) == i && node[i].vis)
root_num++;
if(node[i].conn > )
flag = false;
}
if(root_num > )
flag = false;
if(flag)
printf("Case %d is a tree.\n", case_num++);
else
printf("Case %d is not a tree.\n", case_num++);
flag = true;
init();
continue;
}
if(n != m && find_root(n) == find_root(m))
flag = false;
else
{
node[n].vis = ;
node[m].vis = ;
node[m].conn++;
union_set(n, m);
}
}
return ;
}
上一篇:C++类成员函数的 重载、覆盖和隐藏区别


下一篇:浅谈Android View滑动冲突