#include <bits/stdc++.h>
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define sq(X) ((X)*(X))
#define all(V) (V).begin(), (V).end()
#define unq(V) (V).erase(unique(all(V)), (V).end())
using namespace std;
typedef long long ll;
typedef vector<ll> vlm;
typedef vector<int> vim;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
int st[200010], sum;
void ins(int i, int t) {
if (!st[i]) sum++;
st[i]+=t;
if (!st[i]) sum--;
}
int N, M, C[200010], D[200010], E[200010], chk[200010], A[200010];
vim adj[200010], dia, V[200010];
struct Seg {
int ar[1<<19], fl[1<<19];
void init1(int i, int s, int e, int t) {
if (s==e) { ar[i]=s; return ; }
int md=(s+e)/2;
if (t<=md) init1(i*2, s, md, t);
else init1(i*2+1, md+1, e, t);
ar[i]=max(ar[i*2], ar[i*2+1]);
}
void init() {
memset(ar, 0, sizeof(ar));
memset(fl, 0, sizeof(fl));
for (int i=1; i<=M; i++) init1(1, 1, M, i);
}
void upd(int i, int s, int e, int ts, int te, int v) {
if (te<s||e<ts||te<ts) return ;
if (ts<=s&&e<=te) {
fl[i]+=v;
if (fl[i]) ar[i]=0;
else ar[i]=(s==e?s:max(ar[i*2], ar[i*2+1]));
return ;
}
int md=(s+e)/2;
upd(i*2, s, md, ts, te, v); upd(i*2+1, md+1, e, ts, te, v);
ar[i]=fl[i]?0:max(ar[i*2], ar[i*2+1]);
}
int get(int i, int s, int e, int ts, int te) {
if (te<s||e<ts||te<ts||!ar[i]) return 0;
if (ts<=s&&e<=te) return ar[i];
int md=(s+e)/2;
return max(get(i*2, s, md, ts, te), get(i*2+1, md+1, e, ts, te));
}
}S;
void dfs1(int now, int par) {
D[now]=D[par]+1;
for (auto &i:adj[now]) if (i!=par) dfs1(i, now);
}
void dfs2(int now) {
dia.eb(now);
for (auto &i:adj[now]) if (D[i]==D[now]-1) dfs2(i);
}
void dfs3(int now, int par, int lev) {
V[lev].eb(now);
for (auto &i:adj[now]) if (i!=par) dfs3(i, now, lev+1);
}
void dfs4(int now, int par, int lev) {
D[now]=E[now]=lev;
for (auto &i:adj[now]) if (i!=par&&!chk[i]) dfs4(i, now, lev-1), E[now]=min(E[now], E[i]);
}
void dfs5(int now, int par, int tp) {
vim use;
while (tp>2*D[now]-E[now]) {
if (S.get(1, 1, M, tp, tp)==tp && V[tp].size()==1) { use.eb(V[tp][0]), ins(C[V[tp][0]], 1); }
tp=S.get(1, 1, M, 1, tp-1);
}
A[now]=sum;
V[D[now]].eb(now);
for (auto &i:adj[now]) if (i!=par&&!chk[i]) S.upd(1, 1, M, D[now]+1, 2*D[now]-E[i], 1);
for (auto &i:adj[now]) if (i!=par&&!chk[i]) {
S.upd(1, 1, M, D[now]+1, 2*D[now]-E[i], -1);
dfs5(i, now, tp);
S.upd(1, 1, M, D[now]+1, 2*D[now]-E[i], 1);
}
for (auto &i:adj[now]) if (i!=par&&!chk[i]) S.upd(1, 1, M, D[now]+1, 2*D[now]-E[i], -1);
for (auto &i:use) ins(C[i], -1);
V[D[now]].pop_back();
}
void solve(int st, int lst) {
S.init();
for (int i=1; i<=N; i++) V[i].clear();
dfs3(lst, st, M/2+1);
memset(chk, 0, sizeof(chk)); chk[lst]=1;
dfs4(st, 0, M/2), dfs5(st, 0, M);
}
void solve1(int st, vim lst) {
S.init(); S.upd(1, 1, M, M/2+2, M, 1);
for (int i=1; i<=N; i++) V[i].clear();
memset(chk, 0, sizeof(chk)); for (auto &i:lst) chk[i]=1;
dfs4(st, 0, M/2+1), dfs5(st, 0, M/2+1);
}
int main() {
int im;
scanf("%d %d", &N, &im);
for (int i=1, u, v; i<N; i++) scanf("%d %d", &u, &v), adj[u].eb(v), adj[v].eb(u);
for (int i=1; i<=N; i++) scanf("%d", &C[i]);
dfs1(1, 0);
int mx=0; for (int i=1; i<=N; i++) mx=max(mx, D[i]);
dia.eb(0);
for (int i=1; i<=N; i++) if (mx==D[i]) {
memset(D, 0, sizeof(D));
dfs1(i, 0);
mx=0;
for (int j=1; j<=N; j++) mx=max(mx, D[j]);
for (int j=1; j<=N; j++) if (mx==D[j]) { dfs2(j); break; }
break;
}
M=dia.size()-1;
solve(dia[M/2], dia[M/2+1]);
solve(dia[M+1-M/2], dia[M-M/2]);
if (M%2) solve1(dia[(M+1)/2], {dia[(M+1)/2+1], dia[(M+1)/2-1]});
for (int i=1; i<=N; i++) printf("%d\n", A[i]);
return 0;
}
Compilation message
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:116:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &im);
~~~~~^~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:117:68: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i=1, u, v; i<N; i++) scanf("%d %d", &u, &v), adj[u].eb(v), adj[v].eb(u);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
joi2019_ho_t5.cpp:119:32: 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]);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
15352 KB |
Output is correct |
2 |
Correct |
18 ms |
15480 KB |
Output is correct |
3 |
Correct |
16 ms |
15608 KB |
Output is correct |
4 |
Correct |
18 ms |
15608 KB |
Output is correct |
5 |
Correct |
17 ms |
15480 KB |
Output is correct |
6 |
Correct |
20 ms |
15864 KB |
Output is correct |
7 |
Correct |
19 ms |
15608 KB |
Output is correct |
8 |
Correct |
21 ms |
15480 KB |
Output is correct |
9 |
Correct |
18 ms |
15608 KB |
Output is correct |
10 |
Correct |
19 ms |
15480 KB |
Output is correct |
11 |
Correct |
18 ms |
15480 KB |
Output is correct |
12 |
Correct |
16 ms |
15480 KB |
Output is correct |
13 |
Correct |
21 ms |
15864 KB |
Output is correct |
14 |
Correct |
19 ms |
15608 KB |
Output is correct |
15 |
Correct |
19 ms |
15608 KB |
Output is correct |
16 |
Correct |
16 ms |
15480 KB |
Output is correct |
17 |
Correct |
19 ms |
15736 KB |
Output is correct |
18 |
Correct |
19 ms |
15608 KB |
Output is correct |
19 |
Correct |
18 ms |
15480 KB |
Output is correct |
20 |
Correct |
21 ms |
15864 KB |
Output is correct |
21 |
Correct |
19 ms |
15608 KB |
Output is correct |
22 |
Correct |
17 ms |
15480 KB |
Output is correct |
23 |
Correct |
18 ms |
15608 KB |
Output is correct |
24 |
Correct |
17 ms |
15480 KB |
Output is correct |
25 |
Correct |
18 ms |
15480 KB |
Output is correct |
26 |
Correct |
16 ms |
15480 KB |
Output is correct |
27 |
Correct |
20 ms |
15740 KB |
Output is correct |
28 |
Correct |
18 ms |
15736 KB |
Output is correct |
29 |
Correct |
19 ms |
15608 KB |
Output is correct |
30 |
Correct |
16 ms |
15480 KB |
Output is correct |
31 |
Correct |
20 ms |
15740 KB |
Output is correct |
32 |
Correct |
19 ms |
15608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
297 ms |
22776 KB |
Output is correct |
2 |
Correct |
593 ms |
36076 KB |
Output is correct |
3 |
Correct |
101 ms |
18680 KB |
Output is correct |
4 |
Correct |
556 ms |
28388 KB |
Output is correct |
5 |
Correct |
1208 ms |
52000 KB |
Output is correct |
6 |
Correct |
1027 ms |
39028 KB |
Output is correct |
7 |
Correct |
472 ms |
28788 KB |
Output is correct |
8 |
Correct |
676 ms |
30328 KB |
Output is correct |
9 |
Correct |
720 ms |
29944 KB |
Output is correct |
10 |
Correct |
666 ms |
29688 KB |
Output is correct |
11 |
Correct |
257 ms |
29928 KB |
Output is correct |
12 |
Correct |
1006 ms |
42100 KB |
Output is correct |
13 |
Correct |
909 ms |
39532 KB |
Output is correct |
14 |
Correct |
935 ms |
38384 KB |
Output is correct |
15 |
Correct |
236 ms |
29032 KB |
Output is correct |
16 |
Correct |
947 ms |
44008 KB |
Output is correct |
17 |
Correct |
977 ms |
39356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
440 ms |
27128 KB |
Output is correct |
2 |
Correct |
1256 ms |
52460 KB |
Output is correct |
3 |
Correct |
118 ms |
19320 KB |
Output is correct |
4 |
Correct |
585 ms |
30200 KB |
Output is correct |
5 |
Correct |
1291 ms |
54252 KB |
Output is correct |
6 |
Correct |
1050 ms |
40944 KB |
Output is correct |
7 |
Correct |
532 ms |
30268 KB |
Output is correct |
8 |
Correct |
816 ms |
34164 KB |
Output is correct |
9 |
Correct |
779 ms |
32752 KB |
Output is correct |
10 |
Correct |
799 ms |
31612 KB |
Output is correct |
11 |
Correct |
490 ms |
30792 KB |
Output is correct |
12 |
Correct |
1343 ms |
49032 KB |
Output is correct |
13 |
Correct |
1032 ms |
39836 KB |
Output is correct |
14 |
Correct |
1022 ms |
39524 KB |
Output is correct |
15 |
Correct |
247 ms |
30060 KB |
Output is correct |
16 |
Correct |
1115 ms |
46308 KB |
Output is correct |
17 |
Correct |
1171 ms |
41004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
15352 KB |
Output is correct |
2 |
Correct |
18 ms |
15480 KB |
Output is correct |
3 |
Correct |
16 ms |
15608 KB |
Output is correct |
4 |
Correct |
18 ms |
15608 KB |
Output is correct |
5 |
Correct |
17 ms |
15480 KB |
Output is correct |
6 |
Correct |
20 ms |
15864 KB |
Output is correct |
7 |
Correct |
19 ms |
15608 KB |
Output is correct |
8 |
Correct |
21 ms |
15480 KB |
Output is correct |
9 |
Correct |
18 ms |
15608 KB |
Output is correct |
10 |
Correct |
19 ms |
15480 KB |
Output is correct |
11 |
Correct |
18 ms |
15480 KB |
Output is correct |
12 |
Correct |
16 ms |
15480 KB |
Output is correct |
13 |
Correct |
21 ms |
15864 KB |
Output is correct |
14 |
Correct |
19 ms |
15608 KB |
Output is correct |
15 |
Correct |
19 ms |
15608 KB |
Output is correct |
16 |
Correct |
16 ms |
15480 KB |
Output is correct |
17 |
Correct |
19 ms |
15736 KB |
Output is correct |
18 |
Correct |
19 ms |
15608 KB |
Output is correct |
19 |
Correct |
18 ms |
15480 KB |
Output is correct |
20 |
Correct |
21 ms |
15864 KB |
Output is correct |
21 |
Correct |
19 ms |
15608 KB |
Output is correct |
22 |
Correct |
17 ms |
15480 KB |
Output is correct |
23 |
Correct |
18 ms |
15608 KB |
Output is correct |
24 |
Correct |
17 ms |
15480 KB |
Output is correct |
25 |
Correct |
18 ms |
15480 KB |
Output is correct |
26 |
Correct |
16 ms |
15480 KB |
Output is correct |
27 |
Correct |
20 ms |
15740 KB |
Output is correct |
28 |
Correct |
18 ms |
15736 KB |
Output is correct |
29 |
Correct |
19 ms |
15608 KB |
Output is correct |
30 |
Correct |
16 ms |
15480 KB |
Output is correct |
31 |
Correct |
20 ms |
15740 KB |
Output is correct |
32 |
Correct |
19 ms |
15608 KB |
Output is correct |
33 |
Correct |
297 ms |
22776 KB |
Output is correct |
34 |
Correct |
593 ms |
36076 KB |
Output is correct |
35 |
Correct |
101 ms |
18680 KB |
Output is correct |
36 |
Correct |
556 ms |
28388 KB |
Output is correct |
37 |
Correct |
1208 ms |
52000 KB |
Output is correct |
38 |
Correct |
1027 ms |
39028 KB |
Output is correct |
39 |
Correct |
472 ms |
28788 KB |
Output is correct |
40 |
Correct |
676 ms |
30328 KB |
Output is correct |
41 |
Correct |
720 ms |
29944 KB |
Output is correct |
42 |
Correct |
666 ms |
29688 KB |
Output is correct |
43 |
Correct |
257 ms |
29928 KB |
Output is correct |
44 |
Correct |
1006 ms |
42100 KB |
Output is correct |
45 |
Correct |
909 ms |
39532 KB |
Output is correct |
46 |
Correct |
935 ms |
38384 KB |
Output is correct |
47 |
Correct |
236 ms |
29032 KB |
Output is correct |
48 |
Correct |
947 ms |
44008 KB |
Output is correct |
49 |
Correct |
977 ms |
39356 KB |
Output is correct |
50 |
Correct |
440 ms |
27128 KB |
Output is correct |
51 |
Correct |
1256 ms |
52460 KB |
Output is correct |
52 |
Correct |
118 ms |
19320 KB |
Output is correct |
53 |
Correct |
585 ms |
30200 KB |
Output is correct |
54 |
Correct |
1291 ms |
54252 KB |
Output is correct |
55 |
Correct |
1050 ms |
40944 KB |
Output is correct |
56 |
Correct |
532 ms |
30268 KB |
Output is correct |
57 |
Correct |
816 ms |
34164 KB |
Output is correct |
58 |
Correct |
779 ms |
32752 KB |
Output is correct |
59 |
Correct |
799 ms |
31612 KB |
Output is correct |
60 |
Correct |
490 ms |
30792 KB |
Output is correct |
61 |
Correct |
1343 ms |
49032 KB |
Output is correct |
62 |
Correct |
1032 ms |
39836 KB |
Output is correct |
63 |
Correct |
1022 ms |
39524 KB |
Output is correct |
64 |
Correct |
247 ms |
30060 KB |
Output is correct |
65 |
Correct |
1115 ms |
46308 KB |
Output is correct |
66 |
Correct |
1171 ms |
41004 KB |
Output is correct |
67 |
Correct |
73 ms |
17248 KB |
Output is correct |
68 |
Correct |
528 ms |
31700 KB |
Output is correct |
69 |
Correct |
659 ms |
30708 KB |
Output is correct |
70 |
Correct |
659 ms |
28400 KB |
Output is correct |
71 |
Correct |
1589 ms |
51592 KB |
Output is correct |
72 |
Correct |
1148 ms |
38960 KB |
Output is correct |
73 |
Correct |
547 ms |
28648 KB |
Output is correct |
74 |
Correct |
827 ms |
32184 KB |
Output is correct |
75 |
Correct |
1304 ms |
30236 KB |
Output is correct |
76 |
Correct |
763 ms |
30000 KB |
Output is correct |
77 |
Correct |
421 ms |
28656 KB |
Output is correct |
78 |
Correct |
1063 ms |
45036 KB |
Output is correct |
79 |
Correct |
984 ms |
42352 KB |
Output is correct |
80 |
Correct |
1020 ms |
36848 KB |
Output is correct |
81 |
Correct |
261 ms |
28780 KB |
Output is correct |
82 |
Correct |
1000 ms |
44012 KB |
Output is correct |
83 |
Correct |
1031 ms |
39152 KB |
Output is correct |
84 |
Correct |
558 ms |
28536 KB |
Output is correct |
85 |
Correct |
1252 ms |
52300 KB |
Output is correct |
86 |
Correct |
1085 ms |
39416 KB |
Output is correct |
87 |
Correct |
492 ms |
28792 KB |
Output is correct |
88 |
Correct |
796 ms |
32756 KB |
Output is correct |
89 |
Correct |
719 ms |
30968 KB |
Output is correct |
90 |
Correct |
712 ms |
30328 KB |
Output is correct |
91 |
Correct |
411 ms |
29168 KB |
Output is correct |
92 |
Correct |
1278 ms |
51408 KB |
Output is correct |
93 |
Correct |
863 ms |
36216 KB |
Output is correct |
94 |
Correct |
819 ms |
34040 KB |
Output is correct |
95 |
Correct |
242 ms |
29352 KB |
Output is correct |
96 |
Correct |
1025 ms |
44812 KB |
Output is correct |
97 |
Correct |
1027 ms |
39748 KB |
Output is correct |
98 |
Correct |
592 ms |
30072 KB |
Output is correct |
99 |
Correct |
1253 ms |
52724 KB |
Output is correct |
100 |
Correct |
1097 ms |
40580 KB |
Output is correct |
101 |
Correct |
542 ms |
29428 KB |
Output is correct |
102 |
Correct |
759 ms |
32608 KB |
Output is correct |
103 |
Correct |
719 ms |
31224 KB |
Output is correct |
104 |
Correct |
684 ms |
30840 KB |
Output is correct |
105 |
Correct |
393 ms |
29552 KB |
Output is correct |
106 |
Correct |
1018 ms |
42152 KB |
Output is correct |
107 |
Correct |
1006 ms |
43088 KB |
Output is correct |
108 |
Correct |
887 ms |
36340 KB |
Output is correct |
109 |
Correct |
235 ms |
30056 KB |
Output is correct |
110 |
Correct |
1038 ms |
45360 KB |
Output is correct |
111 |
Correct |
1006 ms |
40720 KB |
Output is correct |