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"
#define MAXN 200009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
vector<int>adj[MAXN];
int arr[MAXN],n,m,lvl[MAXN],height[MAXN];
void dfs0(int nd,int pr=0){
lvl[nd]=lvl[pr]+1;
for(auto to:adj[nd])
if(to!=pr)
dfs0(to,nd);
}
void dfs1(int nd,int pr=0){
height[nd]=0;
for(auto to:adj[nd])
if(to!=pr){
dfs1(to,nd);
umax(height[nd],height[to]+1);
}
}
bool cmp(int x,int y){
return (height[x]>height[y]);
}
int ans[MAXN],cur,cnt[MAXN];
vector<int>S;
void upd(int nd,int d){
while(!S.empty() and lvl[nd]-lvl[S.back()]<=d)
cur-=(--cnt[arr[S.back()]]==0),S.ppb();
}
void dfs2(int nd,int pr){
sort(all(adj[nd]),cmp);
if(adj[nd].size()>1){
if(adj[nd].size()>=3)
upd(nd,height[adj[nd][2]]+1);
S.pb(nd);
cur+=(cnt[arr[nd]]++==0);
dfs2(adj[nd][1],nd);
if(!S.empty() and S.back()==nd)
cur-=(--cnt[arr[nd]]==0),S.ppb();
upd(nd,height[adj[nd][1]]+1);
for(int i=2;i<int(adj[nd].size());i++){
int to=adj[nd][i];
S.pb(nd);
cur+=(cnt[arr[nd]]++==0);
dfs2(to,nd);
if(!S.empty() and S.back()==nd)
cur-=(--cnt[arr[nd]]==0),S.ppb();
}
}
umax(ans[nd],cur);
}
int main(){
//freopen("file.in", "r", stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
adj[u].pb(v);
adj[v].pb(u);
}
for(int i=1;i<=n;i++)
scanf("%d",arr+i);
dfs0(1);
for(int i=0;i<2;i++){
int mx=0,who=0;
for(int j=1;j<=n;j++)
if(umax(mx,lvl[j]))
who=j;
dfs1(who); dfs0(who); S.pb(who);
cur+=(cnt[arr[who]]++==0);
dfs2(adj[who][0],who);
}
for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
return 0;
}
Compilation message (stderr)
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp:77:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d",arr+i);
| ~~~~~^~~~~~~~~~~~
# | 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... |