#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using treap = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define REP(i, n) for (long long i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (long long i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) begin(obj), end(obj)
#define RALL(obj) rbegin(obj), rend(obj)
#define fi first
#define se second
using ii = pair<int, int>;
vector<ii> dirs = {
    {1, 0}, {0, 1}, {-1, 0}, {0, -1},  // 4方向
    {1, 1}, {-1, 1}, {-1, -1}, {1, -1},  // 斜め
    {0, 0},  // 自身
};
template <class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }
template <class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template <class T, class S> vector<T> make_vec(size_t n, S x) { return vector<T>(n, x); }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...)); }

// debug
template <class T> ostream& operator<<(ostream& s, vector<T>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " "); return s; }
template <class T> ostream& operator<<(ostream& s, vector<vector<T>>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : "\n"); return s; }
template <class T, class S> ostream& operator<<(ostream& s, pair<T, S>& p) { s << "{" << p.first << ", " << p.second << "}"; return s; }
template <class T> ostream& operator<<(ostream& s, set<T> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; }
template <class T> ostream& operator<<(ostream& s, multiset<T> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; }
template <class T, class S> ostream& operator<<(ostream& s, map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; }
template <class T, class S> ostream& operator<<(ostream& s, unordered_map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; }
#ifdef _MY_DEBUG
    #define dump(...) cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl, dump_func(__VA_ARGS__), cerr << "*/\n\n";
#else
    #define dump(...)
    #define endl "\n"
#endif
void dump_func() { cerr << endl; }
template <class Head, class... Tail> void dump_func(Head&& h, Tail&&... t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); }

struct Fast { Fast() { cin.tie(nullptr); ios::sync_with_stdio(false); } } fast;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
constexpr int MOD = 1000000007;
// *************** TEMPLATE END *************** 

struct DynamicUnionFind {
    int ans = 0;
    unordered_map<int, int> number;
    // 頂点xが入っていなかったら追加
    int get(int x) {
        return number.count(x) ? number[x] : number[x] = -1;
    }
    struct Data {
        int e = 0;
        Data() {}
        void merge(Data& s) {
            // 自分で定義
            e += s.e;
        }
    };
    unordered_map<int, Data> data;
    vector<ii> edges;
    int score(int x) {
        x = root(x);
        return min(data[x].e, size(x));
    }

    int root(int x) {
        return get(x) < 0 ? x : number[x] = root(number[x]);
    }
    void unite(int x, int y) {
        edges.emplace_back(x, y);
        x = root(x);
        y = root(y);
        if (x == y) {
            ans -= score(x);
            data[x].e += 1;
            ans += score(x);
            return;
        }
        if (number[y] < number[x]) swap(x, y);
        ans -= score(x);
        ans -= score(y);
        // yをxにマージ
        number[x] += number[y];
        number[y] = x;
        // dataのマージ方法に合わせて変える
        data[x].merge(data[y]);
        data[x].e += 1;
        ans += score(x);
    }
    bool is_same(int x, int y) { return root(x) == root(y); }
    int size(int x) { return -number[root(x)]; }
    Data& ref(int x) { return data[root(x)]; }
};

signed main() {
    int n, K; cin >> n >> K;
    vector<vector<int>> g(n);
    REP (i, n - 1) {
        int u, v; cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<int> c(n), d(n);
    REP (i, n) cin >> c[i] >> d[i], c[i]--, d[i]--;
    
    vector<DynamicUnionFind> ufs(n);
    vector<int> ans(n);
    function<void(int, int)> dfs = [&](int v, int prev) {
        for (int s : g[v]) if (s != prev) {
            dfs(s, v);
            if (ufs[s].edges.size() > ufs[v].edges.size()) swap(ufs[s], ufs[v]);
            // for (auto& p : ufs[s].edges) {
            //     ufs[v].unite(p.fi, p.se);
            // }
            while (ufs[s].edges.size()) {
                ufs[v].unite(ufs[s].edges.back().fi, ufs[s].edges.back().se);
                ufs[s].edges.pop_back();
            }
            ufs[s] = DynamicUnionFind();
        }
        ufs[v].unite(c[v], d[v]);
        ans[v] = ufs[v].ans;
    };
    dfs(0, -1);
    for (int x : ans) cout << x << endl;
}




Measured memory_bytes: 96452