#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=200050;
vector<int> E[N];
int col[N],dep[N],par[N];
void DEP(int u, int p)
{
dep[u]=dep[p]+1;
par[u]=p;
for(int v:E[u]) if(v!=p) DEP(v,u);
}
int cen,den,n,m;
int type[N];
bool on_d[N];
const int M=2*N;
const int L=18;
struct SegmentTree
{
int mx[M],ls[M],rs[M],lzy[M],tsz,root,n;
SegmentTree(){}
void init(){ for(int i=1;i<=tsz;i++) ls[i]=rs[i]=mx[i]=lzy[i]=0;root=tsz=n=0;}
void Build(int &c, int ss, int se, int a[])
{
c=++tsz;lzy[c]=0;
if(ss==se){ mx[c]=a[ss];return;}
int mid=ss+se>>1;
Build(ls[c],ss,mid,a);
Build(rs[c],mid+1,se,a);
mx[c]=max(mx[ls[c]],mx[rs[c]]);
}
void Build(int _n, int a[]){ n=_n;Build(root,1,n,a);}
void Set(int c, int ss, int se, int qs, int qe, int f)
{
if(qs>qe || qs>se || ss>qe) return;
if(qs<=ss && qe>=se){ lzy[c]+=f;mx[c]+=f;return;}
int mid=ss+se>>1;
Set(ls[c],ss,mid,qs,qe,f);
Set(rs[c],mid+1,se,qs,qe,f);
mx[c]=max(mx[ls[c]],mx[rs[c]])+lzy[c];
}
void Set(int qs, int qe, int f){ Set(root,1,n,qs,qe,f);}
int Get(int c, int ss, int se, int qs, int qe)
{
if(qs<=ss && qe>=se) return mx[c];
int mid=ss+se>>1;
if(qe<=mid) return Get(ls[c],ss,mid,qs,qe)+lzy[c];
if(qs>mid) return Get(rs[c],mid+1,se,qs,qe)+lzy[c];
return max(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe))+lzy[c];
}
int Get(int qs, int qe){ return Get(root,1,n,qs,qe);}
} ST;
int lid[N],rid[N],tid,a[N];
void DFS(int u, int p)
{
dep[u]=dep[p]+1;
lid[u]=++tid;
a[tid]=dep[u];
for(int v:E[u]) if(v!=p) DFS(v,u);
rid[u]=tid;
}
int cnt[N],anc[N][L],ans[N],sum;
bool work[N];
int GetAnc(int u, int k)
{
for(int i=0;i<L;i++) if(k>>i&1) u=anc[u][i];
return u;
}
void Add(int u)
{
cnt[col[u]]++;
if(cnt[col[u]]==1) sum++;
}
void Del(int u)
{
cnt[col[u]]--;
if(cnt[col[u]]==0) sum--;
}
void Solve(int u, int p, int t)
{
if(on_d[u] && type[u]==t) work[u]=1;
if(work[u])
{
for(int v:E[u]) if(v!=p) work[v]=1;
}
anc[u][0]=p;
for(int i=1;i<L;i++) anc[u][i]=anc[anc[u][i-1]][i-1];
vector<int> add;
ST.Set(lid[u],rid[u],-2);
int myd=ST.Get(lid[u],lid[u]);
int d=ST.Get(lid[u],rid[u])-myd;
int z=GetAnc(u,d);
if(z!=0 && ST.Get(lid[z],rid[z])==ST.Get(lid[z],lid[z]))
{
if(anc[z][0]) add.pb(anc[z][0]);
}
z=anc[z][0];
if(z!=0 && ST.Get(lid[z],rid[z])==ST.Get(lid[z],lid[z]))
{
if(anc[z][0]) add.pb(anc[z][0]);
}
for(int v:add) Add(v);
if(work[u])
{
//printf("%i %i :%i\n",u,t,sum);
ans[u]=sum;
}
for(int v:E[u]) if(v!=p) Solve(v,u,t);
ST.Set(lid[u],rid[u],2);
for(int v:add) Del(v);
}
void Solve(int u, int t)
{
for(int i=1;i<=n;i++) work[i]=0;
ST.init();tid=0;
DFS(u,0);
ST.Build(tid,a);
Solve(u,0,t);
}
int main()
{
int u,v;
scanf("%i %i",&n,&m);
for(int i=1;i<n;i++) scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
for(int i=1;i<=n;i++) scanf("%i",&col[i]);
DEP(1,0);for(int i=1;i<=n;i++) if(dep[i]>dep[cen]) cen=i;
DEP(cen,0);for(int i=1;i<=n;i++) if(dep[i]>dep[den]) den=i;
for(int i=den;i;i=par[i]) on_d[i]=1;
int hal=dep[den]/2;
for(int i=den;dep[i]>hal;i=par[i]) type[i]=1;
Solve(cen,1);
Solve(den,0);
for(int i=1;i<=n;i++) printf("%i\n",ans[i]);
return 0;
}
Compilation message
joi2019_ho_t5.cpp: In member function 'void SegmentTree::Build(int&, int, int, int*)':
joi2019_ho_t5.cpp:27:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=ss+se>>1;
~~^~~
joi2019_ho_t5.cpp: In member function 'void SegmentTree::Set(int, int, int, int, int, int)':
joi2019_ho_t5.cpp:37:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=ss+se>>1;
~~^~~
joi2019_ho_t5.cpp: In member function 'int SegmentTree::Get(int, int, int, int, int)':
joi2019_ho_t5.cpp:46:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=ss+se>>1;
~~^~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:126:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&m);
~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:127:54: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<n;i++) scanf("%i %i",&u,&v),E[u].pb(v),E[v].pb(u);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
joi2019_ho_t5.cpp:128:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf("%i",&col[i]);
~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
12 ms |
5496 KB |
Output is correct |
3 |
Correct |
10 ms |
5496 KB |
Output is correct |
4 |
Correct |
12 ms |
5628 KB |
Output is correct |
5 |
Correct |
13 ms |
5496 KB |
Output is correct |
6 |
Correct |
12 ms |
5880 KB |
Output is correct |
7 |
Correct |
13 ms |
5624 KB |
Output is correct |
8 |
Correct |
12 ms |
5496 KB |
Output is correct |
9 |
Correct |
13 ms |
5624 KB |
Output is correct |
10 |
Correct |
13 ms |
5496 KB |
Output is correct |
11 |
Correct |
13 ms |
5496 KB |
Output is correct |
12 |
Correct |
11 ms |
5512 KB |
Output is correct |
13 |
Correct |
14 ms |
5904 KB |
Output is correct |
14 |
Correct |
13 ms |
5592 KB |
Output is correct |
15 |
Correct |
13 ms |
5596 KB |
Output is correct |
16 |
Correct |
11 ms |
5496 KB |
Output is correct |
17 |
Correct |
12 ms |
5752 KB |
Output is correct |
18 |
Correct |
13 ms |
5624 KB |
Output is correct |
19 |
Correct |
13 ms |
5496 KB |
Output is correct |
20 |
Correct |
12 ms |
5880 KB |
Output is correct |
21 |
Correct |
13 ms |
5624 KB |
Output is correct |
22 |
Correct |
12 ms |
5496 KB |
Output is correct |
23 |
Correct |
13 ms |
5496 KB |
Output is correct |
24 |
Correct |
13 ms |
5544 KB |
Output is correct |
25 |
Correct |
13 ms |
5496 KB |
Output is correct |
26 |
Correct |
11 ms |
5496 KB |
Output is correct |
27 |
Correct |
16 ms |
5752 KB |
Output is correct |
28 |
Correct |
14 ms |
5752 KB |
Output is correct |
29 |
Correct |
19 ms |
5624 KB |
Output is correct |
30 |
Correct |
12 ms |
5624 KB |
Output is correct |
31 |
Correct |
12 ms |
5752 KB |
Output is correct |
32 |
Correct |
13 ms |
5624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
550 ms |
26092 KB |
Output is correct |
2 |
Correct |
746 ms |
49400 KB |
Output is correct |
3 |
Correct |
132 ms |
13048 KB |
Output is correct |
4 |
Correct |
1032 ms |
41696 KB |
Output is correct |
5 |
Correct |
1443 ms |
82396 KB |
Output is correct |
6 |
Correct |
1447 ms |
61316 KB |
Output is correct |
7 |
Correct |
979 ms |
41080 KB |
Output is correct |
8 |
Correct |
1169 ms |
45688 KB |
Output is correct |
9 |
Correct |
1072 ms |
44508 KB |
Output is correct |
10 |
Correct |
1109 ms |
44264 KB |
Output is correct |
11 |
Correct |
755 ms |
41580 KB |
Output is correct |
12 |
Correct |
1516 ms |
66748 KB |
Output is correct |
13 |
Correct |
1316 ms |
61172 KB |
Output is correct |
14 |
Correct |
1418 ms |
59628 KB |
Output is correct |
15 |
Correct |
737 ms |
41448 KB |
Output is correct |
16 |
Correct |
1336 ms |
69068 KB |
Output is correct |
17 |
Correct |
1435 ms |
61436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
794 ms |
35360 KB |
Output is correct |
2 |
Correct |
1391 ms |
81360 KB |
Output is correct |
3 |
Correct |
165 ms |
14328 KB |
Output is correct |
4 |
Correct |
1067 ms |
43096 KB |
Output is correct |
5 |
Correct |
1477 ms |
85020 KB |
Output is correct |
6 |
Correct |
1329 ms |
63108 KB |
Output is correct |
7 |
Correct |
1031 ms |
42724 KB |
Output is correct |
8 |
Correct |
1246 ms |
51424 KB |
Output is correct |
9 |
Correct |
1134 ms |
48508 KB |
Output is correct |
10 |
Correct |
1128 ms |
46460 KB |
Output is correct |
11 |
Correct |
954 ms |
43028 KB |
Output is correct |
12 |
Correct |
1656 ms |
76424 KB |
Output is correct |
13 |
Correct |
1284 ms |
60664 KB |
Output is correct |
14 |
Correct |
1500 ms |
60812 KB |
Output is correct |
15 |
Correct |
749 ms |
42452 KB |
Output is correct |
16 |
Correct |
1363 ms |
71416 KB |
Output is correct |
17 |
Correct |
1349 ms |
63280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
12 ms |
5496 KB |
Output is correct |
3 |
Correct |
10 ms |
5496 KB |
Output is correct |
4 |
Correct |
12 ms |
5628 KB |
Output is correct |
5 |
Correct |
13 ms |
5496 KB |
Output is correct |
6 |
Correct |
12 ms |
5880 KB |
Output is correct |
7 |
Correct |
13 ms |
5624 KB |
Output is correct |
8 |
Correct |
12 ms |
5496 KB |
Output is correct |
9 |
Correct |
13 ms |
5624 KB |
Output is correct |
10 |
Correct |
13 ms |
5496 KB |
Output is correct |
11 |
Correct |
13 ms |
5496 KB |
Output is correct |
12 |
Correct |
11 ms |
5512 KB |
Output is correct |
13 |
Correct |
14 ms |
5904 KB |
Output is correct |
14 |
Correct |
13 ms |
5592 KB |
Output is correct |
15 |
Correct |
13 ms |
5596 KB |
Output is correct |
16 |
Correct |
11 ms |
5496 KB |
Output is correct |
17 |
Correct |
12 ms |
5752 KB |
Output is correct |
18 |
Correct |
13 ms |
5624 KB |
Output is correct |
19 |
Correct |
13 ms |
5496 KB |
Output is correct |
20 |
Correct |
12 ms |
5880 KB |
Output is correct |
21 |
Correct |
13 ms |
5624 KB |
Output is correct |
22 |
Correct |
12 ms |
5496 KB |
Output is correct |
23 |
Correct |
13 ms |
5496 KB |
Output is correct |
24 |
Correct |
13 ms |
5544 KB |
Output is correct |
25 |
Correct |
13 ms |
5496 KB |
Output is correct |
26 |
Correct |
11 ms |
5496 KB |
Output is correct |
27 |
Correct |
16 ms |
5752 KB |
Output is correct |
28 |
Correct |
14 ms |
5752 KB |
Output is correct |
29 |
Correct |
19 ms |
5624 KB |
Output is correct |
30 |
Correct |
12 ms |
5624 KB |
Output is correct |
31 |
Correct |
12 ms |
5752 KB |
Output is correct |
32 |
Correct |
13 ms |
5624 KB |
Output is correct |
33 |
Correct |
550 ms |
26092 KB |
Output is correct |
34 |
Correct |
746 ms |
49400 KB |
Output is correct |
35 |
Correct |
132 ms |
13048 KB |
Output is correct |
36 |
Correct |
1032 ms |
41696 KB |
Output is correct |
37 |
Correct |
1443 ms |
82396 KB |
Output is correct |
38 |
Correct |
1447 ms |
61316 KB |
Output is correct |
39 |
Correct |
979 ms |
41080 KB |
Output is correct |
40 |
Correct |
1169 ms |
45688 KB |
Output is correct |
41 |
Correct |
1072 ms |
44508 KB |
Output is correct |
42 |
Correct |
1109 ms |
44264 KB |
Output is correct |
43 |
Correct |
755 ms |
41580 KB |
Output is correct |
44 |
Correct |
1516 ms |
66748 KB |
Output is correct |
45 |
Correct |
1316 ms |
61172 KB |
Output is correct |
46 |
Correct |
1418 ms |
59628 KB |
Output is correct |
47 |
Correct |
737 ms |
41448 KB |
Output is correct |
48 |
Correct |
1336 ms |
69068 KB |
Output is correct |
49 |
Correct |
1435 ms |
61436 KB |
Output is correct |
50 |
Correct |
794 ms |
35360 KB |
Output is correct |
51 |
Correct |
1391 ms |
81360 KB |
Output is correct |
52 |
Correct |
165 ms |
14328 KB |
Output is correct |
53 |
Correct |
1067 ms |
43096 KB |
Output is correct |
54 |
Correct |
1477 ms |
85020 KB |
Output is correct |
55 |
Correct |
1329 ms |
63108 KB |
Output is correct |
56 |
Correct |
1031 ms |
42724 KB |
Output is correct |
57 |
Correct |
1246 ms |
51424 KB |
Output is correct |
58 |
Correct |
1134 ms |
48508 KB |
Output is correct |
59 |
Correct |
1128 ms |
46460 KB |
Output is correct |
60 |
Correct |
954 ms |
43028 KB |
Output is correct |
61 |
Correct |
1656 ms |
76424 KB |
Output is correct |
62 |
Correct |
1284 ms |
60664 KB |
Output is correct |
63 |
Correct |
1500 ms |
60812 KB |
Output is correct |
64 |
Correct |
749 ms |
42452 KB |
Output is correct |
65 |
Correct |
1363 ms |
71416 KB |
Output is correct |
66 |
Correct |
1349 ms |
63280 KB |
Output is correct |
67 |
Correct |
107 ms |
10124 KB |
Output is correct |
68 |
Correct |
576 ms |
39256 KB |
Output is correct |
69 |
Correct |
825 ms |
40496 KB |
Output is correct |
70 |
Correct |
1048 ms |
41868 KB |
Output is correct |
71 |
Correct |
1437 ms |
82324 KB |
Output is correct |
72 |
Correct |
1691 ms |
61264 KB |
Output is correct |
73 |
Correct |
989 ms |
41200 KB |
Output is correct |
74 |
Correct |
1171 ms |
49096 KB |
Output is correct |
75 |
Correct |
1108 ms |
45184 KB |
Output is correct |
76 |
Correct |
1161 ms |
44744 KB |
Output is correct |
77 |
Correct |
851 ms |
41200 KB |
Output is correct |
78 |
Correct |
1530 ms |
71312 KB |
Output is correct |
79 |
Correct |
1356 ms |
66228 KB |
Output is correct |
80 |
Correct |
1386 ms |
57468 KB |
Output is correct |
81 |
Correct |
748 ms |
41312 KB |
Output is correct |
82 |
Correct |
1261 ms |
68976 KB |
Output is correct |
83 |
Correct |
1350 ms |
61344 KB |
Output is correct |
84 |
Correct |
1016 ms |
41916 KB |
Output is correct |
85 |
Correct |
1421 ms |
82984 KB |
Output is correct |
86 |
Correct |
1428 ms |
61788 KB |
Output is correct |
87 |
Correct |
980 ms |
41232 KB |
Output is correct |
88 |
Correct |
1188 ms |
49928 KB |
Output is correct |
89 |
Correct |
1095 ms |
46028 KB |
Output is correct |
90 |
Correct |
1180 ms |
45052 KB |
Output is correct |
91 |
Correct |
894 ms |
41684 KB |
Output is correct |
92 |
Correct |
1652 ms |
81608 KB |
Output is correct |
93 |
Correct |
1277 ms |
55624 KB |
Output is correct |
94 |
Correct |
1263 ms |
52064 KB |
Output is correct |
95 |
Correct |
741 ms |
41712 KB |
Output is correct |
96 |
Correct |
1289 ms |
69616 KB |
Output is correct |
97 |
Correct |
1354 ms |
62116 KB |
Output is correct |
98 |
Correct |
1077 ms |
43356 KB |
Output is correct |
99 |
Correct |
1485 ms |
83644 KB |
Output is correct |
100 |
Correct |
1440 ms |
62856 KB |
Output is correct |
101 |
Correct |
1004 ms |
41992 KB |
Output is correct |
102 |
Correct |
1229 ms |
48720 KB |
Output is correct |
103 |
Correct |
1129 ms |
46368 KB |
Output is correct |
104 |
Correct |
1133 ms |
45560 KB |
Output is correct |
105 |
Correct |
885 ms |
41968 KB |
Output is correct |
106 |
Correct |
1469 ms |
65352 KB |
Output is correct |
107 |
Correct |
1381 ms |
66560 KB |
Output is correct |
108 |
Correct |
1350 ms |
55108 KB |
Output is correct |
109 |
Correct |
737 ms |
42280 KB |
Output is correct |
110 |
Correct |
1320 ms |
70472 KB |
Output is correct |
111 |
Correct |
1376 ms |
62800 KB |
Output is correct |