#include<bits/stdc++.h>
using namespace std;
const int N=2e5+2;
int cnt[N],dp[N],color[N],level[N],ans[N],pts=0,dem=0;
vector<int> adj[N];
stack<int> lis;
void add(int x){
cnt[color[x]]++;
if(cnt[color[x]]==1){
dem++;
}
lis.push(x);
}
void del(){
assert(lis.size());
int x=lis.top();
lis.pop();
cnt[color[x]]--;
if(cnt[color[x]]==0){
dem--;
}
}
void dfs(int x,int p){
if(level[x]>=level[pts]){
pts=x;
}
dp[x]=level[x];
for(int i=0;i<adj[x].size();i++){
if(adj[x][i]!=p){
level[adj[x][i]]=level[x]+1;
dfs(adj[x][i],x);
dp[x]=max(dp[x],dp[adj[x][i]]);
}
}
}
bool cmp(pair<int,int> x,pair<int,int> y){
return x.first>y.first;
}
void dfs1(int x,int p){
if(x!=p){
add(p);
}
vector<pair<int,int> > wow;
for(int i=0;i<adj[x].size();i++){
if(adj[x][i]!=p){
wow.push_back({dp[adj[x][i]],adj[x][i]});
}
}
sort(wow.begin(),wow.end(),cmp);
for(int i=0;i<wow.size();i++){
int to=0;
if(i==0){
if(wow.size()>1){
to=wow[1].first;
}
}
else{
to=wow[0].first;
}
while(lis.size()&&to-level[x]>=level[x]-level[lis.top()]){
del();
}
dfs1(wow[i].second,x);
}
while(lis.size()&&dp[x]-level[x]>=level[x]-level[lis.top()]){
del();
}
ans[x]=max(ans[x],dem);
// cout<<x<<' '<<lis.size()<<' '<<level[x]<<' '<<dp[x]<<' '<<dem<<" cac\n";
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,i,j,k,l,m;
cin>>n>>m;
for(i=1;i<n;i++){
cin>>j>>k;
adj[j].push_back(k);
adj[k].push_back(j);
}
for(i=1;i<=n;i++){
cin>>color[i];
}
dfs(1,1);
for(j=0;j<2;j++){
// cout<<j<<endl;
i=pts;
level[i]=0;
dfs(i,i);
dfs1(i,i);
assert(lis.size()==0);
}
for(i=1;i<=n;i++){
cout<<ans[i]<<'\n';
}
}
Compilation message
joi2019_ho_t5.cpp: In function 'void dfs(int, int)':
joi2019_ho_t5.cpp:28:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[x].size();i++){
~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'void dfs1(int, int)':
joi2019_ho_t5.cpp:44:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[x].size();i++){
~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp:50:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<wow.size();i++){
~^~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:74:14: warning: unused variable 'l' [-Wunused-variable]
int n,i,j,k,l,m;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5368 KB |
Output is correct |
4 |
Correct |
8 ms |
5368 KB |
Output is correct |
5 |
Correct |
8 ms |
5368 KB |
Output is correct |
6 |
Correct |
8 ms |
5496 KB |
Output is correct |
7 |
Correct |
8 ms |
5368 KB |
Output is correct |
8 |
Correct |
8 ms |
5240 KB |
Output is correct |
9 |
Correct |
8 ms |
5240 KB |
Output is correct |
10 |
Correct |
8 ms |
5240 KB |
Output is correct |
11 |
Correct |
8 ms |
5240 KB |
Output is correct |
12 |
Correct |
8 ms |
5240 KB |
Output is correct |
13 |
Correct |
8 ms |
5496 KB |
Output is correct |
14 |
Correct |
8 ms |
5368 KB |
Output is correct |
15 |
Correct |
8 ms |
5368 KB |
Output is correct |
16 |
Correct |
7 ms |
5240 KB |
Output is correct |
17 |
Correct |
8 ms |
5428 KB |
Output is correct |
18 |
Correct |
8 ms |
5368 KB |
Output is correct |
19 |
Correct |
8 ms |
5240 KB |
Output is correct |
20 |
Correct |
8 ms |
5496 KB |
Output is correct |
21 |
Correct |
8 ms |
5368 KB |
Output is correct |
22 |
Correct |
8 ms |
5240 KB |
Output is correct |
23 |
Correct |
8 ms |
5240 KB |
Output is correct |
24 |
Correct |
8 ms |
5244 KB |
Output is correct |
25 |
Correct |
8 ms |
5240 KB |
Output is correct |
26 |
Correct |
9 ms |
5240 KB |
Output is correct |
27 |
Correct |
10 ms |
5496 KB |
Output is correct |
28 |
Correct |
9 ms |
5368 KB |
Output is correct |
29 |
Correct |
8 ms |
5368 KB |
Output is correct |
30 |
Correct |
7 ms |
5240 KB |
Output is correct |
31 |
Correct |
9 ms |
5496 KB |
Output is correct |
32 |
Correct |
9 ms |
5368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
203 ms |
12536 KB |
Output is correct |
2 |
Correct |
275 ms |
28924 KB |
Output is correct |
3 |
Correct |
42 ms |
8952 KB |
Output is correct |
4 |
Correct |
402 ms |
17912 KB |
Output is correct |
5 |
Correct |
563 ms |
46840 KB |
Output is correct |
6 |
Correct |
512 ms |
32232 KB |
Output is correct |
7 |
Correct |
408 ms |
18512 KB |
Output is correct |
8 |
Correct |
563 ms |
20984 KB |
Output is correct |
9 |
Correct |
481 ms |
19804 KB |
Output is correct |
10 |
Correct |
435 ms |
19780 KB |
Output is correct |
11 |
Correct |
320 ms |
21800 KB |
Output is correct |
12 |
Correct |
510 ms |
36088 KB |
Output is correct |
13 |
Correct |
462 ms |
32368 KB |
Output is correct |
14 |
Correct |
474 ms |
31524 KB |
Output is correct |
15 |
Correct |
253 ms |
21692 KB |
Output is correct |
16 |
Correct |
463 ms |
38624 KB |
Output is correct |
17 |
Correct |
473 ms |
32844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
329 ms |
16488 KB |
Output is correct |
2 |
Correct |
513 ms |
47556 KB |
Output is correct |
3 |
Correct |
49 ms |
9592 KB |
Output is correct |
4 |
Correct |
424 ms |
19832 KB |
Output is correct |
5 |
Correct |
532 ms |
49396 KB |
Output is correct |
6 |
Correct |
487 ms |
34120 KB |
Output is correct |
7 |
Correct |
382 ms |
19616 KB |
Output is correct |
8 |
Correct |
434 ms |
25768 KB |
Output is correct |
9 |
Correct |
416 ms |
23928 KB |
Output is correct |
10 |
Correct |
562 ms |
21888 KB |
Output is correct |
11 |
Correct |
361 ms |
21468 KB |
Output is correct |
12 |
Correct |
480 ms |
43384 KB |
Output is correct |
13 |
Correct |
501 ms |
31916 KB |
Output is correct |
14 |
Correct |
447 ms |
32512 KB |
Output is correct |
15 |
Correct |
251 ms |
22888 KB |
Output is correct |
16 |
Correct |
464 ms |
40156 KB |
Output is correct |
17 |
Correct |
452 ms |
34488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5368 KB |
Output is correct |
4 |
Correct |
8 ms |
5368 KB |
Output is correct |
5 |
Correct |
8 ms |
5368 KB |
Output is correct |
6 |
Correct |
8 ms |
5496 KB |
Output is correct |
7 |
Correct |
8 ms |
5368 KB |
Output is correct |
8 |
Correct |
8 ms |
5240 KB |
Output is correct |
9 |
Correct |
8 ms |
5240 KB |
Output is correct |
10 |
Correct |
8 ms |
5240 KB |
Output is correct |
11 |
Correct |
8 ms |
5240 KB |
Output is correct |
12 |
Correct |
8 ms |
5240 KB |
Output is correct |
13 |
Correct |
8 ms |
5496 KB |
Output is correct |
14 |
Correct |
8 ms |
5368 KB |
Output is correct |
15 |
Correct |
8 ms |
5368 KB |
Output is correct |
16 |
Correct |
7 ms |
5240 KB |
Output is correct |
17 |
Correct |
8 ms |
5428 KB |
Output is correct |
18 |
Correct |
8 ms |
5368 KB |
Output is correct |
19 |
Correct |
8 ms |
5240 KB |
Output is correct |
20 |
Correct |
8 ms |
5496 KB |
Output is correct |
21 |
Correct |
8 ms |
5368 KB |
Output is correct |
22 |
Correct |
8 ms |
5240 KB |
Output is correct |
23 |
Correct |
8 ms |
5240 KB |
Output is correct |
24 |
Correct |
8 ms |
5244 KB |
Output is correct |
25 |
Correct |
8 ms |
5240 KB |
Output is correct |
26 |
Correct |
9 ms |
5240 KB |
Output is correct |
27 |
Correct |
10 ms |
5496 KB |
Output is correct |
28 |
Correct |
9 ms |
5368 KB |
Output is correct |
29 |
Correct |
8 ms |
5368 KB |
Output is correct |
30 |
Correct |
7 ms |
5240 KB |
Output is correct |
31 |
Correct |
9 ms |
5496 KB |
Output is correct |
32 |
Correct |
9 ms |
5368 KB |
Output is correct |
33 |
Correct |
203 ms |
12536 KB |
Output is correct |
34 |
Correct |
275 ms |
28924 KB |
Output is correct |
35 |
Correct |
42 ms |
8952 KB |
Output is correct |
36 |
Correct |
402 ms |
17912 KB |
Output is correct |
37 |
Correct |
563 ms |
46840 KB |
Output is correct |
38 |
Correct |
512 ms |
32232 KB |
Output is correct |
39 |
Correct |
408 ms |
18512 KB |
Output is correct |
40 |
Correct |
563 ms |
20984 KB |
Output is correct |
41 |
Correct |
481 ms |
19804 KB |
Output is correct |
42 |
Correct |
435 ms |
19780 KB |
Output is correct |
43 |
Correct |
320 ms |
21800 KB |
Output is correct |
44 |
Correct |
510 ms |
36088 KB |
Output is correct |
45 |
Correct |
462 ms |
32368 KB |
Output is correct |
46 |
Correct |
474 ms |
31524 KB |
Output is correct |
47 |
Correct |
253 ms |
21692 KB |
Output is correct |
48 |
Correct |
463 ms |
38624 KB |
Output is correct |
49 |
Correct |
473 ms |
32844 KB |
Output is correct |
50 |
Correct |
329 ms |
16488 KB |
Output is correct |
51 |
Correct |
513 ms |
47556 KB |
Output is correct |
52 |
Correct |
49 ms |
9592 KB |
Output is correct |
53 |
Correct |
424 ms |
19832 KB |
Output is correct |
54 |
Correct |
532 ms |
49396 KB |
Output is correct |
55 |
Correct |
487 ms |
34120 KB |
Output is correct |
56 |
Correct |
382 ms |
19616 KB |
Output is correct |
57 |
Correct |
434 ms |
25768 KB |
Output is correct |
58 |
Correct |
416 ms |
23928 KB |
Output is correct |
59 |
Correct |
562 ms |
21888 KB |
Output is correct |
60 |
Correct |
361 ms |
21468 KB |
Output is correct |
61 |
Correct |
480 ms |
43384 KB |
Output is correct |
62 |
Correct |
501 ms |
31916 KB |
Output is correct |
63 |
Correct |
447 ms |
32512 KB |
Output is correct |
64 |
Correct |
251 ms |
22888 KB |
Output is correct |
65 |
Correct |
464 ms |
40156 KB |
Output is correct |
66 |
Correct |
452 ms |
34488 KB |
Output is correct |
67 |
Correct |
40 ms |
6980 KB |
Output is correct |
68 |
Correct |
164 ms |
23928 KB |
Output is correct |
69 |
Correct |
266 ms |
22540 KB |
Output is correct |
70 |
Correct |
452 ms |
18040 KB |
Output is correct |
71 |
Correct |
567 ms |
47036 KB |
Output is correct |
72 |
Correct |
510 ms |
32168 KB |
Output is correct |
73 |
Correct |
373 ms |
18168 KB |
Output is correct |
74 |
Correct |
413 ms |
23616 KB |
Output is correct |
75 |
Correct |
427 ms |
20756 KB |
Output is correct |
76 |
Correct |
423 ms |
20044 KB |
Output is correct |
77 |
Correct |
324 ms |
19672 KB |
Output is correct |
78 |
Correct |
494 ms |
39032 KB |
Output is correct |
79 |
Correct |
479 ms |
35920 KB |
Output is correct |
80 |
Correct |
416 ms |
29764 KB |
Output is correct |
81 |
Correct |
237 ms |
21756 KB |
Output is correct |
82 |
Correct |
432 ms |
38620 KB |
Output is correct |
83 |
Correct |
441 ms |
32976 KB |
Output is correct |
84 |
Correct |
413 ms |
18300 KB |
Output is correct |
85 |
Correct |
539 ms |
47480 KB |
Output is correct |
86 |
Correct |
491 ms |
32556 KB |
Output is correct |
87 |
Correct |
391 ms |
18448 KB |
Output is correct |
88 |
Correct |
425 ms |
24424 KB |
Output is correct |
89 |
Correct |
449 ms |
21496 KB |
Output is correct |
90 |
Correct |
458 ms |
20604 KB |
Output is correct |
91 |
Correct |
352 ms |
20328 KB |
Output is correct |
92 |
Correct |
489 ms |
46500 KB |
Output is correct |
93 |
Correct |
413 ms |
28436 KB |
Output is correct |
94 |
Correct |
414 ms |
25520 KB |
Output is correct |
95 |
Correct |
252 ms |
22076 KB |
Output is correct |
96 |
Correct |
441 ms |
38872 KB |
Output is correct |
97 |
Correct |
505 ms |
33256 KB |
Output is correct |
98 |
Correct |
447 ms |
19700 KB |
Output is correct |
99 |
Correct |
566 ms |
47864 KB |
Output is correct |
100 |
Correct |
526 ms |
33868 KB |
Output is correct |
101 |
Correct |
420 ms |
19256 KB |
Output is correct |
102 |
Correct |
447 ms |
23772 KB |
Output is correct |
103 |
Correct |
435 ms |
21880 KB |
Output is correct |
104 |
Correct |
414 ms |
20984 KB |
Output is correct |
105 |
Correct |
342 ms |
20748 KB |
Output is correct |
106 |
Correct |
480 ms |
35576 KB |
Output is correct |
107 |
Correct |
463 ms |
35984 KB |
Output is correct |
108 |
Correct |
464 ms |
28168 KB |
Output is correct |
109 |
Correct |
303 ms |
22728 KB |
Output is correct |
110 |
Correct |
452 ms |
39484 KB |
Output is correct |
111 |
Correct |
457 ms |
34112 KB |
Output is correct |