#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=2e5;
const int P=(1<<18);
vector<int> e[N+10];
int tab[N+10];
int dd[N+10][2];
int fst[N+10];
int tr[2*P+10];
int clr[2*P+10];
int val[2*P+10];
bool myturn[N+10];
int out[N+10];
void add(int x,int c)
{
for(x+=P-1;x>0;x/=2)
{
if(clr[x]!=0)
break;
tr[x]+=c;
}
return;
}
void block(int i,int l,int r,int ls,int rs,int id)
{
if(clr[i]!=0 || rs<l || r<ls)
return;
if(ls<=l && r<=rs)
{
clr[i]=id;
val[i]=tr[i];
tr[i]=0;
return;
}
int mid=(l+r)/2;
block(2*i,l,mid,ls,rs,id);
block(2*i+1,mid+1,r,ls,rs,id);
tr[i]=tr[2*i]+tr[2*i+1];
return;
}
void unblock(int i,int l,int r,int ls,int rs,int id)
{
if(clr[i]==id)
{
clr[i]=0;
tr[i]=val[i];
return;
}
if(clr[i]!=0 || rs<l || r<ls)
return;
int mid=(l+r)/2;
unblock(2*i,l,mid,ls,rs,id);
unblock(2*i+1,mid+1,r,ls,rs,id);
tr[i]=tr[2*i]+tr[2*i+1];
return;
}
bool is_on(int x)
{
for(x+=P-1;x>0;x/=2)
{
if(clr[x]!=0)
return false;
}
return true;
}
pair<int,int> dfs_far(int x,int p)
{
pair<int,int> ans={-1,x};
for(auto v:e[x])
{
if(v!=p)
ans=max(ans,dfs_far(v,x));
}
ans.fi++;
return ans;
}
void dfs_dep(int x,int p,vector<int> &ans)
{
ans[x]=ans[p]+1;
for(auto v:e[x])
{
if(v!=p)
dfs_dep(v,x,ans);
}
return;
}
void dfs_dd(int x,int p)
{
dd[x][0]=dd[x][1]=0;
for(auto v:e[x])
{
if(v!=p)
{
dfs_dd(v,x);
if(dd[v][0]+1>dd[x][0])
{
dd[x][1]=dd[x][0];
dd[x][0]=dd[v][0]+1;
}
else if(dd[v][0]+1>dd[x][1])
dd[x][1]=dd[v][0]+1;
}
}
return;
}
void solve(int x,int p,int dep)
{
if(myturn[x])
{
block(1,1,P,dep-dd[x][0],dep-1,x);
out[x]=tr[1];
unblock(1,1,P,dep-dd[x][0],dep-1,x);
}
for(auto v:e[x])
{
if(v!=p)
{
int di=(dd[x][0]==dd[v][0]+1 ? dd[x][1]:dd[x][0]);
block(1,1,P,dep-di,dep-1,x);
int last_fst=-1;
if(fst[tab[x]]==0 || !is_on(fst[tab[x]]))
{
last_fst=fst[tab[x]];
fst[tab[x]]=dep;
add(fst[tab[x]],1);
}
solve(v,x,dep+1);
if(last_fst!=-1)
{
add(fst[tab[x]],-1);
fst[tab[x]]=last_fst;
}
unblock(1,1,P,dep-di,dep-1,x);
}
}
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
e[a].push_back(b);
e[b].push_back(a);
}
for(int i=1;i<=n;i++)
cin>>tab[i];
int dia[2];
dia[0]=dfs_far(1,0).se;
dia[1]=dfs_far(dia[0],0).se;
//cerr<<dia[0]<<" "<<dia[1]<<"\n";
vector<int> diadep[2];
for(int i:{0,1})
{
diadep[i].resize(N+10);
diadep[i][0]=0;
dfs_dep(dia[i],0,diadep[i]);
//for(int j=1;j<=n;j++)
// cerr<<diadep[i][j]<<" \n"[j==n];
}
for(bool it:{0,1})
{
for(int i=1;i<=n;i++)
{
myturn[i]=(diadep[it][i]>=diadep[!it][i]);
//cerr<<i<<" "<<myturn[i]<<"\n";
}
dfs_dd(dia[it],0);
solve(dia[it],0,1);
}
for(int i=1;i<=n;i++)
cout<<out[i]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
7 ms |
6868 KB |
Output is correct |
3 |
Correct |
6 ms |
6868 KB |
Output is correct |
4 |
Correct |
7 ms |
6868 KB |
Output is correct |
5 |
Correct |
8 ms |
6756 KB |
Output is correct |
6 |
Correct |
7 ms |
7116 KB |
Output is correct |
7 |
Correct |
7 ms |
6868 KB |
Output is correct |
8 |
Correct |
7 ms |
6848 KB |
Output is correct |
9 |
Correct |
7 ms |
6772 KB |
Output is correct |
10 |
Correct |
7 ms |
6868 KB |
Output is correct |
11 |
Correct |
7 ms |
6868 KB |
Output is correct |
12 |
Correct |
7 ms |
6740 KB |
Output is correct |
13 |
Correct |
7 ms |
6996 KB |
Output is correct |
14 |
Correct |
7 ms |
6868 KB |
Output is correct |
15 |
Correct |
7 ms |
6840 KB |
Output is correct |
16 |
Correct |
7 ms |
6740 KB |
Output is correct |
17 |
Correct |
9 ms |
6996 KB |
Output is correct |
18 |
Correct |
7 ms |
6996 KB |
Output is correct |
19 |
Correct |
8 ms |
6868 KB |
Output is correct |
20 |
Correct |
8 ms |
7132 KB |
Output is correct |
21 |
Correct |
7 ms |
6996 KB |
Output is correct |
22 |
Correct |
7 ms |
6836 KB |
Output is correct |
23 |
Correct |
7 ms |
6868 KB |
Output is correct |
24 |
Correct |
7 ms |
6772 KB |
Output is correct |
25 |
Correct |
7 ms |
6836 KB |
Output is correct |
26 |
Correct |
7 ms |
6740 KB |
Output is correct |
27 |
Correct |
8 ms |
7068 KB |
Output is correct |
28 |
Correct |
7 ms |
6996 KB |
Output is correct |
29 |
Correct |
7 ms |
6868 KB |
Output is correct |
30 |
Correct |
8 ms |
6868 KB |
Output is correct |
31 |
Correct |
7 ms |
6964 KB |
Output is correct |
32 |
Correct |
7 ms |
6892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
279 ms |
14164 KB |
Output is correct |
2 |
Correct |
364 ms |
27452 KB |
Output is correct |
3 |
Correct |
61 ms |
10024 KB |
Output is correct |
4 |
Correct |
558 ms |
19708 KB |
Output is correct |
5 |
Correct |
760 ms |
42940 KB |
Output is correct |
6 |
Correct |
730 ms |
30408 KB |
Output is correct |
7 |
Correct |
541 ms |
19752 KB |
Output is correct |
8 |
Correct |
597 ms |
22092 KB |
Output is correct |
9 |
Correct |
604 ms |
21324 KB |
Output is correct |
10 |
Correct |
588 ms |
21196 KB |
Output is correct |
11 |
Correct |
454 ms |
20408 KB |
Output is correct |
12 |
Correct |
732 ms |
34264 KB |
Output is correct |
13 |
Correct |
673 ms |
31116 KB |
Output is correct |
14 |
Correct |
643 ms |
30524 KB |
Output is correct |
15 |
Correct |
437 ms |
20256 KB |
Output is correct |
16 |
Correct |
643 ms |
34856 KB |
Output is correct |
17 |
Correct |
673 ms |
30528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
432 ms |
18172 KB |
Output is correct |
2 |
Correct |
725 ms |
43144 KB |
Output is correct |
3 |
Correct |
73 ms |
10752 KB |
Output is correct |
4 |
Correct |
584 ms |
21372 KB |
Output is correct |
5 |
Correct |
783 ms |
45644 KB |
Output is correct |
6 |
Correct |
793 ms |
32480 KB |
Output is correct |
7 |
Correct |
553 ms |
21448 KB |
Output is correct |
8 |
Correct |
646 ms |
26284 KB |
Output is correct |
9 |
Correct |
629 ms |
24628 KB |
Output is correct |
10 |
Correct |
617 ms |
23356 KB |
Output is correct |
11 |
Correct |
592 ms |
21708 KB |
Output is correct |
12 |
Correct |
764 ms |
40732 KB |
Output is correct |
13 |
Correct |
686 ms |
31712 KB |
Output is correct |
14 |
Correct |
689 ms |
31920 KB |
Output is correct |
15 |
Correct |
433 ms |
21304 KB |
Output is correct |
16 |
Correct |
673 ms |
37308 KB |
Output is correct |
17 |
Correct |
681 ms |
32768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
7 ms |
6868 KB |
Output is correct |
3 |
Correct |
6 ms |
6868 KB |
Output is correct |
4 |
Correct |
7 ms |
6868 KB |
Output is correct |
5 |
Correct |
8 ms |
6756 KB |
Output is correct |
6 |
Correct |
7 ms |
7116 KB |
Output is correct |
7 |
Correct |
7 ms |
6868 KB |
Output is correct |
8 |
Correct |
7 ms |
6848 KB |
Output is correct |
9 |
Correct |
7 ms |
6772 KB |
Output is correct |
10 |
Correct |
7 ms |
6868 KB |
Output is correct |
11 |
Correct |
7 ms |
6868 KB |
Output is correct |
12 |
Correct |
7 ms |
6740 KB |
Output is correct |
13 |
Correct |
7 ms |
6996 KB |
Output is correct |
14 |
Correct |
7 ms |
6868 KB |
Output is correct |
15 |
Correct |
7 ms |
6840 KB |
Output is correct |
16 |
Correct |
7 ms |
6740 KB |
Output is correct |
17 |
Correct |
9 ms |
6996 KB |
Output is correct |
18 |
Correct |
7 ms |
6996 KB |
Output is correct |
19 |
Correct |
8 ms |
6868 KB |
Output is correct |
20 |
Correct |
8 ms |
7132 KB |
Output is correct |
21 |
Correct |
7 ms |
6996 KB |
Output is correct |
22 |
Correct |
7 ms |
6836 KB |
Output is correct |
23 |
Correct |
7 ms |
6868 KB |
Output is correct |
24 |
Correct |
7 ms |
6772 KB |
Output is correct |
25 |
Correct |
7 ms |
6836 KB |
Output is correct |
26 |
Correct |
7 ms |
6740 KB |
Output is correct |
27 |
Correct |
8 ms |
7068 KB |
Output is correct |
28 |
Correct |
7 ms |
6996 KB |
Output is correct |
29 |
Correct |
7 ms |
6868 KB |
Output is correct |
30 |
Correct |
8 ms |
6868 KB |
Output is correct |
31 |
Correct |
7 ms |
6964 KB |
Output is correct |
32 |
Correct |
7 ms |
6892 KB |
Output is correct |
33 |
Correct |
279 ms |
14164 KB |
Output is correct |
34 |
Correct |
364 ms |
27452 KB |
Output is correct |
35 |
Correct |
61 ms |
10024 KB |
Output is correct |
36 |
Correct |
558 ms |
19708 KB |
Output is correct |
37 |
Correct |
760 ms |
42940 KB |
Output is correct |
38 |
Correct |
730 ms |
30408 KB |
Output is correct |
39 |
Correct |
541 ms |
19752 KB |
Output is correct |
40 |
Correct |
597 ms |
22092 KB |
Output is correct |
41 |
Correct |
604 ms |
21324 KB |
Output is correct |
42 |
Correct |
588 ms |
21196 KB |
Output is correct |
43 |
Correct |
454 ms |
20408 KB |
Output is correct |
44 |
Correct |
732 ms |
34264 KB |
Output is correct |
45 |
Correct |
673 ms |
31116 KB |
Output is correct |
46 |
Correct |
643 ms |
30524 KB |
Output is correct |
47 |
Correct |
437 ms |
20256 KB |
Output is correct |
48 |
Correct |
643 ms |
34856 KB |
Output is correct |
49 |
Correct |
673 ms |
30528 KB |
Output is correct |
50 |
Correct |
432 ms |
18172 KB |
Output is correct |
51 |
Correct |
725 ms |
43144 KB |
Output is correct |
52 |
Correct |
73 ms |
10752 KB |
Output is correct |
53 |
Correct |
584 ms |
21372 KB |
Output is correct |
54 |
Correct |
783 ms |
45644 KB |
Output is correct |
55 |
Correct |
793 ms |
32480 KB |
Output is correct |
56 |
Correct |
553 ms |
21448 KB |
Output is correct |
57 |
Correct |
646 ms |
26284 KB |
Output is correct |
58 |
Correct |
629 ms |
24628 KB |
Output is correct |
59 |
Correct |
617 ms |
23356 KB |
Output is correct |
60 |
Correct |
592 ms |
21708 KB |
Output is correct |
61 |
Correct |
764 ms |
40732 KB |
Output is correct |
62 |
Correct |
686 ms |
31712 KB |
Output is correct |
63 |
Correct |
689 ms |
31920 KB |
Output is correct |
64 |
Correct |
433 ms |
21304 KB |
Output is correct |
65 |
Correct |
673 ms |
37308 KB |
Output is correct |
66 |
Correct |
681 ms |
32768 KB |
Output is correct |
67 |
Correct |
52 ms |
8592 KB |
Output is correct |
68 |
Correct |
252 ms |
23048 KB |
Output is correct |
69 |
Correct |
398 ms |
22120 KB |
Output is correct |
70 |
Correct |
605 ms |
19660 KB |
Output is correct |
71 |
Correct |
744 ms |
43072 KB |
Output is correct |
72 |
Correct |
734 ms |
30496 KB |
Output is correct |
73 |
Correct |
554 ms |
19748 KB |
Output is correct |
74 |
Correct |
613 ms |
24320 KB |
Output is correct |
75 |
Correct |
595 ms |
21804 KB |
Output is correct |
76 |
Correct |
598 ms |
21668 KB |
Output is correct |
77 |
Correct |
547 ms |
19984 KB |
Output is correct |
78 |
Correct |
732 ms |
36940 KB |
Output is correct |
79 |
Correct |
653 ms |
33992 KB |
Output is correct |
80 |
Correct |
678 ms |
29180 KB |
Output is correct |
81 |
Correct |
432 ms |
20036 KB |
Output is correct |
82 |
Correct |
645 ms |
34700 KB |
Output is correct |
83 |
Correct |
683 ms |
30508 KB |
Output is correct |
84 |
Correct |
566 ms |
19952 KB |
Output is correct |
85 |
Correct |
746 ms |
43804 KB |
Output is correct |
86 |
Correct |
716 ms |
30964 KB |
Output is correct |
87 |
Correct |
563 ms |
20012 KB |
Output is correct |
88 |
Correct |
629 ms |
25036 KB |
Output is correct |
89 |
Correct |
597 ms |
22640 KB |
Output is correct |
90 |
Correct |
642 ms |
21860 KB |
Output is correct |
91 |
Correct |
485 ms |
20408 KB |
Output is correct |
92 |
Correct |
747 ms |
42952 KB |
Output is correct |
93 |
Correct |
643 ms |
28220 KB |
Output is correct |
94 |
Correct |
637 ms |
26060 KB |
Output is correct |
95 |
Correct |
442 ms |
20588 KB |
Output is correct |
96 |
Correct |
656 ms |
35384 KB |
Output is correct |
97 |
Correct |
664 ms |
31176 KB |
Output is correct |
98 |
Correct |
584 ms |
21292 KB |
Output is correct |
99 |
Correct |
768 ms |
44248 KB |
Output is correct |
100 |
Correct |
774 ms |
32360 KB |
Output is correct |
101 |
Correct |
627 ms |
20584 KB |
Output is correct |
102 |
Correct |
633 ms |
24516 KB |
Output is correct |
103 |
Correct |
607 ms |
23020 KB |
Output is correct |
104 |
Correct |
600 ms |
22708 KB |
Output is correct |
105 |
Correct |
530 ms |
20776 KB |
Output is correct |
106 |
Correct |
745 ms |
34380 KB |
Output is correct |
107 |
Correct |
665 ms |
34616 KB |
Output is correct |
108 |
Correct |
727 ms |
28684 KB |
Output is correct |
109 |
Correct |
436 ms |
21288 KB |
Output is correct |
110 |
Correct |
685 ms |
36360 KB |
Output is correct |
111 |
Correct |
719 ms |
32308 KB |
Output is correct |