This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//  ~Be name khoda~  //
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second
#define cl       clear
#define endll    '\n'
const int maxn  =  1e6   + 10;
const int maxn5 =  2e3   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const ll  inf   =  2e18;
bool mark[maxn5];
int cnt[maxn5], a[maxn5], h[maxn5];
vector <int> adj[maxn5];
inline void dfs(int v){
	mark[v] = true;
	cnt[h[v]]++;
	for(auto u : adj[v]) if(!mark[u]){
		h[u] = h[v] + 1;
		dfs(u);
	}
	return;
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	
	int n, m; cin >> n >> m;
	for(int i = 0; i < n - 1; i++){
		int a, b; cin >> a >> b;
		a--; b--;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	for(int i = 0; i < n; i++)
		cin >> a[i];
	for(int i = 0; i < n; i++){
		h[i] = 0;
		memset(mark, false, sizeof mark);
		memset(cnt, 0, sizeof cnt);
		dfs(i);
		memset(mark, false, sizeof mark);
		int ans = 0;
		for(int j = 0; j < n; j++)
			if(i != j && cnt[h[j]] == 1){
				ans += (!mark[a[j]]);
				mark[a[j]] = true;
			}
		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... |