传统题 800ms 256MiB

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 rr, called the root; and zero or more nonempty (sub)trees, each of whose roots are connected by a directed edge from rr. 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 nn (104\le 10^4), the number of nodes. Hence we assume that all the nodes are numbered from 1 to nn. 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 10410^4 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

PAT2024夏季重现赛

未参加
状态
已结束
规则
IOI
题目
11
开始于
2025-8-10 14:00
结束于
2025-8-10 17:30
持续时间
3.5 小时
主持人
参赛人数
37