#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define semicolon ;
#define ryan bear
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18+7;
struct seg
{
int c[800010],sm[800010],t[800010];
seg(){}
inline void init()
{
fill(c,c+800010,0);
fill(sm,sm+800010,0);
fill(t,t+800010,0);
}
inline void set(int n)
{
t[n]=c[n]>0?sm[n]:(n<400005?t[n*2]+t[n*2+1]:0);
return;
}
void set(int n,int s,int e,int x,int p)
{
if(s==e)
{
sm[n]=p;
set(n);
return;
}
int m=(s+e)/2;
if(x>m)
set(n*2+1,m+1,e,x,p);
else
set(n*2,s,m,x,p);
sm[n]=sm[n*2]+sm[n*2+1];
set(n);
return;
}
bool get(int n,int s,int e,int x)
{
if(c[n]>0)
return 1;
if(s==e)
return 0;
int m=(s+e)/2;
if(x>m)
return get(n*2+1,m+1,e,x);
return get(n*2,s,m,x);
}
void si(int n,int s,int e,int S,int E,int p)
{
if(s>E||S>e)
return;
if(S<=s&&e<=E)
{
c[n]+=p;
set(n);
return;
}
int m=(s+e)/2;
si(n*2,s,m,S,E,p);
si(n*2+1,m+1,e,S,E,p);
set(n);
return;
}
inline int sq()
{
return t[1];
}
}st;
vector<int>adj[200010];
int dp[200010],dp2[200010];
int fd[200010],sd[200010];
bool ch[200010];
int pos[200010];
int n,m;
void dfs(int x,int p)
{
ch[x]=dp[p]+1>dp[x];
dp[x]=dp[p]+1;
for(int&t:adj[x])
if(t!=p)
dfs(t,x);
fd[x]=sd[x]=0;
for(int&t:adj[x])
{
if(t==p)
continue;
if(fd[x]<dp2[t])
sd[x]=fd[x],fd[x]=dp2[t];
else if(sd[x]<dp2[t])
sd[x]=dp2[t];
}
dp2[x]=fd[x]+1;
return;
}
int ans[200010],c[200010];
int c2;
void dfs2(int x,int p)
{
if(ch[x])
{
int up=max(1,dp[x]-fd[x]);
if(up<dp[x])
st.si(1,1,n,up,dp[x]-1,1);
ans[x]=c2-st.sq();
if(up<dp[x])
st.si(1,1,n,up,dp[x]-1,-1);
}
for(int&t:adj[x])
{
if(t==p)
continue;
int up=max(1,dp[x]-(dp2[t]==fd[x]?sd[x]:fd[x]));
if(up<dp[x])
st.si(1,1,n,up,dp[x]-1,1);
int pp=pos[c[x]];
bool cn=0;
if(pp==0||st.get(1,1,n,pp))
pos[c[x]]=dp[x],cn=1,st.set(1,1,n,dp[x],1),c2++;
dfs2(t,x);
if(cn)
pos[c[x]]=pp,st.set(1,1,n,dp[x],0),c2--;
if(up<dp[x])
st.si(1,1,n,up,dp[x]-1,-1);
}
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m;
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
adj[u].eb(v);
adj[v].eb(u);
}
for(int i=0;i++<n;)
cin>>c[i];
dfs(1,0);
int l1=max_element(dp+1,dp+n+1)-dp;
dfs(l1,0);
for(int i=0;i++<n;)
ch[i]=1;
dfs2(l1,0);
int l2=max_element(dp+1,dp+n+1)-dp;
for(int i=0;i++<n;)
ch[i]=0;
dfs(l2,0);
dfs2(l2,0);
for(int i=0;i++<n;)
cout<<ans[i]<<'\n';
cout.flush();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5112 KB |
Output is correct |
2 |
Correct |
11 ms |
5368 KB |
Output is correct |
3 |
Correct |
9 ms |
5240 KB |
Output is correct |
4 |
Correct |
10 ms |
5368 KB |
Output is correct |
5 |
Correct |
12 ms |
5240 KB |
Output is correct |
6 |
Correct |
10 ms |
5496 KB |
Output is correct |
7 |
Correct |
11 ms |
5368 KB |
Output is correct |
8 |
Correct |
10 ms |
5240 KB |
Output is correct |
9 |
Correct |
11 ms |
5240 KB |
Output is correct |
10 |
Correct |
12 ms |
5368 KB |
Output is correct |
11 |
Correct |
11 ms |
5240 KB |
Output is correct |
12 |
Correct |
10 ms |
5240 KB |
Output is correct |
13 |
Correct |
10 ms |
5496 KB |
Output is correct |
14 |
Correct |
11 ms |
5368 KB |
Output is correct |
15 |
Correct |
12 ms |
5368 KB |
Output is correct |
16 |
Correct |
10 ms |
5240 KB |
Output is correct |
17 |
Correct |
11 ms |
5368 KB |
Output is correct |
18 |
Correct |
10 ms |
5368 KB |
Output is correct |
19 |
Correct |
12 ms |
5240 KB |
Output is correct |
20 |
Correct |
12 ms |
5500 KB |
Output is correct |
21 |
Correct |
11 ms |
5368 KB |
Output is correct |
22 |
Correct |
11 ms |
5240 KB |
Output is correct |
23 |
Correct |
11 ms |
5368 KB |
Output is correct |
24 |
Correct |
13 ms |
5240 KB |
Output is correct |
25 |
Correct |
11 ms |
5240 KB |
Output is correct |
26 |
Correct |
10 ms |
5240 KB |
Output is correct |
27 |
Correct |
12 ms |
5496 KB |
Output is correct |
28 |
Correct |
12 ms |
5368 KB |
Output is correct |
29 |
Correct |
11 ms |
5368 KB |
Output is correct |
30 |
Correct |
10 ms |
5240 KB |
Output is correct |
31 |
Correct |
11 ms |
5368 KB |
Output is correct |
32 |
Correct |
11 ms |
5368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
350 ms |
13048 KB |
Output is correct |
2 |
Correct |
355 ms |
26232 KB |
Output is correct |
3 |
Correct |
57 ms |
8824 KB |
Output is correct |
4 |
Correct |
679 ms |
19836 KB |
Output is correct |
5 |
Correct |
842 ms |
42456 KB |
Output is correct |
6 |
Correct |
789 ms |
31224 KB |
Output is correct |
7 |
Correct |
654 ms |
19852 KB |
Output is correct |
8 |
Correct |
779 ms |
22392 KB |
Output is correct |
9 |
Correct |
783 ms |
21504 KB |
Output is correct |
10 |
Correct |
814 ms |
21624 KB |
Output is correct |
11 |
Correct |
506 ms |
20500 KB |
Output is correct |
12 |
Correct |
888 ms |
34620 KB |
Output is correct |
13 |
Correct |
777 ms |
31408 KB |
Output is correct |
14 |
Correct |
748 ms |
31092 KB |
Output is correct |
15 |
Correct |
434 ms |
20332 KB |
Output is correct |
16 |
Correct |
651 ms |
35312 KB |
Output is correct |
17 |
Correct |
658 ms |
31604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
558 ms |
16120 KB |
Output is correct |
2 |
Correct |
851 ms |
42872 KB |
Output is correct |
3 |
Correct |
75 ms |
9592 KB |
Output is correct |
4 |
Correct |
747 ms |
18744 KB |
Output is correct |
5 |
Correct |
895 ms |
44360 KB |
Output is correct |
6 |
Correct |
839 ms |
31356 KB |
Output is correct |
7 |
Correct |
706 ms |
18864 KB |
Output is correct |
8 |
Correct |
818 ms |
24064 KB |
Output is correct |
9 |
Correct |
769 ms |
22124 KB |
Output is correct |
10 |
Correct |
768 ms |
20732 KB |
Output is correct |
11 |
Correct |
585 ms |
20592 KB |
Output is correct |
12 |
Correct |
900 ms |
42104 KB |
Output is correct |
13 |
Correct |
817 ms |
32372 KB |
Output is correct |
14 |
Correct |
849 ms |
32884 KB |
Output is correct |
15 |
Correct |
452 ms |
21484 KB |
Output is correct |
16 |
Correct |
771 ms |
38764 KB |
Output is correct |
17 |
Correct |
774 ms |
34292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
5112 KB |
Output is correct |
2 |
Correct |
11 ms |
5368 KB |
Output is correct |
3 |
Correct |
9 ms |
5240 KB |
Output is correct |
4 |
Correct |
10 ms |
5368 KB |
Output is correct |
5 |
Correct |
12 ms |
5240 KB |
Output is correct |
6 |
Correct |
10 ms |
5496 KB |
Output is correct |
7 |
Correct |
11 ms |
5368 KB |
Output is correct |
8 |
Correct |
10 ms |
5240 KB |
Output is correct |
9 |
Correct |
11 ms |
5240 KB |
Output is correct |
10 |
Correct |
12 ms |
5368 KB |
Output is correct |
11 |
Correct |
11 ms |
5240 KB |
Output is correct |
12 |
Correct |
10 ms |
5240 KB |
Output is correct |
13 |
Correct |
10 ms |
5496 KB |
Output is correct |
14 |
Correct |
11 ms |
5368 KB |
Output is correct |
15 |
Correct |
12 ms |
5368 KB |
Output is correct |
16 |
Correct |
10 ms |
5240 KB |
Output is correct |
17 |
Correct |
11 ms |
5368 KB |
Output is correct |
18 |
Correct |
10 ms |
5368 KB |
Output is correct |
19 |
Correct |
12 ms |
5240 KB |
Output is correct |
20 |
Correct |
12 ms |
5500 KB |
Output is correct |
21 |
Correct |
11 ms |
5368 KB |
Output is correct |
22 |
Correct |
11 ms |
5240 KB |
Output is correct |
23 |
Correct |
11 ms |
5368 KB |
Output is correct |
24 |
Correct |
13 ms |
5240 KB |
Output is correct |
25 |
Correct |
11 ms |
5240 KB |
Output is correct |
26 |
Correct |
10 ms |
5240 KB |
Output is correct |
27 |
Correct |
12 ms |
5496 KB |
Output is correct |
28 |
Correct |
12 ms |
5368 KB |
Output is correct |
29 |
Correct |
11 ms |
5368 KB |
Output is correct |
30 |
Correct |
10 ms |
5240 KB |
Output is correct |
31 |
Correct |
11 ms |
5368 KB |
Output is correct |
32 |
Correct |
11 ms |
5368 KB |
Output is correct |
33 |
Correct |
350 ms |
13048 KB |
Output is correct |
34 |
Correct |
355 ms |
26232 KB |
Output is correct |
35 |
Correct |
57 ms |
8824 KB |
Output is correct |
36 |
Correct |
679 ms |
19836 KB |
Output is correct |
37 |
Correct |
842 ms |
42456 KB |
Output is correct |
38 |
Correct |
789 ms |
31224 KB |
Output is correct |
39 |
Correct |
654 ms |
19852 KB |
Output is correct |
40 |
Correct |
779 ms |
22392 KB |
Output is correct |
41 |
Correct |
783 ms |
21504 KB |
Output is correct |
42 |
Correct |
814 ms |
21624 KB |
Output is correct |
43 |
Correct |
506 ms |
20500 KB |
Output is correct |
44 |
Correct |
888 ms |
34620 KB |
Output is correct |
45 |
Correct |
777 ms |
31408 KB |
Output is correct |
46 |
Correct |
748 ms |
31092 KB |
Output is correct |
47 |
Correct |
434 ms |
20332 KB |
Output is correct |
48 |
Correct |
651 ms |
35312 KB |
Output is correct |
49 |
Correct |
658 ms |
31604 KB |
Output is correct |
50 |
Correct |
558 ms |
16120 KB |
Output is correct |
51 |
Correct |
851 ms |
42872 KB |
Output is correct |
52 |
Correct |
75 ms |
9592 KB |
Output is correct |
53 |
Correct |
747 ms |
18744 KB |
Output is correct |
54 |
Correct |
895 ms |
44360 KB |
Output is correct |
55 |
Correct |
839 ms |
31356 KB |
Output is correct |
56 |
Correct |
706 ms |
18864 KB |
Output is correct |
57 |
Correct |
818 ms |
24064 KB |
Output is correct |
58 |
Correct |
769 ms |
22124 KB |
Output is correct |
59 |
Correct |
768 ms |
20732 KB |
Output is correct |
60 |
Correct |
585 ms |
20592 KB |
Output is correct |
61 |
Correct |
900 ms |
42104 KB |
Output is correct |
62 |
Correct |
817 ms |
32372 KB |
Output is correct |
63 |
Correct |
849 ms |
32884 KB |
Output is correct |
64 |
Correct |
452 ms |
21484 KB |
Output is correct |
65 |
Correct |
771 ms |
38764 KB |
Output is correct |
66 |
Correct |
774 ms |
34292 KB |
Output is correct |
67 |
Correct |
68 ms |
7288 KB |
Output is correct |
68 |
Correct |
288 ms |
23288 KB |
Output is correct |
69 |
Correct |
421 ms |
22128 KB |
Output is correct |
70 |
Correct |
717 ms |
19960 KB |
Output is correct |
71 |
Execution timed out |
143 ms |
14456 KB |
Time limit exceeded (wall clock) |
72 |
Halted |
0 ms |
0 KB |
- |