#include<bits/stdc++.h>
using namespace std;
bool M1;
#define PI 3.14159265358979323846
#define sz(a) (int)a.size()
#define all(x) x.begin(),x.end()
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii,ii>
#define se second
#define fi first
#define ffi fi.fi
#define sfi se.fi
#define sse se.se
#define fse fi.se
#define lt(i, c, d) for(int i = c; i <= d; ++i)
#define fl(i, c, d) for(int i = d; i >= c; --i)
#define pb push_back
#define emb emplace_back
#define emf emplace_front
#define em emplace
#define look_memory cerr<<abs(&M2-&M1)/1024.0/1024<<'\n'
#define look_time cerr << "TIME : " << clock() * 0.001 << "s" <<'\n'
const int N=1e6+5,lg=30,mod=1e9+7;
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int u,int v){
return u+rd()%(v-u+1);
}
int dx[]={1,0,-1,0,1,1,-1,-1};
int dy[]={0,-1,0,1,1,-1,1,-1};
int h[N],node,m,S,T,c[N],dp[N],ans[N];
vector<int>ver[N];
void dfs(int u,int cha){
dp[u]=0;
for(auto v:ver[u]){
if(v==cha)continue;
h[v]=h[u]+1;
dfs(v,u);
dp[u]=max(dp[u],dp[v]+1);
}
}
void find(){
dfs(1,-1);
S=T=1;
for(int i=1;i<=node;++i){
if(h[S]<h[i])S=i;
}
h[S]=0;
dfs(S,-1);
for(int i=1;i<=node;++i){
if(h[T]<h[i])T=i;
}
}
void cal(int u,int cha,int mn){
if(h[u]>mn+dp[u])ans[u]=1;
vector<int>tab;
tab.emb(-1);
for(auto v:ver[u]){
if(v==cha)continue;
tab.emb(dp[v]);
}
sort(all(tab));
for(auto v:ver[u]){
if(v==cha)continue;
if(tab.back()==dp[v]){
if(h[u]-tab[sz(tab)-2]-1<=mn)cal(v,u,h[u]);
else cal(v,u,mn);
}
else{
if(h[u]-tab.back()-1<=mn)cal(v,u,h[u]);
else cal(v,u,mn);
}
}
}
bool M2;
void solve(){
cin >> node >> m;
for(int i=1;i<node;++i){
int u,v;
cin >> u >> v;
ver[u].emb(v);
ver[v].emb(u);
}
for(int i=1;i<=node;++i)cin >> c[i];
find();
// cout <<S<<" "<<T<<'\n';
h[S]=0;
dfs(S,-1);
cal(S,-1,0);
h[T]=0;
dfs(T,-1);
cal(T,-1,0);
for(int i=1;i<=node;++i)cout << ans[i]<<"\n";
}
main()
{
srand(time(0));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#define task "aws"
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int t=1;
// cin >> t;
while(t--){
solve();
}
look_memory;
look_time;
}
Compilation message (stderr)
joi2019_ho_t5.cpp:95:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
95 | main()
| ^~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:103:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:104:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |