#include <bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define pb push_back
#define fi first
#define se second
#define ins insert
#define pf push_front
#define bit(n,k) ((n>>k)&1)
#define pi acos(-1.0)
const ll N=1e5+5, inf = 1e18, mod=1e9+7;
using namespace std;
vector<ll>v[N];
set<ll>s[N];
ll col[N],idx[N],ans[N];
void dfs(ll u, ll par){
s[u].ins(col[u]);
for(ll x:v[u]){
if(x==par) continue;
dfs(x,u);
if(s[idx[x]].size()>s[idx[u]].size()){
for(ll z:s[idx[u]]) s[idx[x]].ins(z);
swap(idx[x],idx[u]);
}
else{
for(ll z:s[idx[x]]) s[idx[u]].ins(z);
}
}
ans[u]=s[idx[u]].size();
}
int main(){
// freopen("TRAVEL.INP", "r", stdin);
// freopen("TRAVEL.OUT", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n,m,root; cin >>n >> m >> root;
ll x,y;
for(ll i=1;i<=n-1;i++){
cin >> x >> y;
v[x].pb(y);
v[y].pb(x);
}
for(ll i=1;i<=n;i++) cin >> col[i],idx[i]=i;
dfs(root,0);
while(m--){
ll x; cin >> x;
cout << ans[x] << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
7512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
7512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
7512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |