Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21579 Accepted Submission(s): 4857
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.
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、有且只有一个根节点(没有父节点的点)
2、每个节点只有一个父节点
3、 从根节点到任何一节点只有一条路径
题解:用幷查集的思想,不过不是用数组,而是用结构体来定义,要确保是否被用过,和被指了多少次
#include <stdio.h>
#define M 100010
struct node
{
int degree, num, root;//解释在下面
};
node no[M];
bool ok;
int find(int a)
{
int r = a;
while(r != no[r].root)//这里就不用pre[M]了
{
r = no[r].root;
}
int i = a, j;
while(r != no[i].root)
{
j = no[i].root;
no[i].root = r;
i = j;
}
return r;
}
void join(int x, int y)
{
int fx = find(x);
int fy = find(y);
if(fx != fy)
{
no[y].root = x;
}
}
void init()
{
for(int i=0; i<M; i++)
{
no[i].degree = 0;//记录被指了多少次
no[i].num = 0;//记录是否被指过
no[i].root = i;//相当于并查集的根
}
}
int main()
{
int n, m, ca = 1;
bool ok = false;
init();
while(scanf("%d%d", &n, &m) != EOF && n >= 0 && m >= 0)
{
if(ok && n != 0 && m != 0)//当已经确定不是的时候,就没必要浪费时间进行下面的了
continue;
if(n == 0 && m == 0)
{
int sum = 0;
for(int i=0; i<M; i++)
{
if(no[i].num && find(i) == i)//num == 0 的时候表示没有这个点
{
sum++;
}
if(no[i].degree > 1)
{
ok = true;
break;
}
}
if(sum > 1)
{
ok = true;
}
if(!ok)
{
printf("Case %d is a tree.\n", ca++);
}
else
{
printf("Case %d is not a tree.\n", ca++);
}
init();
ok =false;
continue;
}
if(m != n && find(m) == find(n))//根是一样的就不可能再相连了
{
ok = true;
}
else
{
no[n].num = 1;
no[m].num = 1;
no[m].degree++;
join(n, m);
}
}
return 0;
}