#include<bits/stdc++.h>
using namespace std;
const int nmax=2e5+42;
int n,m;
vector<int> adj[nmax];
int colour[nmax];
int dist[nmax];
void dfs_dist(int node,int par,int d)
{
dist[node]=d;
for(auto k:adj[node])
if(k!=par)dfs_dist(k,node,d+1);
}
int farthest(int node)
{
dfs_dist(node,node,0);
int ret=1;
for(int i=1;i<=n;i++)
if(dist[ret]<dist[i])ret=i;
return ret;
}
int depth[nmax],height[nmax];
void dfs_depth(int node,int par)
{
height[node]=height[par]+1;
for(auto k:adj[node])
if(k!=par)
{
dfs_depth(k,node);
depth[node]=max(depth[node],depth[k]);
}
depth[node]++;
}
int seen[nmax],diff=0;
int outp[nmax];
stack<int> active;
void my_push(int node)
{
if(seen[colour[node]]==0)diff++;
seen[colour[node]]++;
active.push(node);
}
void my_pop()
{
if(seen[colour[active.top()]]==1)diff--;
seen[colour[active.top()]]--;
active.pop();
}
bool cmp(pair<int/*depth*/,int/*node*/> a,pair<int/*depth*/,int/*node*/> b)
{
return a.first>b.first;
}
void print(stack<int> s)
{
while(s.size())
{
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
}
void dfs_solve(int node,int par)
{
if(par!=node)
{
my_push(par);
}
vector< pair<int/*depth*/,int/*node*/> > to={};
for(auto k:adj[node])
if(k!=par)to.push_back({depth[k],k});
sort(to.begin(),to.end(),cmp);
/*
cout<<node<<" -> "<<active.size()<<" "<<diff<<endl;
for(int i=1;i<=n;i++)cout<<seen[i]<<" ";cout<<endl;
print(active);
cout<<"---"<<endl;
*/
for(auto k:to)
{
int mx_depth=0;
if(k==to[0])
{
if(to.size()>1)mx_depth=to[1].first;
}
else mx_depth=to[0].first;
//cout<<node<<" "<<par<<" "<<k.first<<" "<<k.second<<" "<<active.size()<<endl;
while(active.size()>=1&&height[k.second]-height[active.top()]<=mx_depth+1)my_pop();
//cout<<"became "<<active.size()<<endl;
dfs_solve(k.second,node);
}
if(active.size()&&active.top()==node)my_pop();
while(active.size()&&height[node]-height[active.top()]<depth[node])my_pop();
/*
cout<<"ENDING "<<endl;
cout<<node<<" -> "<<active.size()<<" "<<diff<<endl;
for(int i=1;i<=n;i++)cout<<seen[i]<<" ";cout<<endl;
print(active);
cout<<"---"<<endl;
*/
outp[node]=max(outp[node],diff);
}
void solve(int node)
{
memset(depth,0,sizeof(depth));
memset(height,0,sizeof(height));
//cout<<"\tsolve "<<node<<endl;
dfs_depth(node,node);
dfs_solve(node,node);
}
int main()
{
scanf("%i%i",&n,&m);
for(int i=1;i<n;i++)
{
int u,v;
scanf("%i%i",&u,&v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=1;i<=n;i++)scanf("%i",&colour[i]);
int u=farthest(1);
int v=farthest(u);
solve(u);
solve(v);
for(int i=1;i<=n;i++)printf("%i\n",outp[i]);
return 0;
}
Compilation message
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:146:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
146 | scanf("%i%i",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp:151:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
151 | scanf("%i%i",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp:156:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
156 | for(int i=1;i<=n;i++)scanf("%i",&colour[i]);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
6656 KB |
Output is correct |
2 |
Correct |
6 ms |
6656 KB |
Output is correct |
3 |
Correct |
6 ms |
6784 KB |
Output is correct |
4 |
Correct |
7 ms |
6912 KB |
Output is correct |
5 |
Correct |
6 ms |
6656 KB |
Output is correct |
6 |
Correct |
7 ms |
7040 KB |
Output is correct |
7 |
Correct |
7 ms |
6912 KB |
Output is correct |
8 |
Correct |
7 ms |
6656 KB |
Output is correct |
9 |
Correct |
6 ms |
6784 KB |
Output is correct |
10 |
Correct |
7 ms |
6784 KB |
Output is correct |
11 |
Correct |
6 ms |
6784 KB |
Output is correct |
12 |
Correct |
6 ms |
6784 KB |
Output is correct |
13 |
Correct |
9 ms |
6984 KB |
Output is correct |
14 |
Correct |
7 ms |
6784 KB |
Output is correct |
15 |
Correct |
7 ms |
6784 KB |
Output is correct |
16 |
Correct |
6 ms |
6784 KB |
Output is correct |
17 |
Correct |
6 ms |
6912 KB |
Output is correct |
18 |
Correct |
6 ms |
6912 KB |
Output is correct |
19 |
Correct |
6 ms |
6656 KB |
Output is correct |
20 |
Correct |
7 ms |
7040 KB |
Output is correct |
21 |
Correct |
7 ms |
6912 KB |
Output is correct |
22 |
Correct |
7 ms |
6656 KB |
Output is correct |
23 |
Correct |
7 ms |
6784 KB |
Output is correct |
24 |
Correct |
7 ms |
6784 KB |
Output is correct |
25 |
Correct |
7 ms |
6784 KB |
Output is correct |
26 |
Correct |
6 ms |
6784 KB |
Output is correct |
27 |
Correct |
7 ms |
7040 KB |
Output is correct |
28 |
Correct |
6 ms |
6912 KB |
Output is correct |
29 |
Correct |
7 ms |
6912 KB |
Output is correct |
30 |
Correct |
6 ms |
6784 KB |
Output is correct |
31 |
Correct |
7 ms |
6912 KB |
Output is correct |
32 |
Correct |
6 ms |
6912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
199 ms |
13500 KB |
Output is correct |
2 |
Correct |
276 ms |
31828 KB |
Output is correct |
3 |
Correct |
40 ms |
10488 KB |
Output is correct |
4 |
Correct |
464 ms |
18628 KB |
Output is correct |
5 |
Correct |
573 ms |
50840 KB |
Output is correct |
6 |
Correct |
550 ms |
34356 KB |
Output is correct |
7 |
Correct |
454 ms |
19120 KB |
Output is correct |
8 |
Correct |
495 ms |
22008 KB |
Output is correct |
9 |
Correct |
527 ms |
20828 KB |
Output is correct |
10 |
Correct |
491 ms |
20672 KB |
Output is correct |
11 |
Correct |
273 ms |
22440 KB |
Output is correct |
12 |
Correct |
516 ms |
38556 KB |
Output is correct |
13 |
Correct |
489 ms |
34544 KB |
Output is correct |
14 |
Correct |
493 ms |
33672 KB |
Output is correct |
15 |
Correct |
263 ms |
22476 KB |
Output is correct |
16 |
Correct |
493 ms |
41380 KB |
Output is correct |
17 |
Correct |
606 ms |
35060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
349 ms |
17444 KB |
Output is correct |
2 |
Correct |
714 ms |
51192 KB |
Output is correct |
3 |
Correct |
45 ms |
11260 KB |
Output is correct |
4 |
Correct |
586 ms |
20344 KB |
Output is correct |
5 |
Correct |
566 ms |
53368 KB |
Output is correct |
6 |
Correct |
545 ms |
36296 KB |
Output is correct |
7 |
Correct |
463 ms |
20388 KB |
Output is correct |
8 |
Correct |
582 ms |
27128 KB |
Output is correct |
9 |
Correct |
459 ms |
24832 KB |
Output is correct |
10 |
Correct |
518 ms |
22716 KB |
Output is correct |
11 |
Correct |
506 ms |
22184 KB |
Output is correct |
12 |
Correct |
605 ms |
46584 KB |
Output is correct |
13 |
Correct |
666 ms |
34044 KB |
Output is correct |
14 |
Correct |
524 ms |
34584 KB |
Output is correct |
15 |
Correct |
294 ms |
23584 KB |
Output is correct |
16 |
Correct |
602 ms |
43100 KB |
Output is correct |
17 |
Correct |
703 ms |
36840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
6656 KB |
Output is correct |
2 |
Correct |
6 ms |
6656 KB |
Output is correct |
3 |
Correct |
6 ms |
6784 KB |
Output is correct |
4 |
Correct |
7 ms |
6912 KB |
Output is correct |
5 |
Correct |
6 ms |
6656 KB |
Output is correct |
6 |
Correct |
7 ms |
7040 KB |
Output is correct |
7 |
Correct |
7 ms |
6912 KB |
Output is correct |
8 |
Correct |
7 ms |
6656 KB |
Output is correct |
9 |
Correct |
6 ms |
6784 KB |
Output is correct |
10 |
Correct |
7 ms |
6784 KB |
Output is correct |
11 |
Correct |
6 ms |
6784 KB |
Output is correct |
12 |
Correct |
6 ms |
6784 KB |
Output is correct |
13 |
Correct |
9 ms |
6984 KB |
Output is correct |
14 |
Correct |
7 ms |
6784 KB |
Output is correct |
15 |
Correct |
7 ms |
6784 KB |
Output is correct |
16 |
Correct |
6 ms |
6784 KB |
Output is correct |
17 |
Correct |
6 ms |
6912 KB |
Output is correct |
18 |
Correct |
6 ms |
6912 KB |
Output is correct |
19 |
Correct |
6 ms |
6656 KB |
Output is correct |
20 |
Correct |
7 ms |
7040 KB |
Output is correct |
21 |
Correct |
7 ms |
6912 KB |
Output is correct |
22 |
Correct |
7 ms |
6656 KB |
Output is correct |
23 |
Correct |
7 ms |
6784 KB |
Output is correct |
24 |
Correct |
7 ms |
6784 KB |
Output is correct |
25 |
Correct |
7 ms |
6784 KB |
Output is correct |
26 |
Correct |
6 ms |
6784 KB |
Output is correct |
27 |
Correct |
7 ms |
7040 KB |
Output is correct |
28 |
Correct |
6 ms |
6912 KB |
Output is correct |
29 |
Correct |
7 ms |
6912 KB |
Output is correct |
30 |
Correct |
6 ms |
6784 KB |
Output is correct |
31 |
Correct |
7 ms |
6912 KB |
Output is correct |
32 |
Correct |
6 ms |
6912 KB |
Output is correct |
33 |
Correct |
199 ms |
13500 KB |
Output is correct |
34 |
Correct |
276 ms |
31828 KB |
Output is correct |
35 |
Correct |
40 ms |
10488 KB |
Output is correct |
36 |
Correct |
464 ms |
18628 KB |
Output is correct |
37 |
Correct |
573 ms |
50840 KB |
Output is correct |
38 |
Correct |
550 ms |
34356 KB |
Output is correct |
39 |
Correct |
454 ms |
19120 KB |
Output is correct |
40 |
Correct |
495 ms |
22008 KB |
Output is correct |
41 |
Correct |
527 ms |
20828 KB |
Output is correct |
42 |
Correct |
491 ms |
20672 KB |
Output is correct |
43 |
Correct |
273 ms |
22440 KB |
Output is correct |
44 |
Correct |
516 ms |
38556 KB |
Output is correct |
45 |
Correct |
489 ms |
34544 KB |
Output is correct |
46 |
Correct |
493 ms |
33672 KB |
Output is correct |
47 |
Correct |
263 ms |
22476 KB |
Output is correct |
48 |
Correct |
493 ms |
41380 KB |
Output is correct |
49 |
Correct |
606 ms |
35060 KB |
Output is correct |
50 |
Correct |
349 ms |
17444 KB |
Output is correct |
51 |
Correct |
714 ms |
51192 KB |
Output is correct |
52 |
Correct |
45 ms |
11260 KB |
Output is correct |
53 |
Correct |
586 ms |
20344 KB |
Output is correct |
54 |
Correct |
566 ms |
53368 KB |
Output is correct |
55 |
Correct |
545 ms |
36296 KB |
Output is correct |
56 |
Correct |
463 ms |
20388 KB |
Output is correct |
57 |
Correct |
582 ms |
27128 KB |
Output is correct |
58 |
Correct |
459 ms |
24832 KB |
Output is correct |
59 |
Correct |
518 ms |
22716 KB |
Output is correct |
60 |
Correct |
506 ms |
22184 KB |
Output is correct |
61 |
Correct |
605 ms |
46584 KB |
Output is correct |
62 |
Correct |
666 ms |
34044 KB |
Output is correct |
63 |
Correct |
524 ms |
34584 KB |
Output is correct |
64 |
Correct |
294 ms |
23584 KB |
Output is correct |
65 |
Correct |
602 ms |
43100 KB |
Output is correct |
66 |
Correct |
703 ms |
36840 KB |
Output is correct |
67 |
Correct |
42 ms |
8440 KB |
Output is correct |
68 |
Correct |
186 ms |
26360 KB |
Output is correct |
69 |
Correct |
489 ms |
24440 KB |
Output is correct |
70 |
Correct |
552 ms |
18808 KB |
Output is correct |
71 |
Correct |
617 ms |
50680 KB |
Output is correct |
72 |
Correct |
711 ms |
34484 KB |
Output is correct |
73 |
Correct |
575 ms |
18768 KB |
Output is correct |
74 |
Correct |
592 ms |
24960 KB |
Output is correct |
75 |
Correct |
652 ms |
21500 KB |
Output is correct |
76 |
Correct |
503 ms |
20928 KB |
Output is correct |
77 |
Correct |
391 ms |
20440 KB |
Output is correct |
78 |
Correct |
560 ms |
42004 KB |
Output is correct |
79 |
Correct |
480 ms |
38424 KB |
Output is correct |
80 |
Correct |
546 ms |
31924 KB |
Output is correct |
81 |
Correct |
230 ms |
22220 KB |
Output is correct |
82 |
Correct |
582 ms |
41432 KB |
Output is correct |
83 |
Correct |
483 ms |
35044 KB |
Output is correct |
84 |
Correct |
475 ms |
19064 KB |
Output is correct |
85 |
Correct |
724 ms |
51448 KB |
Output is correct |
86 |
Correct |
530 ms |
34888 KB |
Output is correct |
87 |
Correct |
436 ms |
19216 KB |
Output is correct |
88 |
Correct |
447 ms |
25724 KB |
Output is correct |
89 |
Correct |
534 ms |
22736 KB |
Output is correct |
90 |
Correct |
552 ms |
21604 KB |
Output is correct |
91 |
Correct |
350 ms |
20844 KB |
Output is correct |
92 |
Correct |
580 ms |
50296 KB |
Output is correct |
93 |
Correct |
524 ms |
30288 KB |
Output is correct |
94 |
Correct |
490 ms |
27048 KB |
Output is correct |
95 |
Correct |
243 ms |
22732 KB |
Output is correct |
96 |
Correct |
464 ms |
41724 KB |
Output is correct |
97 |
Correct |
459 ms |
35432 KB |
Output is correct |
98 |
Correct |
452 ms |
20472 KB |
Output is correct |
99 |
Correct |
548 ms |
51724 KB |
Output is correct |
100 |
Correct |
533 ms |
36008 KB |
Output is correct |
101 |
Correct |
410 ms |
20028 KB |
Output is correct |
102 |
Correct |
465 ms |
24824 KB |
Output is correct |
103 |
Correct |
440 ms |
22820 KB |
Output is correct |
104 |
Correct |
445 ms |
22040 KB |
Output is correct |
105 |
Correct |
323 ms |
21392 KB |
Output is correct |
106 |
Correct |
524 ms |
37880 KB |
Output is correct |
107 |
Correct |
478 ms |
38444 KB |
Output is correct |
108 |
Correct |
496 ms |
29712 KB |
Output is correct |
109 |
Correct |
245 ms |
23616 KB |
Output is correct |
110 |
Correct |
475 ms |
42204 KB |
Output is correct |
111 |
Correct |
494 ms |
36400 KB |
Output is correct |