Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22100 Accepted Submission(s): 4999
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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/dbfd6338a244acaeed373e99012489cf.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3e74e44d0ed08edf8e4406d625477f8f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b8cf79d332319091be58f28180d228f3.png)
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 2 0 0时 结果为 not
//当测试数据为 1 2 3 2 0 0 时 结果是not
#include <iostream>
#include <string.h>
using namespace std;
int vis[100005], pre[100005], t, rudu[100000];
int find(int x)
{
if (pre[x] == x)
return x;
else return find(pre[x]);
}
void join(int x, int y)
{
int fx = find(x);
int fy = find(y);
if (fx != fy&&rudu[y] <= 1) //判断一下每次出入的父节点的入度
pre[fx] = fy;
else
t = 0;
}
int main()
{
std::cin.tie(false);
ios::sync_with_stdio(false);
int a, b, p, i, k, n = 0;
while (cin >> a >> b)
{
memset(rudu, 0, sizeof(rudu));
n++;
p = 1;
if (a < 0 && b < 0) break;
if (a == 0 && b == 0)
{
p = 0;
t = 1;
}
if (p)
{
for (i = 0; i <= 100000; i++)
{
pre[i] = i;
vis[i] = 0;
}
t = 1;
vis[a] = 1;
vis[b] = 1;
rudu[b]++;
join(a, b);
while (cin >> a >> b)
{
if (a == 0 && b == 0)
break;
vis[a] = 1;
vis[b] = 1;
rudu[b]++;
join(a, b);
}
}
k = 0;
for (i = 0; i <= 100000; i++)
{
if (pre[i] == i&&vis[i])
k++;
if (k > 1)
{
t = 0;
break;
}
}
if (t == 0) cout << "Case "<<n<<" is not a tree." << endl;
else cout << "Case " << n << " is a tree." << endl;
}
}