以下是re代码
import java.io.*;
import java.util.*;
public class Main{
static int N = 60010,M = 2*N;
static int[] h = new int[N],e = new int[M],ne = new int[M];
static int[] depth = new int[N],sum = new int[N];
static int[][] fa = new int[N][30];
static int idx = 0,INF = 0x3f3f3f3f,n,m,max = 0;
static BufferedReader br
= new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer sc = new StreamTokenizer(br);
public static void main(String[] args)throws IOException{
n = nextRead();
m = nextRead();
Arrays.fill(h,-1);
for(int i = 0;i<n-1;i++){
int a = nextRead();
int b = nextRead();
add(a,b);
add(b,a);
}
bfs();
for(int i = 1;i<=m;i++){
int a = nextRead();
int b = nextRead();
int anc = lca(a,b);
sum[a]++;
sum[b]++;
sum[anc]--;
sum[fa[anc][0]]--;
}
dfs(1,0);
System.out.println(max);
}
public static void dfs(int u,int fa){
for(int i = h[u];i!=-1;i = ne[i]){
int j = e[i];
if(j == fa) continue;
dfs(j,u);
sum[u]+=sum[j];
}
max = Math.max(max,sum[u]);
}
public static int lca(int a,int b){
if(depth[a]<depth[b]){
int c = a;
a = b;
b = c;
}
for(int k = 19;k>=0;k--)
if(depth[fa[a][k]]>=depth[b])
a = fa[a][k];
if(a == b) return a;
for(int k = 19;k>=0;k--)
if(fa[a][k]!=fa[b][k]){
a = fa[a][k];
b = fa[b][k];
}
return fa[a][0];
}
public static void bfs(){
Arrays.fill(depth,INF);
depth[0] = 0;
depth[1] = 1;
Queue<Integer> q = new LinkedList<>();
q.add(1);
while(!q.isEmpty()){
int t = q.poll();
for(int i = h[t];i!=-1;i = ne[i]){
int j = e[i];
if(depth[j]>depth[t]+1){
depth[j] = depth[t]+1;
q.add(j);
fa[j][0] = t;
for(int k = 1;k<=19;k++)
fa[j][k] = fa[fa[j][k-1]][k-1];
}
}
}
}
public static void add(int a,int b){
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
public static int nextRead()throws IOException{
sc.nextToken();
return (int)sc.nval;
}
}