#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define N 400000 int n,m,x,y,ans,tot; int fa[N],deep[N],size[N],head[N],top[N],cnt[N]; struct edge{int v,nxt;}e[N]; void add(int x,int y){e[++tot].v=y; e[tot].nxt=head[x]; head[x]=tot;} void dfs1(int u){ if (!fa[u]) fa[u]=u; deep[u]=deep[fa[u]]+1; size[u]=1; for (int i=head[u];i;i=e[i].nxt){ int v=e[i].v; if (fa[u]!=v){fa[v]=u; dfs1(v); size[u]+=size[v];} } } void dfs2(int u){ if (!top[u]) top[u]=u; int t=0,v; for (int i=head[u];i;i=e[i].nxt){v=e[i].v; t=(v!=fa[u] && size[v]>size[t])?v:t;} if (t==0) return; top[t]=top[u]; dfs2(t); for (int i=head[u];i;i=e[i].nxt){v=e[i].v; if (v!=fa[u] && v!=t) dfs2(v);} } int lca(int u,int v){ for (;top[u]!=top[v];u=fa[top[u]]) if (deep[top[u]]<deep[top[v]]) swap(u,v); return deep[u]<deep[v]?u:v; } void count(int u){ for (int i=head[u];i;i=e[i].nxt){ int v=e[i].v; if (v!=fa[u]){count(v); cnt[u]+=cnt[v];} } } int main(){ scanf("%d%d",&n,&m); for (int i=1;i<n;i++){scanf("%d%d",&x,&y); add(x,y); add(y,x);} dfs1(1); dfs2(1); for (int i=1;i<=m;i++){ scanf("%d%d",&x,&y); cnt[x]++; cnt[y]++; cnt[lca(x,y)]-=2; } count(1); for (int i=2;i<=n;i++){ if (cnt[i]==0) ans+=m; if (cnt[i]==1) ans++; } printf("%d\n",ans); return 0; }
|