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;
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 |
---|
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... |