#P10787. [NOI2024] 树的定向

[NOI2024] 树的定向

Background

Due to performance differences among judging machines, the original time limit was 3 s3\ \mathrm{s}, while on Luogu the time limit is 6 s6\ \mathrm{s}.

Problem Description

Given a tree with nn vertices, numbered from 11 to nn. The ii-th edge (1in1)(1\leq i\leq n-1) connects vertices uiu_i and viv_i.

Now we want to assign a direction to every edge of the tree. Any orientation can be described by a string S=s1s2sn1S=s_1s_2\ldots s_{n-1} of length n1n-1. Here, si=0s_i=0 means the ii-th edge is directed as uiviu_i \to v_i; otherwise, si=1s_i=1 means the ii-th edge is directed as viuiv_i \to u_i.

Given mm pairs of vertices (ai,bi)(a_i,b_i), where 1ai,bin1\leq a_i,b_i \leq n and aibia_i\neq b_i.

A perfect orientation is defined as follows: under this orientation, for every 1im1\leq i\leq m, aia_i cannot reach bib_i.

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 S=s1s2sn1S=s_1s_2\ldots s_{n-1} is smaller than T=t1t2tn1T=t_1t_2\ldots t_{n-1} if there exists an index kk such that s1=t1,s2=t2,,sk1=tk1s_1=t_1, s_2=t_2, \ldots, s_{k-1}=t_{k-1} and sk<tks_k < t_k.

Input Format

The first line contains three non-negative integers c,n,mc,n,m, representing the test point ID, the number of vertices in the tree, and the number of vertex pairs. Here c=0c=0 means this test point is a sample.

The next n1n-1 lines each contain two positive integers ui,viu_i,v_i, describing an edge of the tree. It is guaranteed that 1ui,vin1\leq u_i,v_i\leq n and these n1n-1 edges form a tree.

The next mm lines each contain two positive integers ai,bia_i,b_i. It is guaranteed that 1ai,bin1\leq a_i,b_i \leq n and aibia_i\neq b_i.

Output Format

Output one line containing a string S=s1s2sn1S=s_1s_2\ldots s_{n-1}, the 0101 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 S=000S=000, then in this orientation 11 can reach 44 (there is a path 12341\to 2\to 3\to 4), so it is not a perfect orientation. If S=001S=001, then in this orientation 33 cannot reach 22, and 11 cannot reach 44, so it is a perfect orientation. Therefore the answer is 001001.

[Sample 2 Explanation]

In this sample, any perfect orientation must satisfy that 44 cannot reach 33, and 55 cannot reach 11. Therefore s1=s5=1s_1=s_5=1. If s2=s3=0s_2=s_3=0, then there exists a path 12341\to 2\to 3\to 4, so 11 can reach 44. Hence it is not a perfect orientation. Therefore, all perfect orientations must have SS lexicographically not smaller than 1010110101. It is also easy to verify that when S=10101S=10101, the corresponding orientation is a perfect orientation.

[Constraints]

For all testdata, it is guaranteed that 2n5×1052\leq n\leq 5\times 10^5, 1m5×1051\leq m\leq 5\times 10^5, 1ui,vin1\leq u_i,v_i\leq n, and all edges form a tree; 1ai,bin1\leq a_i,b_i \leq n and aibia_i\neq b_i.

It is guaranteed that at least one perfect orientation exists.

::cute-table{tuack}

Test point ID nn mm Special property
131\sim 3 15\leq 15 50\leq 50 None
464\sim 6 300\leq 300
7,87,8 400\leq 400 =(n1)(n2)=(n-1)(n-2) A
9,109,10 2000\leq 2\,000 B
111411\sim 14 None
15,1615,16 105\leq 10^5 B
17,1817,18 None
192119\sim 21 2×105\leq 2\times 10^5
222522\sim 25 5×105\leq 5\times 10^5
  • Special property A: It is guaranteed that (a,b)(a,b) appears in (ai,bi)(a_i,b_i) if and only if aba\neq b and a,ba,b are not adjacent in the tree.
  • Special property B: It is guaranteed that the vertex numbered 11 is adjacent to every other vertex in the tree.

Translated by ChatGPT 5