#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; void solve() { int n, m; cin >> n >> m; vector> vp(n - 1); for (auto& [u, v] : vp) cin >> u >> v, u--, v--; map, int> mp; rep(i, 0, m) { int u, v; cin >> u >> v; u--, v--; mp[{u, v}]++; } atcoder::dsu uf(n); vector> vv(n); rep(i, 0, n) vv[i] = {(int)i}; vector ans = {0}; int nw = 0; while (!vp.empty()) { auto [u, v] = vp.back(); vp.pop_back(); if (uf.same(u, v)) { ans.push_back(nw); continue; } u = uf.leader(u), v = uf.leader(v); for (int x : vv[u]) { for (int y : vv[v]) { auto itr = mp.find(minmax(x, y)); if (itr != mp.end()) { nw += itr->second; mp.erase(itr); } } } { int w = uf.merge(u, v); v = u ^ v ^ w; u = w; } if (vv[u].size() < vv[v].size()) swap(vv[u], vv[v]); vv[u].insert(vv[u].end(), vv[v].begin(), vv[v].end()); ans.push_back(nw); } ans.pop_back(); while (!ans.empty()) { cout << ans.back() << '\n'; ans.pop_back(); } } int main() { int t = 1; // cin >> t; while (t--) solve(); }