//
// Created by yamunaku on 2019/10/22.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repl(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
#define CST(x) cout<<fixed<<setprecision(x)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> mti;
typedef vector<ll> vl;
typedef vector<vector<ll>> mtl;


mti e;
vi ec, ed;

struct UnionFind{
private:
    struct d{
        int num;
        int flag;
    };
    map<int, d> node;
    int ans = 0;

public:
    int root(int x){
        auto itr = node.find(x);
        if(itr == node.end()){
            node[x] = {-1, 0};
            return x;
        }else if(itr->second.num < 0){
            return x;
        }else{
            return itr->second.num = root(itr->second.num);
        }
    }

    void uni(int x, int y){
        x = root(x);
        y = root(y);
        if(x == y){
            if(node[x].flag == 0){
                ans++;
                node[x].flag = 1;
            }
            return;
        }
        ans++;
        if(node[x].flag == 1 && node[y].flag == 1) ans--;
        if(-node[x].num < -node[y].num) swap(x, y);
        node[x].num += node[y].num;
        if(node[y].flag == 1) node[x].flag = 1;
        node[y].num = x;
    }

    int solve(){
        return ans;
    }
};

struct data{
    vector<pair<int, int>> color;
    UnionFind uf;
};

vector<data *> dt;

int merge(int l, int r){
    if(dt[l]->color.size() < dt[r]->color.size()) swap(l, r);
    for(auto &p: dt[r]->color){
        dt[l]->uf.uni(p.first, p.second);
        dt[l]->color.push_back(p);
    }
    delete dt[r];
    return l;
}

vi ans;

int dfs(int x, int p){
    int nd = dt.size();
    dt.push_back(new data());
    for(auto ch:e[x]){
        if(ch == p) continue;
        nd = merge(nd, dfs(ch, x));
    }
    dt[nd]->color.emplace_back(ec[x], ed[x]);
    dt[nd]->uf.uni(ec[x], ed[x]);
    ans[x] = dt[nd]->uf.solve();
    return nd;
}


int main(){
    int n, k;
    cin >> n >> k;
    e = mti(n);
    ans = vi(n, -1);
    ec = vi(n), ed = vi(n);
    int u, v;
    rep(i, n - 1){
        cin >> u >> v;
        u--, v--;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    rep(i, n){
        cin >> ec[i] >> ed[i];
    }
    dfs(0, -1);
    rep(i, n){
        cout << ans[i] << endl;
    }
    return 0;
}

