#P10787. [NOI2024] 树的定向
[NOI2024] 树的定向
Background
Due to performance differences among judging machines, the original time limit was , while on Luogu the time limit is .
Problem Description
Given a tree with vertices, numbered from to . The -th edge connects vertices and .
Now we want to assign a direction to every edge of the tree. Any orientation can be described by a string of length . Here, means the -th edge is directed as ; otherwise, means the -th edge is directed as .
Given pairs of vertices , where and .
A perfect orientation is defined as follows: under this orientation, for every , cannot reach .
Among all perfect orientations, find the one whose corresponding string is lexicographically smallest. It is guaranteed that at least one perfect orientation exists.
We say the lexicographical order of is smaller than if there exists an index such that and .
Input Format
The first line contains three non-negative integers , representing the test point ID, the number of vertices in the tree, and the number of vertex pairs. Here means this test point is a sample.
The next lines each contain two positive integers , describing an edge of the tree. It is guaranteed that and these edges form a tree.
The next lines each contain two positive integers . It is guaranteed that and .
Output Format
Output one line containing a string , the string corresponding to the lexicographically smallest perfect orientation.
0 4 2
1 2
2 3
3 4
3 2
1 4
001
0 6 8
5 1
2 3
1 2
5 6
4 3
4 3
5 1
6 3
5 4
1 4
5 2
3 6
6 2
10101
见 tree3.in/tree3.ans
这个样例满足测试点 1-3 的约束条件。
见 tree4.in/tree4.ans
这个样例满足测试点 4-6 的约束条件。
见 tree5.in/tree5.ans
这个样例满足测试点 7,8 的约束条件。
见 tree6.in/tree6.ans
这个样例满足测试点 9,10 的约束条件。
Hint
[Sample 1 Explanation]
In this sample, if , then in this orientation can reach (there is a path ), so it is not a perfect orientation. If , then in this orientation cannot reach , and cannot reach , so it is a perfect orientation. Therefore the answer is .
[Sample 2 Explanation]
In this sample, any perfect orientation must satisfy that cannot reach , and cannot reach . Therefore . If , then there exists a path , so can reach . Hence it is not a perfect orientation. Therefore, all perfect orientations must have lexicographically not smaller than . It is also easy to verify that when , the corresponding orientation is a perfect orientation.
[Constraints]
For all testdata, it is guaranteed that , , , and all edges form a tree; and .
It is guaranteed that at least one perfect orientation exists.
::cute-table{tuack}
| Test point ID | Special property | ||
|---|---|---|---|
| None | |||
| A | |||
| B | |||
| None | |||
| B | |||
| None | |||
- Special property A: It is guaranteed that appears in if and only if and are not adjacent in the tree.
- Special property B: It is guaranteed that the vertex numbered is adjacent to every other vertex in the tree.
Translated by ChatGPT 5