# problem

# solution

Consider one ( u , v ) (u,v) The red edge of (u,v) is on the blue tree u , v u,v u. V distance between two points ≥ 3 \ge 3 ≥3.

If you arrive first u , v u,v u. V at any of these points, if the second hand action in the next step cannot grasp the first hand, the game will enter a dead cycle.

Through drawing, you will find that this conclusion is tenable. Once the second hand approaches the first hand, the first hand will immediately go to another point on the side and pull away from the second hand again. (fishing system)

This is the favorable conclusion of our judgment - 1. (mark these points)

The first player wants to maximize the number of rounds, in other words, the first player will try to walk some red edges to the mark point.

And these can go ( u , v ) (u,v) (u,v) are naturally satisfied with the blue tree u , v u,v u. Distance of V ≤ 2 \le 2 ≤2.

We put the blue tree y y y is the root, so the starting point of the first hand x x x is inside.

For the backhand, it must be close to the subtree where the forehand is located, and the forehand must not cross the backhand to the top.

The most extreme is that the first hand points on the blue tree. The second hand points the son. The first hand can only jump up two steps at most, and then become the second hand. The father will be caught in the next step. It's better not to jump up at first.

Therefore, it is impossible for the first hand to step over the second hand. Once you step over the next step, you will be caught.

Based on this, we can launch if a point p p p on mangroves with x x Distance of x ≥ \ge ≥ p p p in the blue tree with y y y distance, then the second hand will come first p p p on the way or p p Grasp it at point p.

Therefore, it is absolutely impossible to go first. To go is to die. Then the only way to walk is to walk with me on the mangrove x x Distance of x < < < on the blue tree with y y The point of the distance of y.

So we can dfs \text{dfs} dfs, through the edge that can be walked to the point that meets the limit.

If one of these points is marked, you can enter an infinite loop.

Otherwise, the biggest answer must be to point on the mangrove and x x Maximum distance of x × 2 \times 2 × 2. When you arrive, stand in place and wait to catch.

# code

#include <bits/stdc++.h> using namespace std; #define maxn 200005 int n, x, y, ans; vector < int > R[maxn], B[maxn]; pair < int, int > E[maxn]; int dis_b[maxn], dis_r[maxn], f[maxn]; bool vis[maxn]; void dfs1( int u, int fa ) { f[u] = fa; for( int v : B[u] ) if( v ^ fa ) { dis_b[v] = dis_b[u] + 1; dfs1( v, u ); } } bool check( int u, int v ) { if( dis_b[u] < dis_b[v] ) swap( u, v ); if( dis_b[u] == dis_b[v] ) return f[u] ^ f[v]; if( dis_b[u] == dis_b[v] + 1 ) return f[u] ^ v; if( dis_b[u] == dis_b[v] + 2 ) return f[f[u]] ^ v; return 1; } void dfs2( int u, int fa ) { if( vis[u] ) { puts("-1"); exit(0); } ans = max( ans, dis_b[u] ); for( int v : R[u] ) if( v ^ fa ) { dis_r[v] = dis_r[u] + 1; if( dis_r[v] < dis_b[v] ) dfs2( v, u ); } } int main() { scanf( "%d %d %d", &n, &x, &y ); for( int i = 1, u, v;i < n;i ++ ) { scanf( "%d %d", &u, &v ); R[u].push_back( v ); R[v].push_back( u ); E[i] = make_pair( u, v ); } for( int i = 1, u, v;i < n;i ++ ) { scanf( "%d %d", &u, &v ); B[u].push_back( v ); B[v].push_back( u ); } dfs1( y, 0 ); for( int i = 1;i < n;i ++ ) { int u = E[i].first, v = E[i].second; if( check( u, v ) ) vis[u] = vis[v] = 1; //Judge whether u and V are infinite cyclic points } dfs2( x, 0 ); printf( "%d\n", ans << 1 ); return 0; }