# https://betrue12.hateblo.jp/entry/2019/08/14/152227 # のコードを改造 import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline N,M=map(int,input().split()) U=[tuple(map(int,input().split())) for i in range(N-1)] U.reverse() Queries=[tuple(map(int,input().split())) for i in range(M)] # UnionFind Group = [i for i in range(N+5)] # グループ分け Nodes = [1]*(N+5) # 各グループのノードの数 def find(x): while Group[x] != x: x=Group[x] return x def Union(x,y): if find(x) != find(y): if Nodes[find(x)] < Nodes[find(y)]: Nodes[find(y)] += Nodes[find(x)] Nodes[find(x)] = 0 Group[find(x)] = find(y) else: Nodes[find(x)] += Nodes[find(y)] Nodes[find(y)] = 0 Group[find(y)] = find(x) OK=[N-1]*M NG=[-1]*M for tests in range(13): Group = [i for i in range(N+5)] # グループ分け Nodes = [1]*(N+5) # 各グループのノードの数 K=[[] for i in range(N+5)] for i in range(M): if OK[i]==-1: continue K[(OK[i]+NG[i])//2].append(i) for i in range(N-1): x,y=U[i] Union(x,y) for ind in K[i]: s,t=Queries[ind] if find(s)==find(t): OK[ind]=i else: NG[ind]=i OK.sort(reverse=True) ANS=[] now=M ind=0 for i in range(N-2,-1,-1): while ind