#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    if (!v.empty()) {
        os << v.front();
        for (int i = 1; i < v.size(); ++i) os << ' ' << v[i];
    }
    return os;
}

const int N = 200010;

int n;
int c[N], dis[N], prt[N];
pair<int, int> dep[N];
int cnt[N], seq[N], tag[N];
bool vis[N];
vector<int> g[N];

int cans;

void dfs(int u) {
    vis[u] = true;
    for (int v : g[u])
        if (!vis[v]) {
            dis[v] = dis[u] + 1;
            dfs(v);
        }
}

void addc(int x) {
    if (x == 0)
        return;
    if (!cnt[x]++)
        ++cans;
}

void ersc(int x) {
    if (x == 0)
        return;
    if (!--cnt[x])
        --cans;
}

void ins(int k, int x) {
    seq[k] = x;
    if (!tag[k])
        addc(x);
}

void ers(int k) {
    if (!tag[k])
        ersc(seq[k]);
    seq[k] = 0;
}

void add(int l, int r) {
    l = max(l, 0);
    for (int i = l; i <= r; ++i)
        if (!tag[i]++)
            ersc(seq[i]);
}

void sub(int l, int r) {
    l = max(l, 0);
    for (int i = l; i <= r; ++i)
        if (!--tag[i])
            addc(seq[i]);
}

struct DepthDecomposition {
    int dis[N], ans[N];

    void dfs2(int u) {
        dep[u] = make_pair(0, u);
        vis[u] = true;
        for (int v : g[u])
            if (!vis[v]) {
                dis[v] = dis[u] + 1;
                prt[v] = u;
                dfs2(v);
                dep[u] = max(dep[u], make_pair(dep[v].first + 1, dep[v].second));
            }
    }

    void dfs3(int u) {
        vector<int> chain;
        for (int v = dep[u].second; v != prt[u]; v = prt[v]) chain.push_back(v);
        reverse(chain.begin(), chain.end());
        add(dis[u] - dep[u].first - 2, dis[u] - 1);
        for (int v : chain) {
            sub(dis[v] - dep[v].first - 2, dis[v] - dep[v].first - 1);

            ans[v] = cans;
            ins(dis[v], c[v]);
            for (int w : g[v])
                if (prt[w] == v && dep[w].second != dep[v].second)
                    dfs3(w);
            for (int w : g[v])
                if (prt[w] == v && dep[w].second != dep[v].second)
                    add(dis[v] - dep[w].first - 1, dis[v] - 1);
            add(dis[v], dis[v]);
        }
        sub(dis[u] + dep[u].first, dis[u] + dep[u].first);
        for (int v : chain) {
            for (int w : g[v])
                if (prt[w] == v && dep[w].second != dep[v].second)
                    sub(dis[v] - dep[w].first - 1, dis[v] - 1);
            ers(dis[v]);
        }
    }

    void run(int s) {
        memset(vis, 0, sizeof(vis));
        prt[s] = 0;
        dfs2(s);
        dfs3(s);
    }

} decompS, decompT;

int main() {
#ifdef LBT
    freopen("test.in", "r", stdin);
    int nol_cl = clock();
#endif

    int m;
    scanf("%d%d", &n, &m);
    for (int rep = 1; rep < n; ++rep) {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= n; ++i) cin >> c[i];
    dfs(1);
    int s = max_element(dis + 1, dis + n + 1) - dis;
    memset(vis, 0, sizeof(vis));
    dfs(s);
    int t = max_element(dis + 1, dis + n + 1) - dis;
    decompS.run(s);
    decompT.run(t);
    for (int i = 1; i <= n; ++i)
        printf("%d\n", (decompS.dis[i] > decompT.dis[i] ? decompS.ans[i] : decompT.ans[i]));

#ifdef LBT
    LOG("Time: %dms\n", int((clock() - nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
    return 0;
}


Measured memory_bytes: 32864