This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n,m,k,x,y,z;
int c[2005], v[2005], d[2005], ok[2005];
vector<int> gph[2005];
void dfs(int id, int dis) {
    v[id] = 1;
    d[id] = dis;
    for (auto i : gph[id]) {
        if (!v[i]) dfs(i,dis+1);
    }
}
int main() {
    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
    cin >> n >> m;
    for (int i = 1; i < n; i++) {
        cin >> x >> y;
        gph[x].push_back(y);
        gph[y].push_back(x);
    }
    for (int i = 1; i <= n; i++) cin >> c[i];
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) v[j] = ok[j] = 0;
        dfs(i,0);
        pair<int,int> ar[2005];
        for (int j = 1; j <= n; j++) ar[j] = {d[j],j};
        sort(ar+1,ar+1+n);
        int ans = 0;
        for (int j = 1; j <= n; j++) if (ar[j].first != ar[j-1].first && ar[j].first != ar[j+1].first) {
            ok[c[ar[j].second]] = 1;
        }
        for (int j = 1; j <= n; j++) ans += ok[j];
        cout << ans << "\n";
    }
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |