#P10388. [蓝桥杯 2024 省 A] 团建
[蓝桥杯 2024 省 A] 团建
Problem Description
Xiaolan is doing team building with friends. There is a game that requires two people to cooperate. Each person receives a tree of size and respectively, and each node on the tree has a positive integer weight.
Starting from the root node of their own tree, each person needs to walk to some leaf node. The weights of all nodes on the path from the root to that leaf form a positive integer sequence. Their score is the length of the longest common prefix of the two sequences. Given the two trees, compute the maximum possible score.
Input Format
The first line contains two positive integers , separated by a space.
The second line contains positive integers , separated by spaces, where denotes the weight of node in the first tree.
The third line contains positive integers , separated by spaces, where denotes the weight of node in the second tree.
The next lines each contain two positive integers indicating that the first tree has an edge between and .
The next lines each contain two positive integers indicating that the second tree has an edge between and .
Output Format
Output one line containing one integer, the answer.
2 2
10 20
10 30
1 2
2 1
1
5 4
10 20 30 40 50
10 40 20 30
1 2
1 3
2 4
3 5
1 2
1 3
3 4
2
Hint
For of the test cases, .
For all test cases, , , ,
. For any node, the weights of its child nodes are all distinct.
Translated by ChatGPT 5