#include <bits/stdc++.h>
using namespace std;
#ifndef lisie_bimbi
#define endl '\n'
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,bmi2,fma")
#endif
using ll = long long;
const ll inf = 1'000'000'000;
vector<vector<int>> v;
vector<int> c;
pair<int, int> furthest(int u, int par){
int mx = 0, z = u;
for(auto i : v[u]){
if(i != par){
auto [mx2, z2] = furthest(i, u);
if(mx2 + 1 > mx){
mx = mx2 + 1;
z = z2;
}
}
}
return {mx, z};
}
void calcdist(int u, int par, vector<int> &d){
if(par == -1){
d[u] = 0;
}else{
d[u] = d[par] + 1;
}
for(auto i : v[u]){
if(i != par){
calcdist(i, u, d);
}
}
}
void calcleaf(int u, int par, vector<int> &d){
for(auto i : v[u]){
if(i != par){
calcleaf(i, u, d);
d[u] = max(d[u], d[i] + 1);
}
}
}
int n;
void calcans(int u, int par, int level, int NOW, vector<int> &d, vector<int> &ans){
if(NOW + d[u] < level){
ans[u] = 1;
}
int mx1 = 0, mx2 = 0;
for(auto i : v[u]){
if(i != par){
if(d[i] + 1 > mx1){
mx2 = mx1;
mx1 = d[i] + 1;
}else if(d[i] + 1 > mx2){
mx2 = d[i] + 1;
}
}
}
// cout << u << ' ' << NOW << ' ' << d[u] << endl;
// cout << mx1 << ' ' << mx2 << endl;
for(auto i : v[u]){
if(i != par){
if(d[i] + 1 != mx1){
if(NOW + mx1 < level){
calcans(i, u, level + 1, NOW, d, ans);
}else{
calcans(i, u, level + 1, level, d, ans);
}
}else{
if(NOW + mx2 < level){
calcans(i, u, level + 1, NOW, d, ans);
}else{
calcans(i, u, level + 1, level, d, ans);
}
}
}
}
}
vector<int> calc(int s){
vector<int> a(n);
calcleaf(s, -1, a);
vector<int> ans(n);
// cout << "aaaaaaaaaaaaaaaaaaaaa\n";
calcans(s, -1, 0, 0, a, ans);
return ans;
}
void solve(){
int m;
cin >> n >> m;
v.resize(n);
for(int i = 0; i < n - 1; i++){
int x, y;
cin >> x >> y;
x--;y--;
v[x].push_back(y);
v[y].push_back(x);
}
c.resize(n);
for(int i = 0; i < n; i++){
cin >> c[i];
}
if(n <= 2000){
for(int i = 0; i < n; i++){
vector<int> d(n);
calcdist(i, -1, d);
vector<vector<int>> z(n);
for(int j = 0; j < n; j++){
z[d[j]].push_back(c[j]);
}
set<int> cc;
for(int zz = 1; zz < n; zz++){
auto j = z[zz];
if(j.size() == 1){
cc.insert(j[0]);
}
}
cout << cc.size() << endl;
}
return;
}
auto [_, s1] = furthest(0, -1);
auto [__, s2] = furthest(s1, -1);
vector<int> d1(n), d2(n);
calcdist(s1, -1, d1);
calcdist(s2, -1, d2);
auto ans1 = calc(s1);
auto ans2 = calc(s2);
// cout << s1 << ' ' << s2 << endl;
// for(auto i : d1){
// cout << i << ' ';
// }
// cout << endl;
// for(auto i : d2){
// cout << i << ' ';
// }
// cout << endl;
// for(auto i : ans1){
// cout << i << ' ';
// }
// cout << endl;
// for(auto i : ans2){
// cout << i << ' ';
// }
// cout << endl;
for(int i = 0; i < n; i++){
if(d1[i] > d2[i]){
cout << ans1[i] << endl;
}else{
cout << ans2[i] << endl;
}
}
}
signed main(){
#ifdef lisie_bimbi
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
#endif
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
//cin >> t;
while(t--){
solve();
}
}
| # | 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... |