#include using namespace std; #define For(i, a, n) for(int i =a;i<(int)n;i++) #define endl '\n' #define vec vector typedef long long ll; typedef pair pii; template void dbg(Args&&... args){ ((cerr< pair operator+(const pair&a, const pair& b){ return {a.first +b.first, a.second + b.second}; } template ostream& operator<< (ostream& os, const pair& a){ return os<<"("< basic_ostream& operator<<(basic_ostream& os, const C&c){ for(auto it=begin(c); it!=end(c);++it){ os<<(it==begin(c)?"":" ")<<*it; } return os; } vec z, k; vec> g; ll current_time = 0; vecp; void dfs1(int v, int parr=-1){ z[v] = current_time; p[v] = parr; for(auto i: g[v]){ if(i==parr)continue; current_time ++; dfs1(i, v); } current_time++; k[v] = current_time; } bool otec(int o, int v){ if(o == -1)return true; if(z[o]<=z[v]&& k[v]<=k[o]){ return true; } return false; } vec> jumps; vec dist(int a, int b){ ll za = a, zb = b; ll ans = 0; ll da = 0; for(int k = jumps.size()-1;k>=0;k--){ if(!otec(jumps[k][a].first, b)){ a = jumps[k][a].first; da += jumps[k][a].second; } } ll db = 0; for(int k = jumps.size()-1;k>=0;k--){ if(!otec(jumps[k][b].first, a)){ b = jumps[k][b].first; db += jumps[k][b].second; } } if(!otec(a, b))da+= 1; if(!otec(b, a))db += 1; ans = da + db; dbg("distance", za, zb,"is", ans); return {ans, da, db}; } vec mid(int a, int b){ auto ddr = dist(a, b); int d=ddr[0], da=ddr[1], db=ddr[2]; int m = (d)/2; ll za =a, zb =b; // dbg(d, m, da, db); vec ans; if(d==1)return {za, zb}; if(da > db){ ll aktd = 0; for(int k = jumps.size()-1;k>=0;k--){ if(jumps[k][a].first==-1)continue; // dbg("ajump", k, a, aktd, jumps[k][a]); if(aktd + jumps[k][a].second < m){ aktd+=jumps[k][a].second; a=jumps[k][a].first; } } a = p[a]; if(d%2)ans = {a, p[a]}; else ans= {a}; } else { ll aktd = 0; for(int k = jumps.size()-1;k>=0;k--){ if(jumps[k][b].first==-1)continue; // dbg("bjump", k, a, aktd, jumps[k][a]); if(aktd + jumps[k][b].second < m){ aktd+=jumps[k][b].second; b=jumps[k][b].first; } } b = p[b]; if(d%2)ans= {b, p[b]}; else ans= {b}; } // else{ // ll aktd = 0; // for(int k = jumps.size()-1;k>=0;k--){ // if(jumps[k][b].first==-1)continue; // dbg("eaual jump", k, b, aktd, jumps[k][b]); // if(aktd + jumps[k][b].second < m){ // aktd+=jumps[k][b].second; // b=jumps[k][b].first; // } // } // b = p[b]; // ans= {b}; // } dbg("mid of", za, zb, "is", ans); return ans; } vec macka; set s; vec> memo; #define inf 123466893 int f(int v, bool m){ if(memo[v][m]!=-1)return memo[v][m]; int t; if(m){ t = 1; for(auto i: g[v]){ if(i== p[v])continue; t += min(f(i, 0), f(i, 1)); } } else{ t = 0; if(macka[v])return inf; for(auto i: g[v]){ if(i== p[v])continue; t += min(f(i, s.count({min(i, v), max(i, v)})), f(i, 1)); } } return memo[v][m] = t; } void solve(){ int n, q;cin>>n>>q; g.resize(n); p.resize(n); z.resize(n); k.resize(n); For(i, 0, n-1){ int a, b;cin>>a>>b; g[a].push_back(b); g[b].push_back(a); } dfs1(0); jumps.push_back({}); For(i, 0, n){ jumps[0].push_back({i, 0}); } jumps.push_back({}); for(auto i:p){ jumps[1].push_back({i, 1}); } for(int k = 2;;k++){ vec nj(n, {-1, -1}); bool ok = false; For(i, 0, n){ pii o = jumps[k-1][i]; if(o.first==-1)continue; nj[i] = jumps[k-1][o.first]; nj[i].second+= o.second; ok = true; } jumps.push_back(nj); if(!ok)break; dbg(nj); } macka.resize(n); For(i, 0, q){ int a, b;cin>>a>>b; auto m = mid(a, b); dbg(m); if(m.size()==1)macka[m[0]]=true; else{ s.insert({min(m[0], m[1]), max(m[1], m[0])}); } } memo.resize(n, vec(2, -1)); cout<< min(f(0, 0), f(0, 1))<sync_with_stdio(0); cin.exceptions(cin.failbit); int t=1;//cin>>t; while(t--){ solve(); } }