#P9246. [蓝桥杯 2023 省 B] 砍树

    ID: 10417 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2023最近公共祖先 LCA蓝桥杯省赛

[蓝桥杯 2023 省 B] 砍树

Problem Description

Given a tree with nn nodes and mm distinct unordered pairs $\left(a_{1},b_{1}\right),\left(a_{2},b_{2}\right),\ldots,\left(a_{m},b_{m}\right)$, where all aia_{i} are pairwise different, all bib_{i} are pairwise different, and aibj(1i,jm)a_{i} \neq b_{j}(1 \leq i,j \leq m).

Xiaoming wants to know whether it is possible to choose one edge in the tree and cut it, such that for every (ai,bi)\left(a_{i},b_{i}\right), aia_{i} and bib_{i} become disconnected. If it is possible, output the index of the edge that should be cut (indices start from 11 in the input order). Otherwise, output -1.

Input Format

The input has n+mn+m lines. The first line contains two positive integers n,mn,m.

The next n1n-1 lines each contain two positive integers xi,yix_{i},y_{i}, representing the two endpoints of the ii-th edge.

The last mm lines each contain two positive integers ai,bia_{i},b_{i}.

Output Format

Output one integer in one line, representing the answer. If there are multiple answers, output the one with the largest index.

6 2
1 2
2 3
4 3
2 5
6 5
3 6
4 5

4

Hint

Sample Explanation.

After cutting the 22-nd edge, two connected components are formed: {3,4},{1,2,5,6}\{3,4\},\{1,2,5,6\}, which satisfies that 33 and 66 are disconnected, and 44 and 55 are disconnected.

After cutting the 44-th edge, two connected components are formed: {1,2,3,4},{5,6}\{1,2,3,4\},\{5,6\}, which also satisfies that 33 and 66 are disconnected, and 44 and 55 are disconnected.

Since 44 has a larger index, the answer is 44.

Constraints.

For 30%30\% of the testdata, 1<n1031<n \leq 10^3 is guaranteed.

For 100%100\% of the testdata, 1<n1051<n \leq 10^{5} and 1mn21 \leq m \leq \frac{n}{2} are guaranteed.

Lanqiao Cup 2023 Provincial Contest B Group, Problem J.

Translated by ChatGPT 5