1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<ctype.h> #define R register using namespace std; inline int read() { int x=0,w=0;char c=getchar(); while(!isdigit(c))w|=c=='-',c=getchar(); while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar(); return w?-x:x; } const int maxn=233333; int n,m,ans[maxn]; int top[maxn],fa[maxn][18],dep[maxn],head[maxn],ecnt,cnt; struct Edge{ int to,nxt; }e[maxn<<1]; inline void addedge(int a,int b) { e[++ecnt]=(Edge){b,head[a]};head[a]=ecnt; e[++ecnt]=(Edge){a,head[b]};head[b]=ecnt; } bool vis[maxn]; void dfs1(int x,int depth) { for (R int i=0;fa[x][i];++i) fa[x][i+1]=fa[fa[x][i]][i]; vis[x]=1; dep[x]=depth; for(R int i=head[x];i;i=e[i].nxt) { int u=e[i].to; if(vis[u])continue; fa[u][0]=x; dfs1(u,depth+1); } } inline int LCA(int u,int v) { if(dep[u]<dep[v])swap(u,v); for(R int i=0;i<=16;i++) if((dep[u]-dep[v])&(1<<i))u=fa[u][i]; if(u==v)return u; for(R int i=16;i>=0;i--) if(fa[u][i]!=fa[v][i])u=fa[u][i],v=fa[v][i]; return fa[u][0]; } int Ans=0; void dfs2(int x) { vis[x]=1; for(R int i=head[x];i;i=e[i].nxt) { int u=e[i].to; if(vis[u])continue; dfs2(u); ans[x]+=ans[u]; } Ans=max(Ans,ans[x]); } int main() { n=read(),m=read(); for(R int i=1;i<n;i++)addedge(read(),read()); fa[1][0]=0; dfs1(1,1); for(R int i=1;i<=m;i++) { int x=read(),y=read(); int lca=LCA(x,y); ans[x]++,ans[y]++,ans[lca]--,ans[fa[lca][0]]--; } memset(vis,0,sizeof vis); dfs2(1); printf("%d\n",Ans); return 0; }
|