#include using namespace std; #define rep(i, a, b) for(int i=a; i pii; typedef pair pll; typedef vector vi; typedef vector vll; const int NMAX = 112345; const int LOGMAX = 25; int n,k; struct Nd *sta[NMAX]; struct Nd{ vector e; int h; bool is_here; bool is_upper; vector at_here; vector at_upper; int opt_solved; int opt_unsolved; Nd * up[LOGMAX]; void go(Nd * from=NULL, int hh=0) { h =hh; up[0]=from; fo(i,LOGMAX-1) up[i+1] = up[i]?up[i]->up[i]:0; for(Nd * x : e) if(x!=from) x->go(this, h+1); } void go2() { sta[h]=this; for(int x : at_here) sta[x]->is_here=true; for(int x : at_upper) sta[x]->is_upper=true; for(Nd * x : e) if(x!=up[0]) x->go2(); } void go3() { for(Nd * x : e) if(x!=up[0]) x->go3(); int with = 1; int without = is_here; for(Nd * x : e) if(x!=up[0]) with += x->opt_unsolved; for(Nd * x : e) if(x!=up[0]) without += x->opt_solved; opt_solved = min(with, is_upper?without+1:without); opt_unsolved = min(with, without); } }nds[NMAX]; Nd * nca(Nd *a, Nd *b) { if(b->h > a->h) return nca(b,a); for(int i=LOGMAX;i-->0;) if(a->up[i] && a->up[i]->h >= b->h) a = a->up[i]; for(int i=LOGMAX;i-->0;) if(a->up[i] != b->up[i]) a=a->up[i], b=b->up[i]; if(a==b) return a; return a->up[0]; } void add(Nd *a, Nd * b) { if(b->h > a->h) return add(b,a); Nd *c = nca(a,b); int l = a->h + b->h - 2*c->h; if(l%2) { a->at_upper.push_back(a->h-l/2); } else { a->at_here.push_back(a->h-l/2); } } int main() { scanf("%d%d", &n,&k); fo(i,n-1) { int a,b; scanf("%d%d",&a, &b); nds[a].e.push_back(nds+b); nds[b].e.push_back(nds+a); } nds->go(); fo(i,k) { int a,b; scanf("%d%d",&a, &b); add(nds+a, nds+b); } nds->go2(); nds->go3(); //fo(i,n) printf("%d: %d %d : %d %d\n", i, nds[i].is_here, nds[i].is_upper, nds[i].opt_solved, nds[i].opt_unsolved); printf("%d\n", nds->opt_solved); } /* 7 5 0 1 0 2 2 5 1 4 4 6 1 3 1 6 1 3 3 2 1 5 2 5 0: 0 0 : 3 3 1: 0 1 : 2 2 2: 0 1 : 1 1 3: 0 1 : 1 0 4: 1 0 : 1 1 5: 0 1 : 1 0 6: 0 0 : 0 0 3 */