2024夏-A-4 Is It A Tree
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
题目描述
By definition, a tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node , called the root; and zero or more nonempty (sub)trees, each of whose roots are connected by a directed edge from . In other words, a tree is a spacial case of a directed graph. Given a directed graph, your job is to tell whether or not it is a tree.
输入格式
Each input file contains one test case, which starts from a line containing a positive integer (), the number of nodes. Hence we assume that all the nodes are numbered from 1 to . Then several lines follow, each describes a directed edge in the format
source destination
where both are the nodes' indices. It is guaranteed that there are no more than edges, and that source
is never the same as destination
. The input ends with source
being zero, and that line must not be processed.
Note: duplicated edges are counted as ONE edge (as shown by the first sample).
输出格式
If the given graph is a tree, out put in a line yes root
, where root
is the index of the root. Or if not, output no k
where k
is the number of nodes with 0 indegree.
样例
7
2 1
2 1
4 3
4 2
6 5
6 4
6 7
0
yes 6
7
2 1
4 3
4 2
6 4
6 7
5 3
0
no 2
限制
对于所有的测试用例,限制为800 ms, 256 MB