#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define ii pair<int,int>
#define ll long long
#define umin(x,y) x=min(x,y)
#define umax(x,y) x=max(x,y)
#define orta ((bas+son)>>1)
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define inf 2000000000
#define N 200005
#define MOD 998244353
using namespace std;
int n,m;
int mxd[N][2],der[N];
int ans[N],c[N],deep[N];
int s[N];
int sz;
int tot_op;
int cntd;
int cnt[N];
vector<int> v[N];
struct diameter_finder {
int l,r;
int mx;
void dfs(int node,int ata,int len) {
if(len>mx) {
mx=len;
l=node;
}
for(int i:v[node]) {
if(i==ata) continue ;
dfs(i,node,len+1);
}
}
void find() {
mx=0;
dfs(1,0,0);
r=l;
mx=0;
dfs(r,0,0);
}
} dia;
void add(int node) {
s[++sz]=node;
// ++tot_op;
cntd+=(cnt[c[node]]==0);
cnt[c[node]]++;
}
void del(int cur) {
while(sz && cur<=der[s[sz]]) {
//++tot_op;
cnt[c[s[sz]]]--;
cntd-=(cnt[c[s[sz]]]==0);
--sz;
}
}
void upd(int node,int nd) {
if(mxd[node][1]<nd) mxd[node][1]=nd;
if(mxd[node][1]>mxd[node][0]) swap(mxd[node][1],mxd[node][0]);
}
void dfs(int node,int ata) {
del(2*der[node]-mxd[node][1]);
add(node);
for(int i:v[node]) {
if(i!=deep[node]) continue ;
dfs(i,node);
}
del(2*der[node]-mxd[node][0]);
umax(ans[node],cntd);
for(int i:v[node]) {
if(i==ata || i==deep[node]) continue ;
if(!sz || s[sz]!=node) add(node);
dfs(i,node);
}
del(der[node]);
}
void dfs1(int node,int ata) {
der[node]=der[ata]+1;
mxd[node][0]=mxd[node][1]=der[node];
deep[node]=node;
for(int i:v[node]) {
if(i==ata) continue ;
dfs1(i,node);
upd(node,mxd[i][0]);
if(mxd[node][0]==mxd[i][0]) {
deep[node]=i;
}
}
}
int main() {
scanf("%d %d",&n,&m);
for(int i=1;i<n;i++) {
int x,y;
scanf("%d %d",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
for(int i=1;i<=n;i++) scanf("%d",c+i);
dia.find();
// cerr << "root is : " << dia.l << '\n';
dfs1(dia.l,0);
dfs(dia.l,0);
// cerr << "root is : " << dia.r << '\n';
dfs1(dia.r,0);
dfs(dia.r,0);
for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
// cerr << tot_op << '\n';
}
Compilation message
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:164:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:170:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&x,&y);
~~~~~^~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:177: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("%d",c+i);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5240 KB |
Output is correct |
4 |
Correct |
8 ms |
5240 KB |
Output is correct |
5 |
Correct |
8 ms |
5112 KB |
Output is correct |
6 |
Correct |
11 ms |
5368 KB |
Output is correct |
7 |
Correct |
11 ms |
5240 KB |
Output is correct |
8 |
Correct |
10 ms |
5112 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 |
5368 KB |
Output is correct |
14 |
Correct |
8 ms |
5240 KB |
Output is correct |
15 |
Correct |
8 ms |
5240 KB |
Output is correct |
16 |
Correct |
7 ms |
5116 KB |
Output is correct |
17 |
Correct |
8 ms |
5240 KB |
Output is correct |
18 |
Correct |
8 ms |
5240 KB |
Output is correct |
19 |
Correct |
10 ms |
5112 KB |
Output is correct |
20 |
Correct |
8 ms |
5368 KB |
Output is correct |
21 |
Correct |
8 ms |
5240 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 |
5240 KB |
Output is correct |
25 |
Correct |
8 ms |
5240 KB |
Output is correct |
26 |
Correct |
7 ms |
5240 KB |
Output is correct |
27 |
Correct |
10 ms |
5368 KB |
Output is correct |
28 |
Correct |
8 ms |
5240 KB |
Output is correct |
29 |
Correct |
8 ms |
5240 KB |
Output is correct |
30 |
Correct |
8 ms |
5240 KB |
Output is correct |
31 |
Correct |
8 ms |
5240 KB |
Output is correct |
32 |
Correct |
8 ms |
5240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
11624 KB |
Output is correct |
2 |
Correct |
237 ms |
22524 KB |
Output is correct |
3 |
Correct |
38 ms |
8184 KB |
Output is correct |
4 |
Correct |
396 ms |
19400 KB |
Output is correct |
5 |
Correct |
520 ms |
35704 KB |
Output is correct |
6 |
Correct |
478 ms |
27376 KB |
Output is correct |
7 |
Correct |
348 ms |
19500 KB |
Output is correct |
8 |
Correct |
404 ms |
21148 KB |
Output is correct |
9 |
Correct |
423 ms |
20740 KB |
Output is correct |
10 |
Correct |
413 ms |
20472 KB |
Output is correct |
11 |
Correct |
213 ms |
20072 KB |
Output is correct |
12 |
Correct |
480 ms |
29396 KB |
Output is correct |
13 |
Correct |
393 ms |
27508 KB |
Output is correct |
14 |
Correct |
429 ms |
26768 KB |
Output is correct |
15 |
Correct |
193 ms |
20076 KB |
Output is correct |
16 |
Correct |
386 ms |
30704 KB |
Output is correct |
17 |
Correct |
413 ms |
27380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
259 ms |
14736 KB |
Output is correct |
2 |
Correct |
496 ms |
36916 KB |
Output is correct |
3 |
Correct |
44 ms |
8824 KB |
Output is correct |
4 |
Correct |
393 ms |
21144 KB |
Output is correct |
5 |
Correct |
488 ms |
38264 KB |
Output is correct |
6 |
Correct |
478 ms |
29308 KB |
Output is correct |
7 |
Correct |
359 ms |
21172 KB |
Output is correct |
8 |
Correct |
433 ms |
24672 KB |
Output is correct |
9 |
Correct |
392 ms |
23416 KB |
Output is correct |
10 |
Correct |
375 ms |
22396 KB |
Output is correct |
11 |
Correct |
310 ms |
21364 KB |
Output is correct |
12 |
Correct |
447 ms |
34808 KB |
Output is correct |
13 |
Correct |
406 ms |
28532 KB |
Output is correct |
14 |
Correct |
423 ms |
28400 KB |
Output is correct |
15 |
Correct |
208 ms |
21872 KB |
Output is correct |
16 |
Correct |
425 ms |
32876 KB |
Output is correct |
17 |
Correct |
427 ms |
29588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5240 KB |
Output is correct |
4 |
Correct |
8 ms |
5240 KB |
Output is correct |
5 |
Correct |
8 ms |
5112 KB |
Output is correct |
6 |
Correct |
11 ms |
5368 KB |
Output is correct |
7 |
Correct |
11 ms |
5240 KB |
Output is correct |
8 |
Correct |
10 ms |
5112 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 |
5368 KB |
Output is correct |
14 |
Correct |
8 ms |
5240 KB |
Output is correct |
15 |
Correct |
8 ms |
5240 KB |
Output is correct |
16 |
Correct |
7 ms |
5116 KB |
Output is correct |
17 |
Correct |
8 ms |
5240 KB |
Output is correct |
18 |
Correct |
8 ms |
5240 KB |
Output is correct |
19 |
Correct |
10 ms |
5112 KB |
Output is correct |
20 |
Correct |
8 ms |
5368 KB |
Output is correct |
21 |
Correct |
8 ms |
5240 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 |
5240 KB |
Output is correct |
25 |
Correct |
8 ms |
5240 KB |
Output is correct |
26 |
Correct |
7 ms |
5240 KB |
Output is correct |
27 |
Correct |
10 ms |
5368 KB |
Output is correct |
28 |
Correct |
8 ms |
5240 KB |
Output is correct |
29 |
Correct |
8 ms |
5240 KB |
Output is correct |
30 |
Correct |
8 ms |
5240 KB |
Output is correct |
31 |
Correct |
8 ms |
5240 KB |
Output is correct |
32 |
Correct |
8 ms |
5240 KB |
Output is correct |
33 |
Correct |
187 ms |
11624 KB |
Output is correct |
34 |
Correct |
237 ms |
22524 KB |
Output is correct |
35 |
Correct |
38 ms |
8184 KB |
Output is correct |
36 |
Correct |
396 ms |
19400 KB |
Output is correct |
37 |
Correct |
520 ms |
35704 KB |
Output is correct |
38 |
Correct |
478 ms |
27376 KB |
Output is correct |
39 |
Correct |
348 ms |
19500 KB |
Output is correct |
40 |
Correct |
404 ms |
21148 KB |
Output is correct |
41 |
Correct |
423 ms |
20740 KB |
Output is correct |
42 |
Correct |
413 ms |
20472 KB |
Output is correct |
43 |
Correct |
213 ms |
20072 KB |
Output is correct |
44 |
Correct |
480 ms |
29396 KB |
Output is correct |
45 |
Correct |
393 ms |
27508 KB |
Output is correct |
46 |
Correct |
429 ms |
26768 KB |
Output is correct |
47 |
Correct |
193 ms |
20076 KB |
Output is correct |
48 |
Correct |
386 ms |
30704 KB |
Output is correct |
49 |
Correct |
413 ms |
27380 KB |
Output is correct |
50 |
Correct |
259 ms |
14736 KB |
Output is correct |
51 |
Correct |
496 ms |
36916 KB |
Output is correct |
52 |
Correct |
44 ms |
8824 KB |
Output is correct |
53 |
Correct |
393 ms |
21144 KB |
Output is correct |
54 |
Correct |
488 ms |
38264 KB |
Output is correct |
55 |
Correct |
478 ms |
29308 KB |
Output is correct |
56 |
Correct |
359 ms |
21172 KB |
Output is correct |
57 |
Correct |
433 ms |
24672 KB |
Output is correct |
58 |
Correct |
392 ms |
23416 KB |
Output is correct |
59 |
Correct |
375 ms |
22396 KB |
Output is correct |
60 |
Correct |
310 ms |
21364 KB |
Output is correct |
61 |
Correct |
447 ms |
34808 KB |
Output is correct |
62 |
Correct |
406 ms |
28532 KB |
Output is correct |
63 |
Correct |
423 ms |
28400 KB |
Output is correct |
64 |
Correct |
208 ms |
21872 KB |
Output is correct |
65 |
Correct |
425 ms |
32876 KB |
Output is correct |
66 |
Correct |
427 ms |
29588 KB |
Output is correct |
67 |
Correct |
36 ms |
7160 KB |
Output is correct |
68 |
Correct |
151 ms |
19064 KB |
Output is correct |
69 |
Correct |
233 ms |
19448 KB |
Output is correct |
70 |
Correct |
385 ms |
19476 KB |
Output is correct |
71 |
Correct |
504 ms |
35832 KB |
Output is correct |
72 |
Correct |
520 ms |
27476 KB |
Output is correct |
73 |
Correct |
345 ms |
19576 KB |
Output is correct |
74 |
Correct |
401 ms |
22368 KB |
Output is correct |
75 |
Correct |
425 ms |
20968 KB |
Output is correct |
76 |
Correct |
404 ms |
20728 KB |
Output is correct |
77 |
Correct |
285 ms |
19952 KB |
Output is correct |
78 |
Correct |
460 ms |
31096 KB |
Output is correct |
79 |
Correct |
413 ms |
29404 KB |
Output is correct |
80 |
Correct |
397 ms |
25844 KB |
Output is correct |
81 |
Correct |
201 ms |
19952 KB |
Output is correct |
82 |
Correct |
413 ms |
30528 KB |
Output is correct |
83 |
Correct |
410 ms |
27508 KB |
Output is correct |
84 |
Correct |
356 ms |
19752 KB |
Output is correct |
85 |
Correct |
530 ms |
36648 KB |
Output is correct |
86 |
Correct |
465 ms |
27912 KB |
Output is correct |
87 |
Correct |
365 ms |
19824 KB |
Output is correct |
88 |
Correct |
416 ms |
23288 KB |
Output is correct |
89 |
Correct |
403 ms |
21720 KB |
Output is correct |
90 |
Correct |
416 ms |
20984 KB |
Output is correct |
91 |
Correct |
315 ms |
20208 KB |
Output is correct |
92 |
Correct |
473 ms |
35832 KB |
Output is correct |
93 |
Correct |
418 ms |
25460 KB |
Output is correct |
94 |
Correct |
387 ms |
23928 KB |
Output is correct |
95 |
Correct |
202 ms |
20316 KB |
Output is correct |
96 |
Correct |
388 ms |
31120 KB |
Output is correct |
97 |
Correct |
420 ms |
28148 KB |
Output is correct |
98 |
Correct |
393 ms |
21156 KB |
Output is correct |
99 |
Correct |
526 ms |
36980 KB |
Output is correct |
100 |
Correct |
482 ms |
29008 KB |
Output is correct |
101 |
Correct |
378 ms |
20460 KB |
Output is correct |
102 |
Correct |
429 ms |
22992 KB |
Output is correct |
103 |
Correct |
407 ms |
21948 KB |
Output is correct |
104 |
Correct |
400 ms |
21596 KB |
Output is correct |
105 |
Correct |
292 ms |
20764 KB |
Output is correct |
106 |
Correct |
457 ms |
30000 KB |
Output is correct |
107 |
Correct |
412 ms |
30196 KB |
Output is correct |
108 |
Correct |
474 ms |
26104 KB |
Output is correct |
109 |
Correct |
225 ms |
21484 KB |
Output is correct |
110 |
Correct |
421 ms |
31972 KB |
Output is correct |
111 |
Correct |
445 ms |
28996 KB |
Output is correct |