#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
ll n,m;
cin >> n >> m;
vector<vector<ll>> con(n);
vector<ll> c(n);
for(ll i = 1;i<n;i++){
ll u,v;
cin >> u >> v;
con[u-1].push_back(v-1);
con[v-1].push_back(u-1);
}
for(ll &i : c) cin >> i;
vector<ll> d(n,0),p(n,0);
{
function<void(ll)> dfs;
dfs = [&](ll i){
for(ll j : con[i]) if(j!=p[i]){
p[j] = i;
d[j] = d[i]+1;
dfs(j);
}
};
dfs(0);
}
vector<vector<ll>> pp(20,vector<ll>(n));
pp[0] = p;
for(ll i = 1;i<20;i++){
for(ll j = 0;j<n;j++){
pp[i][j] = pp[i-1][pp[i-1][j]];
}
}
function<ll(ll,ll)> dis = [&](ll a, ll b){
if (d[a]<d[b]) swap(a,b);
//cerr << "dis " << a << " " << b << endl;
ll da = d[a];
ll db = d[b];
ll x = da-db;
while(x){
a = pp[__builtin_ctzll(x)][a];
x -= x&-x;
}
//cerr << "dis " << a << " " << b << endl;
for(ll j = 19;j>=0;j--){
if (pp[j][a]!=pp[j][b]){
a = pp[j][a];
b = pp[j][b];
}
}
if(a!=b) a = p[a];
return da+db-d[a]*2;
};//cerr << "cp1\n";
for(ll i = 0;i<n;i++){
map<ll,ll> cnt;
for(ll j = 0;j<n;j++) if(i!=j){
cnt[dis(i,j)]++;
}
set<ll> s;
for(ll j = 0;j<n;j++) if(i!=j){
if(cnt[dis(i,j)]==1){
s.insert(c[j]);
}
}
cout << s.size() << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
835 ms |
912 KB |
Output is correct |
3 |
Correct |
319 ms |
808 KB |
Output is correct |
4 |
Correct |
1139 ms |
1240 KB |
Output is correct |
5 |
Correct |
985 ms |
1108 KB |
Output is correct |
6 |
Correct |
1733 ms |
1148 KB |
Output is correct |
7 |
Correct |
1416 ms |
1292 KB |
Output is correct |
8 |
Correct |
677 ms |
860 KB |
Output is correct |
9 |
Correct |
1120 ms |
972 KB |
Output is correct |
10 |
Correct |
1018 ms |
860 KB |
Output is correct |
11 |
Correct |
1022 ms |
964 KB |
Output is correct |
12 |
Correct |
395 ms |
964 KB |
Output is correct |
13 |
Correct |
1690 ms |
1136 KB |
Output is correct |
14 |
Correct |
1130 ms |
1024 KB |
Output is correct |
15 |
Correct |
1227 ms |
1008 KB |
Output is correct |
16 |
Correct |
308 ms |
956 KB |
Output is correct |
17 |
Correct |
1090 ms |
1128 KB |
Output is correct |
18 |
Correct |
1260 ms |
1044 KB |
Output is correct |
19 |
Correct |
944 ms |
968 KB |
Output is correct |
20 |
Correct |
1849 ms |
1360 KB |
Output is correct |
21 |
Correct |
1411 ms |
1056 KB |
Output is correct |
22 |
Correct |
687 ms |
876 KB |
Output is correct |
23 |
Correct |
1128 ms |
972 KB |
Output is correct |
24 |
Correct |
996 ms |
1108 KB |
Output is correct |
25 |
Correct |
1053 ms |
968 KB |
Output is correct |
26 |
Correct |
391 ms |
972 KB |
Output is correct |
27 |
Correct |
1642 ms |
1172 KB |
Output is correct |
28 |
Correct |
1283 ms |
1132 KB |
Output is correct |
29 |
Correct |
1209 ms |
1036 KB |
Output is correct |
30 |
Correct |
290 ms |
860 KB |
Output is correct |
31 |
Correct |
1256 ms |
1360 KB |
Output is correct |
32 |
Correct |
1240 ms |
1064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2045 ms |
30548 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2041 ms |
42604 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
835 ms |
912 KB |
Output is correct |
3 |
Correct |
319 ms |
808 KB |
Output is correct |
4 |
Correct |
1139 ms |
1240 KB |
Output is correct |
5 |
Correct |
985 ms |
1108 KB |
Output is correct |
6 |
Correct |
1733 ms |
1148 KB |
Output is correct |
7 |
Correct |
1416 ms |
1292 KB |
Output is correct |
8 |
Correct |
677 ms |
860 KB |
Output is correct |
9 |
Correct |
1120 ms |
972 KB |
Output is correct |
10 |
Correct |
1018 ms |
860 KB |
Output is correct |
11 |
Correct |
1022 ms |
964 KB |
Output is correct |
12 |
Correct |
395 ms |
964 KB |
Output is correct |
13 |
Correct |
1690 ms |
1136 KB |
Output is correct |
14 |
Correct |
1130 ms |
1024 KB |
Output is correct |
15 |
Correct |
1227 ms |
1008 KB |
Output is correct |
16 |
Correct |
308 ms |
956 KB |
Output is correct |
17 |
Correct |
1090 ms |
1128 KB |
Output is correct |
18 |
Correct |
1260 ms |
1044 KB |
Output is correct |
19 |
Correct |
944 ms |
968 KB |
Output is correct |
20 |
Correct |
1849 ms |
1360 KB |
Output is correct |
21 |
Correct |
1411 ms |
1056 KB |
Output is correct |
22 |
Correct |
687 ms |
876 KB |
Output is correct |
23 |
Correct |
1128 ms |
972 KB |
Output is correct |
24 |
Correct |
996 ms |
1108 KB |
Output is correct |
25 |
Correct |
1053 ms |
968 KB |
Output is correct |
26 |
Correct |
391 ms |
972 KB |
Output is correct |
27 |
Correct |
1642 ms |
1172 KB |
Output is correct |
28 |
Correct |
1283 ms |
1132 KB |
Output is correct |
29 |
Correct |
1209 ms |
1036 KB |
Output is correct |
30 |
Correct |
290 ms |
860 KB |
Output is correct |
31 |
Correct |
1256 ms |
1360 KB |
Output is correct |
32 |
Correct |
1240 ms |
1064 KB |
Output is correct |
33 |
Execution timed out |
2045 ms |
30548 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |