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.
给了一些有序实数对,然后判断是否是一棵树。其实就是判断是否有一个儿子指了两个父亲。
就是儿子节点指向的父亲只能更新一次。
然后就是空树也是树……
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1005;
int p[100005];
bool leap[100005];
int find(int x)
{
return p[x] == x?x:p[x] = find(p[x]);
}
void union_set(int x,int y)
{
x = find(x);
y = find(y);
if( x== y)return;
p[y] = x;
}
int main()
{
#ifdef LOCAL
freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
#endif // LOCAL
int a,b,kase = 1;
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a == -1&&b == -1)break;
if(a == 0&&b == 0)
{
printf("Case %d",kase++);
printf(" is a tree.\n");
continue;
}
int temp = a;
for(int i = 1;i <= 100000;i++)
{
p[i] = i;leap[i] = false;
}
int x,y;
bool ok = true;
leap[a] = leap[b] = true;
if(a == b)ok =false;
else
union_set(a,b);
while(scanf("%d%d",&x,&y))
{
if(!x&&!y)break;
leap[x] = leap[y] = true;
x = find(x);y = find(y);
if(x == y)
{
ok = false;continue;
}
union_set(x,y);
}
for(int i = 1;i <= 100000;i++)
{
if(leap[i])
{
if(find(i) != find(temp))ok = false;
}
}
printf("Case %d",kase++);
if(!ok)printf(" is not a tree.\n");
else printf(" is a tree.\n");
}
return 0;
}
该博客讨论了如何确定给定的节点连接是否形成一棵树。树是一种特殊的数据结构,具有唯一根节点、每个非根节点有且仅有一个父节点以及从根到每个节点存在唯一路径的特性。博客提供了输入输出格式,并给出了示例输入和输出,说明了当存在一个节点有两个父节点时,该集合不构成树。
3681

被折叠的 条评论
为什么被折叠?



