#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define ppb pop_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
#define maxn 200005
int n,m,rt=1,a,b,c[maxn],num[maxn],ans[maxn],d[maxn],f[maxn],l[maxn][2],cnt;
vi s,AL[maxn];
void dfs1(int u,int p){ //find furthest
for(int v:AL[u]){
if(v==p)continue;
d[v]=d[u]+1;
if(d[v]>d[rt])rt=v;
dfs1(v,u);
}
}
void dfs2(int u,int p){ //find heavy child
l[u][0]=l[u][1]=0;
for(int v:AL[u]){
if(v==p)continue;
d[v]=d[u]+1;
dfs2(v,u);
if(f[v]>f[l[u][1]])l[u][1]=v;
if(f[l[u][1]]>f[l[u][0]])swap(l[u][0],l[u][1]);
}
f[u]=f[l[u][0]]+1;
}
void add(int i){
if(num[c[i]]==0)++cnt;
++num[c[i]];
}
void rem(int i){
if(num[c[i]]==1)--cnt;
--num[c[i]];
}
void dfs3(int u,int p){
if(p)add(p),s.pb(p);
if(l[u][0]){
while(!s.empty()&&d[s.back()]>=d[u]-f[l[u][1]]){
rem(s.back());s.ppb();
}
dfs3(l[u][0],u);
}
while(!s.empty()&&d[s.back()]>=d[u]-f[l[u][0]]){
rem(s.back());s.ppb();
}
for(int v:AL[u]){
if(v!=p&&v!=l[u][0])dfs3(v,u);
}
mxto(ans[u],cnt);
if(!s.empty()&&s.back()==p){
rem(s.back());s.ppb();
}
}
void idk(int x){
d[x]=0;
dfs1(x,0);
d[rt]=0;
dfs2(rt,0);
cnt=0;s.clear();
for(int i=1;i<=n;++i)num[i]=0;
dfs3(rt,0);
}
int main(){
sf("%d%d",&n,&m);
for(int i=1;i<n;++i){
sf("%d%d",&a,&b);
AL[a].pb(b);AL[b].pb(a);
}
for(int i=1;i<=n;++i){
sf("%d",&c[i]);
}
idk(1);idk(rt);
for(int i=1;i<=n;++i){
pf("%d\n",ans[i]);
}
}
Compilation message
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:100:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | sf("%d%d",&n,&m);
| ^
joi2019_ho_t5.cpp:102:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | sf("%d%d",&a,&b);
| ^
joi2019_ho_t5.cpp:106:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | sf("%d",&c[i]);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
5 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
5 ms |
5196 KB |
Output is correct |
5 |
Correct |
5 ms |
5068 KB |
Output is correct |
6 |
Correct |
5 ms |
5324 KB |
Output is correct |
7 |
Correct |
5 ms |
5196 KB |
Output is correct |
8 |
Correct |
7 ms |
5068 KB |
Output is correct |
9 |
Correct |
5 ms |
5196 KB |
Output is correct |
10 |
Correct |
5 ms |
5196 KB |
Output is correct |
11 |
Correct |
5 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
5 ms |
5276 KB |
Output is correct |
14 |
Correct |
5 ms |
5196 KB |
Output is correct |
15 |
Correct |
5 ms |
5196 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
5 ms |
5196 KB |
Output is correct |
18 |
Correct |
5 ms |
5196 KB |
Output is correct |
19 |
Correct |
5 ms |
5068 KB |
Output is correct |
20 |
Correct |
5 ms |
5280 KB |
Output is correct |
21 |
Correct |
5 ms |
5180 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
5180 KB |
Output is correct |
24 |
Correct |
5 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5152 KB |
Output is correct |
27 |
Correct |
5 ms |
5324 KB |
Output is correct |
28 |
Correct |
5 ms |
5196 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
4 ms |
5148 KB |
Output is correct |
31 |
Correct |
5 ms |
5324 KB |
Output is correct |
32 |
Correct |
5 ms |
5280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
13652 KB |
Output is correct |
2 |
Correct |
186 ms |
24952 KB |
Output is correct |
3 |
Correct |
36 ms |
8468 KB |
Output is correct |
4 |
Correct |
390 ms |
20240 KB |
Output is correct |
5 |
Correct |
405 ms |
39684 KB |
Output is correct |
6 |
Correct |
418 ms |
29772 KB |
Output is correct |
7 |
Correct |
342 ms |
20344 KB |
Output is correct |
8 |
Correct |
368 ms |
22048 KB |
Output is correct |
9 |
Correct |
360 ms |
21436 KB |
Output is correct |
10 |
Correct |
362 ms |
21372 KB |
Output is correct |
11 |
Correct |
201 ms |
20904 KB |
Output is correct |
12 |
Correct |
397 ms |
32192 KB |
Output is correct |
13 |
Correct |
331 ms |
29620 KB |
Output is correct |
14 |
Correct |
348 ms |
29108 KB |
Output is correct |
15 |
Correct |
177 ms |
20848 KB |
Output is correct |
16 |
Correct |
346 ms |
33496 KB |
Output is correct |
17 |
Correct |
345 ms |
29892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
252 ms |
17564 KB |
Output is correct |
2 |
Correct |
383 ms |
39860 KB |
Output is correct |
3 |
Correct |
35 ms |
9028 KB |
Output is correct |
4 |
Correct |
354 ms |
21076 KB |
Output is correct |
5 |
Correct |
393 ms |
41528 KB |
Output is correct |
6 |
Correct |
388 ms |
30984 KB |
Output is correct |
7 |
Correct |
339 ms |
21060 KB |
Output is correct |
8 |
Correct |
356 ms |
25144 KB |
Output is correct |
9 |
Correct |
357 ms |
23756 KB |
Output is correct |
10 |
Correct |
353 ms |
22660 KB |
Output is correct |
11 |
Correct |
291 ms |
21376 KB |
Output is correct |
12 |
Correct |
385 ms |
37284 KB |
Output is correct |
13 |
Correct |
346 ms |
29632 KB |
Output is correct |
14 |
Correct |
354 ms |
30040 KB |
Output is correct |
15 |
Correct |
207 ms |
21732 KB |
Output is correct |
16 |
Correct |
330 ms |
34868 KB |
Output is correct |
17 |
Correct |
361 ms |
31100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
5 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
5 ms |
5196 KB |
Output is correct |
5 |
Correct |
5 ms |
5068 KB |
Output is correct |
6 |
Correct |
5 ms |
5324 KB |
Output is correct |
7 |
Correct |
5 ms |
5196 KB |
Output is correct |
8 |
Correct |
7 ms |
5068 KB |
Output is correct |
9 |
Correct |
5 ms |
5196 KB |
Output is correct |
10 |
Correct |
5 ms |
5196 KB |
Output is correct |
11 |
Correct |
5 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
5 ms |
5276 KB |
Output is correct |
14 |
Correct |
5 ms |
5196 KB |
Output is correct |
15 |
Correct |
5 ms |
5196 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
5 ms |
5196 KB |
Output is correct |
18 |
Correct |
5 ms |
5196 KB |
Output is correct |
19 |
Correct |
5 ms |
5068 KB |
Output is correct |
20 |
Correct |
5 ms |
5280 KB |
Output is correct |
21 |
Correct |
5 ms |
5180 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
5180 KB |
Output is correct |
24 |
Correct |
5 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5152 KB |
Output is correct |
27 |
Correct |
5 ms |
5324 KB |
Output is correct |
28 |
Correct |
5 ms |
5196 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
4 ms |
5148 KB |
Output is correct |
31 |
Correct |
5 ms |
5324 KB |
Output is correct |
32 |
Correct |
5 ms |
5280 KB |
Output is correct |
33 |
Correct |
165 ms |
13652 KB |
Output is correct |
34 |
Correct |
186 ms |
24952 KB |
Output is correct |
35 |
Correct |
36 ms |
8468 KB |
Output is correct |
36 |
Correct |
390 ms |
20240 KB |
Output is correct |
37 |
Correct |
405 ms |
39684 KB |
Output is correct |
38 |
Correct |
418 ms |
29772 KB |
Output is correct |
39 |
Correct |
342 ms |
20344 KB |
Output is correct |
40 |
Correct |
368 ms |
22048 KB |
Output is correct |
41 |
Correct |
360 ms |
21436 KB |
Output is correct |
42 |
Correct |
362 ms |
21372 KB |
Output is correct |
43 |
Correct |
201 ms |
20904 KB |
Output is correct |
44 |
Correct |
397 ms |
32192 KB |
Output is correct |
45 |
Correct |
331 ms |
29620 KB |
Output is correct |
46 |
Correct |
348 ms |
29108 KB |
Output is correct |
47 |
Correct |
177 ms |
20848 KB |
Output is correct |
48 |
Correct |
346 ms |
33496 KB |
Output is correct |
49 |
Correct |
345 ms |
29892 KB |
Output is correct |
50 |
Correct |
252 ms |
17564 KB |
Output is correct |
51 |
Correct |
383 ms |
39860 KB |
Output is correct |
52 |
Correct |
35 ms |
9028 KB |
Output is correct |
53 |
Correct |
354 ms |
21076 KB |
Output is correct |
54 |
Correct |
393 ms |
41528 KB |
Output is correct |
55 |
Correct |
388 ms |
30984 KB |
Output is correct |
56 |
Correct |
339 ms |
21060 KB |
Output is correct |
57 |
Correct |
356 ms |
25144 KB |
Output is correct |
58 |
Correct |
357 ms |
23756 KB |
Output is correct |
59 |
Correct |
353 ms |
22660 KB |
Output is correct |
60 |
Correct |
291 ms |
21376 KB |
Output is correct |
61 |
Correct |
385 ms |
37284 KB |
Output is correct |
62 |
Correct |
346 ms |
29632 KB |
Output is correct |
63 |
Correct |
354 ms |
30040 KB |
Output is correct |
64 |
Correct |
207 ms |
21732 KB |
Output is correct |
65 |
Correct |
330 ms |
34868 KB |
Output is correct |
66 |
Correct |
361 ms |
31100 KB |
Output is correct |
67 |
Correct |
28 ms |
7080 KB |
Output is correct |
68 |
Correct |
127 ms |
20604 KB |
Output is correct |
69 |
Correct |
234 ms |
20856 KB |
Output is correct |
70 |
Correct |
384 ms |
20236 KB |
Output is correct |
71 |
Correct |
395 ms |
39728 KB |
Output is correct |
72 |
Correct |
426 ms |
29772 KB |
Output is correct |
73 |
Correct |
368 ms |
20292 KB |
Output is correct |
74 |
Correct |
374 ms |
23744 KB |
Output is correct |
75 |
Correct |
346 ms |
21900 KB |
Output is correct |
76 |
Correct |
341 ms |
21568 KB |
Output is correct |
77 |
Correct |
277 ms |
20496 KB |
Output is correct |
78 |
Correct |
376 ms |
34268 KB |
Output is correct |
79 |
Correct |
357 ms |
32280 KB |
Output is correct |
80 |
Correct |
342 ms |
27884 KB |
Output is correct |
81 |
Correct |
176 ms |
20616 KB |
Output is correct |
82 |
Correct |
360 ms |
33444 KB |
Output is correct |
83 |
Correct |
331 ms |
29912 KB |
Output is correct |
84 |
Correct |
342 ms |
20420 KB |
Output is correct |
85 |
Correct |
395 ms |
40560 KB |
Output is correct |
86 |
Correct |
361 ms |
30184 KB |
Output is correct |
87 |
Correct |
329 ms |
20364 KB |
Output is correct |
88 |
Correct |
361 ms |
24496 KB |
Output is correct |
89 |
Correct |
347 ms |
22604 KB |
Output is correct |
90 |
Correct |
340 ms |
22108 KB |
Output is correct |
91 |
Correct |
274 ms |
20860 KB |
Output is correct |
92 |
Correct |
391 ms |
39660 KB |
Output is correct |
93 |
Correct |
348 ms |
27256 KB |
Output is correct |
94 |
Correct |
361 ms |
25412 KB |
Output is correct |
95 |
Correct |
179 ms |
21004 KB |
Output is correct |
96 |
Correct |
353 ms |
34132 KB |
Output is correct |
97 |
Correct |
369 ms |
30392 KB |
Output is correct |
98 |
Correct |
357 ms |
21084 KB |
Output is correct |
99 |
Correct |
413 ms |
40840 KB |
Output is correct |
100 |
Correct |
402 ms |
30868 KB |
Output is correct |
101 |
Correct |
323 ms |
20952 KB |
Output is correct |
102 |
Correct |
366 ms |
24124 KB |
Output is correct |
103 |
Correct |
348 ms |
22724 KB |
Output is correct |
104 |
Correct |
347 ms |
22340 KB |
Output is correct |
105 |
Correct |
270 ms |
21192 KB |
Output is correct |
106 |
Correct |
399 ms |
32356 KB |
Output is correct |
107 |
Correct |
384 ms |
32696 KB |
Output is correct |
108 |
Correct |
396 ms |
27028 KB |
Output is correct |
109 |
Correct |
186 ms |
21688 KB |
Output is correct |
110 |
Correct |
374 ms |
34800 KB |
Output is correct |
111 |
Correct |
338 ms |
30984 KB |
Output is correct |