#include using namespace std; #define ALL(x) (x).begin(),(x).end() #define REP(i, n) for(ll i=0; i<(ll)(n); i++) #define PER(i, n) for(ll i=(ll)(n)-1; i>=0; i--) template int LB(const vector& v, T x) { return lower_bound(ALL(v),x)-v.begin(); } template int UQ(T& v) { sort(ALL(v)); v.erase(unique(ALL(v)),v.end()); return v.size(); } template bool chmax(T &a, T b) { return a bool chmin(T &a, T b) { return a>b ? a=b, true : false; } template using rpriority_queue = priority_queue,greater>; using ll=long long; const int INF=1e9+10; const ll INFL=4e18; using ld=long double; using lll=__int128_t; using ull=unsigned long long; using VI=vector; using VVI=vector; using VL=vector; using VVL=vector; using PL=pair; using VP=vector; using WG=vector>>; #ifdef LOCAL #include "./debug.hpp" #else #define debug(...) #define print_line #endif template class MinOp{ public: T operator () ( T a , T b ){ return min( a , b ); } }; // sparse table // static range semigroup query // time complexity: // OpFunc is binary operator: T x T -> T template struct SparseTable{ OpFunc op; int size; vector lg; vector > table; void init( const vector &array, OpFunc opfunc ){ int n = array.size(); op = opfunc; lg.assign( n + 1 , 0 ); for( int i = 1; i <= n; i++ ){ lg[i] = 31 - __builtin_clz( i ); } table.assign( lg[n] + 1, array ); for( int i = 1; i <= lg[n]; i++ ){ for( int j = 0; j < n; j++ ){ if( j + (1<<(i-1)) < n ){ table[i][j] = op( table[i-1][j] , table[i-1][j+(1<<(i-1))] ); } else { table[i][j] = table[i-1][j]; } } } } T query( int l , int r ){ assert( l < r ); return op( table[ lg[r-l] ][l], table[ lg[r-l] ][r-(1< struct PMORMQ{ vector a; SparseTable,MinOp > > sparse_table; vector > > lookup_table; vector block_type; int block_size, n_block; void init( const vector &array ){ a = array; int n = a.size(); block_size = max( 1, ( 31 - __builtin_clz( n ) ) / 2 ); while( n % block_size != 0 ){ a.push_back( a.back() + 1 ); n++; } n_block = n / block_size; vector > b( n_block, make_pair( INT_MAX, INT_MAX ) ); for( int i = 0; i < n; i++ ){ b[i/block_size] = min( b[i/block_size], make_pair( a[i], i ) ); } sparse_table.init( b, MinOp >() ); block_type.assign( n_block, 0 ); for( int i = 0; i < n_block; i++ ){ int cur = 0; for( int j = 0; j < block_size-1; j++ ){ int ind = i * block_size + j; if( a[ind] < a[ind+1] ){ cur |= 1 << j; } } block_type[i] = cur; } lookup_table.assign( 1 << (block_size-1), vector >( block_size, vector( block_size+1 ) ) ); for( int i = 0; i < (1<<(block_size-1)); i++ ){ for( int j = 0; j < block_size; j++ ){ int res = 0; int cur = 0; int pos = j; for( int k = j+1; k <= block_size; k++ ){ lookup_table[i][j][k] = pos; if( i & ( 1 << (k-1) ) ){ cur++; } else { cur--; } if( res > cur ){ pos = k; res = cur; } } } } } int query( int l, int r ){ // return position assert( l < r ); int lb = l / block_size; int rb = r / block_size; if( lb == rb ){ return lb * block_size + lookup_table[ block_type[lb] ][ l % block_size ][ r % block_size ]; } int pl = lb * block_size + lookup_table[ block_type[lb] ][ l % block_size ][ block_size ]; int pr = rb * block_size + lookup_table[ block_type[rb] ][0][ r % block_size ]; int pos = pl; if( r % block_size > 0 && a[pl] > a[pr] ){ pos = pr; } if( lb + 1 == rb ){ return pos; } int sp = sparse_table.query( lb+1, rb ).second; if( a[pos] > a[sp] ){ return sp; } return pos; } }; // LCA // time complexity: struct LCA{ int n; vector > G; PMORMQ rmq; int cnt; vector depth, id, in; void init( int size ){ n = size; G.assign( n, vector( 0 ) ); } void add_edge( int a , int b ){ G[a].push_back( b ); G[b].push_back( a ); } void dfs( int x , int p , int d ){ id[cnt] = x; depth.push_back( d ); in[x] = cnt++; for( int v : G[x] ){ if( v == p ){ continue; } dfs( v , x , d+1 ); id[cnt] = x; depth.push_back( d ); cnt++; } } void precalc( int root ){ cnt = 0; depth.clear(); in.assign( n, -1 ); id.assign( 2*n-1, -1 ); dfs( root, -1, 0 ); rmq.init( depth ); } int lca( int a , int b ){ int x = in[a]; int y = in[b]; if( x > y ){ swap( x , y ); } int pos = rmq.query( x, y + 1 ); return id[pos]; } }; /// @brief 重みなしグラフ g の頂点 start からの最短距離を求める /// @note O(E+V) vector BFS(const vector>& g, int start=0) { int n=g.size(); vector ret(n,INF); ret[start]=0; queue que; que.push(start); while(!que.empty()) { int now=que.front();que.pop(); for(int nxt:g[now]) if(chmin(ret[nxt],ret[now]+1)) que.push(nxt); } return ret; } //---------------------------------------------------------- void solve() { int N; cin>>N; VVI G(N), H(N); REP(i,N-1) { int u,v; cin>>u>>v; u--; v--; G[u].push_back(v); G[v].push_back(u); } REP(i,N-1) { int u,v; cin>>u>>v; u--; v--; H[u].push_back(v); H[v].push_back(u); } auto d1=BFS(G), d2=BFS(H); LCA l1, l2; l1.init(N); l2.init(N); REP(i,N) for(int j: G[i]) if(i>T; while(T--) solve(); }