#P9246. [蓝桥杯 2023 省 B] 砍树
[蓝桥杯 2023 省 B] 砍树
Problem Description
Given a tree with nodes and distinct unordered pairs $\left(a_{1},b_{1}\right),\left(a_{2},b_{2}\right),\ldots,\left(a_{m},b_{m}\right)$, where all are pairwise different, all are pairwise different, and .
Xiaoming wants to know whether it is possible to choose one edge in the tree and cut it, such that for every , and become disconnected. If it is possible, output the index of the edge that should be cut (indices start from in the input order). Otherwise, output -1.
Input Format
The input has lines. The first line contains two positive integers .
The next lines each contain two positive integers , representing the two endpoints of the -th edge.
The last lines each contain two positive integers .
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 -nd edge, two connected components are formed: , which satisfies that and are disconnected, and and are disconnected.
After cutting the -th edge, two connected components are formed: , which also satisfies that and are disconnected, and and are disconnected.
Since has a larger index, the answer is .
Constraints.
For of the testdata, is guaranteed.
For of the testdata, and are guaranteed.
Lanqiao Cup 2023 Provincial Contest B Group, Problem J.
Translated by ChatGPT 5