首页 > 试题广场 >

Is It A Tree?

[编程题]Is It A Tree?
  • 热度指数:16955 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.

输入描述:
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 and less than 10000.


输出描述:
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).
示例1

输入

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

输出

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
头像 吴明示
发表于 2022-03-11 00:38:17
速度还行 #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <vector> using namespace st 展开全文
头像 大内高手
发表于 2020-03-28 19:08:46
并查集的题目 // runtime: 4ms // space: 608K #include <iostream> using namespace std; const int MAX = 10000; int father[MAX]; int height[MAX]; int in 展开全文
头像 Gargantua
发表于 2022-02-14 20:34:43
首先这道题很容易想到并查集,但是需要注意的是,树的节点之间有一定的关系,即双亲、孩子,但是对于这道题来说无需那么清楚的搞清他们的关系,只需要判断是不是树就可以了。因为一棵树,只有根节点入度为0,其余的点入度为1,如果你发现有某些点的入度高于1,那么它肯定不是一棵树。下面结合代码分析。 #includ 展开全文
头像 普罗列塔丽亚
发表于 2022-02-12 15:48:46
【基于并查集判断树】 +无环(a->b,b->a):getRoot(a)!=b +无多入(a->b,c->b):parent[b]==b +必连通:计算连通子图数 #include<iostream> #include<map& 展开全文
头像 flyflyfly00
发表于 2021-04-02 21:57:12
这样写case1不通过 #include <iostream> using namespace std; const int MAXN = 10000; int father[MAXN]; int height[MAXN]; int inDegree[MAXN]; bool visi 展开全文
头像 小猫学姐
发表于 2022-05-18 18:18:05
/** indegreeOne<node>:存放入度为1的所有节点。 indegreeZero<node>:存放入度为0的所有节点, 对边u,v: indeg0: 若u不存在,那么u加入indeg0,并且检查v在不在indeg0中。若v在,直接寄 若u存在,那么检查v即可 展开全文
头像 Coming680
发表于 2022-02-11 11:01:57
#include<iostream> #include<map> using namespace std; int main() { int s, e, ss = 0, cnt; bool ans = true; map<int, int> 展开全文
头像 牛客883116137号
发表于 2023-03-23 08:43:53
#include <iostream> #include<vector> using namespace std; int main() { //直接用树的定义,1仅有一个根 2每个结点仅被一个双亲结点指向 int m, n; int i 展开全文
头像 Huster水仙
发表于 2023-02-02 18:12:53
树的判定条件 单连通 一个根节点(入度为0) 无环(入度均不大于1或度数之和=结点数-1) #include<iostream> using namespace std; const int maxn=10010; int Father[maxn]; int height[maxn] 展开全文
头像 阿尔芒a
发表于 2024-03-16 17:49:55
#include<iostream> using namespace std; const int MAXN = 10001; int father[MAXN]; bool visited[MAXN]; int Indegree[MAXN]; int index = 0; void i 展开全文