#P1329. Is It A Tree
Is It A Tree
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 n. 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.
样例 #1
样例输入 #1
7
2 1
2 1
4 3
4 2
6 5
6 4
6 7
0
样例输出 #1
yes 6