luogu#P12017. [NOISG 2025 Finals] Reachability
[NOISG 2025 Finals] Reachability
题目描述
Sheepland is a country with cities. There are roads connecting pairs of cities with each other. Road directly connects cities and . Initially, it is possible to travel from any city to any other city using only these roads.
All roads in Sheepland are planned to be renovated. Under the renovation plan, each road will be in one of four states:
- Two-way: citizens from both cities and may cross this road into the other city.
- One-way from city to city : only citizens from city may cross this road into city .
- One-way from city to city : only citizens from city may cross this road into city .
- Closed: no citizens from cities or may cross this road into the other city.
Unfortunately, the renovation plan has gone missing!
To try to recover it, you ask the mayor of each city how many cities are reachable from their city under the renovation plan. The mayor of the -th city replies with . However, some mayors may have provided incorrect values.
A city is considered reachable from a city u if there exists a sequence where and a crossable road exists from to for all . In particular, a city is reachable from itself.
Help Sheepland determine whether there exists a renovation plan that is consistent with the number of cities reachable from each city, as reported by all mayors!
输入格式
Your program must read from standard input.
The first line of input contains one integer .
The second line of input contains space-separated integers .
The following lines of input each contain two space-separated integers. The -th of these lines contains and .
输出格式
Your program must print to standard output.
Output YES
if a renovation plan that is consistent with the number of cities reachable from each city, as reported by all mayors, exists and NO
otherwise.
9
5 2 3 5 2 3 1 1 1
1 4
4 5
2 5
3 6
5 6
6 9
7 8
4 7
YES
9
5 2 3 5 2 3 1 1 2
1 4
4 5
2 5
3 6
5 6
6 9
7 8
4 7
NO
7
3 3 1 3 2 1 2
3 4
1 2
6 2
7 3
5 6
4 2
YES
提示
Subtasks
For all test cases, the input will satisfy the following bounds:
- for all
- for all
- for all
- Initially, it is possible to travel from any city to any other city using roads only.
Your program will be tested on input instances that satisfy the following restrictions:
Subtask | Marks | Additional Constraints |
---|---|---|
Sample test cases | ||
If a plan exists, at least one such plan has no two-way roads | ||
No additional constraints |
Sample Test Case 1 Explanation
This test case is valid for subtasks , and .
Refer to the diagram below. The renovation plan is consistent with the number of cities reachable from each city, as reported by all mayors.
Sample Test Case 2 Explanation
This test case is valid for subtasks , and .
There does not exist a renovation plan consistent with the number of cities reachable from each city, as reported by all mayors.
Sample Test Case 3 Explanation
This test case is valid for subtasks , and .