#P1700. [USACO19OPEN] Milk Factory B

    ID: 11332 远端评测题 1000ms 256MiB 尝试: 2 已通过: 1 难度: 3 上传者: 标签>模拟图论2019USACOO2优化广度优先搜索 BFS深度优先搜索 DFS

[USACO19OPEN] Milk Factory B

Problem Description

The milk business is booming. Farmer John's milk factory has NN processing stations numbered 1N1 \dots N (1N1001 \le N \le 100), and N1N - 1 passages, each connecting two processing stations. (Building passages is expensive, so Farmer John chose to use the minimum number of passages such that starting from any processing station, it was possible to reach all other processing stations.)

To innovate and improve efficiency, Farmer John installed conveyor belts on each passage. Unfortunately, by the time he realized the conveyor belts were one-way, it was too late, and now each passage can only be traveled in one direction. So now it is no longer true that starting from every processing station, you can reach all other processing stations.

However, Farmer John believes things may not be a complete failure, as long as there still exists at least one processing station ii such that starting from every other processing station, it is possible to reach processing station ii. Note that going from any other processing station jj to processing station ii may pass through some intermediate stations between ii and jj. Please help Farmer John determine whether such a processing station ii exists.

Input Format

The first line of input contains an integer NN, the number of processing stations. The following N1N - 1 lines each contain two space-separated integers aia_i and bib_i, satisfying 1ai,biN1 \le a_i, b_i \le N and aibia_i \neq b_i. This indicates there is a conveyor belt that moves from processing station aia_i to processing station bib_i, and movement is only allowed in the direction from aia_i to bib_i.

Output Format

If there exists a processing station ii such that it is reachable from every other processing station, output the smallest such ii. Otherwise, output 1-1.

3
1 2
3 2
2

Hint

Translated by ChatGPT 5