题目源于网络
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.
题意
给出一些有序对(a, b) a是父亲节点,b是子节点,问这些结点是否构成一棵树。
解析
一棵树有三个要素:
1.除空树外, 有且只有一个根节点,没有根节点变成带环有向图,多个根节点变成森林;
2. 根节点入度为0,其他任意子节点入度为1,入度就相当于父亲节点,大于1说明有多个父亲节点,不是树;
3. 空树也是树,即0 0 情况是成立的。
#include<iostream>
#include<math.h>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
using namespace std;
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define req(i, a, b) for(int i=(a); i<=(b); i++)
#define ull unsigned __int64
#define sc(t) scanf("%d",&(t))
#define sc2(t,x) scanf("%d%d",&(t),&(x))
#define pr(t) printf("%d\n",(t))
#define pf printf
#define prk printf("\n")
#define pi acos(-1.0)
#define ms(a,b) memset((a),(b),sizeof((a)))
#define mc(a,b) memcpy((a),(b),sizeof((a)))
#define w while
typedef long long ll;
//没有根节点或者多个根节点的不是树,有两个以上父节点的也不是树
struct acm{
int father; //标记是否有父亲节点
int son; //标记是否有儿子节点
}f[10005];
int a, b;
bool flag;//判断是否是树,题目要求必须输入完整,因此只能用标记,不能直接输出
int main()
{
int cas = 1;
w(sc2(a, b) && a!=-1 && b!=-1)//为-1时停止
{
flag = 0;
if(a == 0 && b==0)//空树
{
pf("Case %d is a tree.\n", cas++);
continue;
}
if(a == b)//这里是一种特殊情况,如 1 1 0 0,
flag = 1;
ms(f, 0);
f[a].son++;
f[b].father++;
w(sc2(a, b) && a && b)
{
if(a == b)//有环就标记,如 1 2 2 5 5 5 0 0
flag = 1;
f[a].son++;//记录a的儿子节点数+1
f[b].father++;//记录b的父亲节点数+1
}
if(flag == 1)
{
pf("Case %d is not a tree.\n",cas++);
continue;
}
int cnt = 0;
rep(i, 1, 10001)
{
if(f[i].son && !f[i].father)//记录根节点个数,叶子节点不可能为根节点,所以f[i].son可以排除叶子节点和不在结构内的空点
cnt++;
if(f[i].father >= 2)//两个以上父节点不是树,如 1 2 1 3 3 5 5 2 0 0 ,2有1和5两个父节点
{
flag = 1;
break;
}
if(cnt >= 2)//两个以上根节点是森林 如 1 2 3 5 5 6 0 0,1和3两个根节点
{
flag = 1;
break;
}
}
if(flag == 1 || cnt == 0)//没有根节点也不是树,如 1 2 2 3 3 1
pf("Case %d is not a tree.\n",cas++);
else
pf("Case %d is a tree.\n", cas++);
}
return 0;
}