#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
#define g4(a) get<4>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
using iiiii = tuple<int, int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, M, lon[200005], ans_st[200005], ans_ed[200005], lon_st[200005], lon_ed[200005], dist_st[200005], dist_ed[200005], C[200005];
ii m = mp(-1, -1);
vector<int> adj[200005];
stack<int> rep[200005];
struct node {
node *left, *right;
int S, E, pv;
ii val;
void combine() {
if (S == E) return;
if (left->val.first == right->val.first) val = mp(left->val.first, left->val.second + right->val.second);
else val = min(left->val, right->val);
}
node(int _s, int _e) : S(_s), E(_e), pv(0) {
if (S == E) {
val = mp(0, 1);
return;
}
int M = (S + E) >> 1;
left = new node(S, M);
right = new node(M + 1, E);
combine();
}
void prop() {
if (S == E || !pv) return;
left->val.first += pv;
left->pv += pv;
right->val.first += pv;
right->pv += pv;
pv = 0;
}
void upd(int l, int r, int v) {
if (S == 0 && E == N - 1 && l <= r) {
//~ cout << "+ " << l << ' ' << r << ' ' << v << '\n';
}
if (l > E || r < S) return;
if (l <= S && E <= r) {
val.first += v;
pv += v;
return;
}
prop();
left->upd(l, r, v);
right->upd(l, r, v);
combine();
}
ii qry(int l, int r) {
if (l > E || r < S) return mp((int)1e16, 0);
if (l <= S && E <= r) return val;
prop();
auto lq = left->qry(l, r), rq = right->qry(l, r);
if (lq.first == rq.first) return mp(lq.first, lq.second + rq.second);
else return min(lq, rq);
}
} *root;
void dfs(int n, int e = -1, int d = 0) {
m = max(m, mp(d, n));
for (auto u : adj[n]) if (u != e) {
dfs(u, n, d + 1);
}
}
void init(int n, int lon[], int dist[], int e = -1) {
for (auto u : adj[n]) if (u != e) {
dist[u] = dist[n] + 1;
init(u, lon, dist, n);
lon[n] = max(lon[n], lon[u]);
}
lon[n]++;
}
void get_ans(int n, int ans[], int lon[], int e = -1, int d = 0) {
root->upd(d - lon[n] + 1, d - 1, 1);
bool pushed = 0;
if (rep[C[n]].empty() || root->qry(rep[C[n]].top(), rep[C[n]].top()).first) {
rep[C[n]].push(d);
pushed = 1;
//~ cout << "PUSH " << n << '\n';
} else {
root->upd(d, d, 1);
}
auto tmp = root->qry(0, d);
//~ cout << "COMPUTE " << n << ' ' << tmp.first << ' ' << tmp.second - pushed << '\n';
for (int i = 0; i < N; i++) {
//~ cout << !!root->qry(i, i).first;
}
//~ cout << '\n';
ans[n] = (tmp.first ? 0ll : tmp.second - pushed);
root->upd(d - lon[n] + 1, d - 1, -1);
if (pushed) {
assert(!rep[C[n]].empty() && rep[C[n]].top() == d);
rep[C[n]].pop();
//~ cout << "POP " << n << '\n';
} else {
root->upd(d, d, -1);
}
multiset<int> m;
for (auto u : adj[n]) if (u != e) {
m.insert(lon[u]);
}
for (auto u : adj[n]) if (u != e) {
//~ cout << "AT " << u << '\n';
m.erase(m.find(lon[u]));
int x = (m.empty() ? 0ll : *m.rbegin());
root->upd(d - x, d - 1, 1);
bool pushed = 0;
if (rep[C[n]].empty() || root->qry(rep[C[n]].top(), rep[C[n]].top()).first) {
rep[C[n]].push(d);
pushed = 1;
//~ cout << "PUSH " << n << '\n';
} else {
//~ cout << "DONTPUSH " << n << ' ' << rep[C[n]].top() << '\n';
root->upd(d, d, 1);
}
get_ans(u, ans, lon, n, d + 1);
root->upd(d - x, d - 1, -1);
m.insert(lon[u]);
if (pushed) {
assert(!rep[C[n]].empty() && rep[C[n]].top() == d);
rep[C[n]].pop();
//~ cout << "POP " << n << '\n';
} else {
root->upd(d, d, -1);
}
}
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
for (int i = 1, u, v; i < N; i++) {
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 1; i <= N; i++) {
cin >> C[i];
}
dfs(1);
int st = m.second;
m = mp(-1, -1);
dfs(st);
int ed = m.second;
root = new node(0, N - 1);
init(st, lon_st, dist_st);
get_ans(st, ans_st, lon_st);
root = new node(0, N - 1);
init(ed, lon_ed, dist_ed);
for (int i = 1; i <= M; i++) {
while (!rep[i].empty()) rep[i].pop();
}
get_ans(ed, ans_ed, lon_ed);
for (int i = 1; i <= N; i++) {
if (dist_st[i] <= dist_ed[i]) {
cout << ans_ed[i] << '\n';
} else {
cout << ans_st[i] << '\n';
}
}
}
Compilation message
joi2019_ho_t5.cpp:157:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
157 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
139596 KB |
Output is correct |
2 |
Correct |
83 ms |
140388 KB |
Output is correct |
3 |
Correct |
83 ms |
140180 KB |
Output is correct |
4 |
Correct |
82 ms |
140520 KB |
Output is correct |
5 |
Correct |
85 ms |
140416 KB |
Output is correct |
6 |
Correct |
86 ms |
140756 KB |
Output is correct |
7 |
Correct |
85 ms |
140512 KB |
Output is correct |
8 |
Correct |
86 ms |
140428 KB |
Output is correct |
9 |
Correct |
86 ms |
140356 KB |
Output is correct |
10 |
Correct |
86 ms |
140484 KB |
Output is correct |
11 |
Correct |
85 ms |
140416 KB |
Output is correct |
12 |
Correct |
82 ms |
140464 KB |
Output is correct |
13 |
Correct |
87 ms |
140792 KB |
Output is correct |
14 |
Correct |
85 ms |
140588 KB |
Output is correct |
15 |
Correct |
87 ms |
140472 KB |
Output is correct |
16 |
Correct |
84 ms |
140424 KB |
Output is correct |
17 |
Correct |
84 ms |
140672 KB |
Output is correct |
18 |
Correct |
85 ms |
140624 KB |
Output is correct |
19 |
Correct |
83 ms |
140356 KB |
Output is correct |
20 |
Correct |
85 ms |
140748 KB |
Output is correct |
21 |
Correct |
84 ms |
140492 KB |
Output is correct |
22 |
Correct |
92 ms |
140392 KB |
Output is correct |
23 |
Correct |
90 ms |
140492 KB |
Output is correct |
24 |
Correct |
84 ms |
140468 KB |
Output is correct |
25 |
Correct |
85 ms |
140348 KB |
Output is correct |
26 |
Correct |
84 ms |
140428 KB |
Output is correct |
27 |
Correct |
85 ms |
140620 KB |
Output is correct |
28 |
Correct |
86 ms |
140628 KB |
Output is correct |
29 |
Correct |
84 ms |
140600 KB |
Output is correct |
30 |
Correct |
85 ms |
140520 KB |
Output is correct |
31 |
Correct |
87 ms |
140596 KB |
Output is correct |
32 |
Correct |
83 ms |
140592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
551 ms |
180568 KB |
Output is correct |
2 |
Correct |
635 ms |
203584 KB |
Output is correct |
3 |
Correct |
180 ms |
152532 KB |
Output is correct |
4 |
Correct |
990 ms |
211040 KB |
Output is correct |
5 |
Correct |
1163 ms |
250512 KB |
Output is correct |
6 |
Correct |
1090 ms |
230820 KB |
Output is correct |
7 |
Correct |
1000 ms |
212640 KB |
Output is correct |
8 |
Correct |
1061 ms |
216396 KB |
Output is correct |
9 |
Correct |
1031 ms |
215016 KB |
Output is correct |
10 |
Correct |
1065 ms |
214736 KB |
Output is correct |
11 |
Correct |
827 ms |
220192 KB |
Output is correct |
12 |
Correct |
1182 ms |
238972 KB |
Output is correct |
13 |
Correct |
1108 ms |
233980 KB |
Output is correct |
14 |
Correct |
1174 ms |
231564 KB |
Output is correct |
15 |
Correct |
879 ms |
220888 KB |
Output is correct |
16 |
Correct |
1064 ms |
240708 KB |
Output is correct |
17 |
Correct |
1074 ms |
232940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
709 ms |
196036 KB |
Output is correct |
2 |
Correct |
1031 ms |
246348 KB |
Output is correct |
3 |
Correct |
191 ms |
154248 KB |
Output is correct |
4 |
Correct |
879 ms |
210824 KB |
Output is correct |
5 |
Correct |
1121 ms |
251152 KB |
Output is correct |
6 |
Correct |
1170 ms |
230604 KB |
Output is correct |
7 |
Correct |
866 ms |
212284 KB |
Output is correct |
8 |
Correct |
971 ms |
221124 KB |
Output is correct |
9 |
Correct |
947 ms |
218096 KB |
Output is correct |
10 |
Correct |
950 ms |
215164 KB |
Output is correct |
11 |
Correct |
828 ms |
215436 KB |
Output is correct |
12 |
Correct |
1116 ms |
245140 KB |
Output is correct |
13 |
Correct |
999 ms |
231748 KB |
Output is correct |
14 |
Correct |
1102 ms |
230800 KB |
Output is correct |
15 |
Correct |
860 ms |
220708 KB |
Output is correct |
16 |
Correct |
1041 ms |
240960 KB |
Output is correct |
17 |
Correct |
1027 ms |
233084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
139596 KB |
Output is correct |
2 |
Correct |
83 ms |
140388 KB |
Output is correct |
3 |
Correct |
83 ms |
140180 KB |
Output is correct |
4 |
Correct |
82 ms |
140520 KB |
Output is correct |
5 |
Correct |
85 ms |
140416 KB |
Output is correct |
6 |
Correct |
86 ms |
140756 KB |
Output is correct |
7 |
Correct |
85 ms |
140512 KB |
Output is correct |
8 |
Correct |
86 ms |
140428 KB |
Output is correct |
9 |
Correct |
86 ms |
140356 KB |
Output is correct |
10 |
Correct |
86 ms |
140484 KB |
Output is correct |
11 |
Correct |
85 ms |
140416 KB |
Output is correct |
12 |
Correct |
82 ms |
140464 KB |
Output is correct |
13 |
Correct |
87 ms |
140792 KB |
Output is correct |
14 |
Correct |
85 ms |
140588 KB |
Output is correct |
15 |
Correct |
87 ms |
140472 KB |
Output is correct |
16 |
Correct |
84 ms |
140424 KB |
Output is correct |
17 |
Correct |
84 ms |
140672 KB |
Output is correct |
18 |
Correct |
85 ms |
140624 KB |
Output is correct |
19 |
Correct |
83 ms |
140356 KB |
Output is correct |
20 |
Correct |
85 ms |
140748 KB |
Output is correct |
21 |
Correct |
84 ms |
140492 KB |
Output is correct |
22 |
Correct |
92 ms |
140392 KB |
Output is correct |
23 |
Correct |
90 ms |
140492 KB |
Output is correct |
24 |
Correct |
84 ms |
140468 KB |
Output is correct |
25 |
Correct |
85 ms |
140348 KB |
Output is correct |
26 |
Correct |
84 ms |
140428 KB |
Output is correct |
27 |
Correct |
85 ms |
140620 KB |
Output is correct |
28 |
Correct |
86 ms |
140628 KB |
Output is correct |
29 |
Correct |
84 ms |
140600 KB |
Output is correct |
30 |
Correct |
85 ms |
140520 KB |
Output is correct |
31 |
Correct |
87 ms |
140596 KB |
Output is correct |
32 |
Correct |
83 ms |
140592 KB |
Output is correct |
33 |
Correct |
551 ms |
180568 KB |
Output is correct |
34 |
Correct |
635 ms |
203584 KB |
Output is correct |
35 |
Correct |
180 ms |
152532 KB |
Output is correct |
36 |
Correct |
990 ms |
211040 KB |
Output is correct |
37 |
Correct |
1163 ms |
250512 KB |
Output is correct |
38 |
Correct |
1090 ms |
230820 KB |
Output is correct |
39 |
Correct |
1000 ms |
212640 KB |
Output is correct |
40 |
Correct |
1061 ms |
216396 KB |
Output is correct |
41 |
Correct |
1031 ms |
215016 KB |
Output is correct |
42 |
Correct |
1065 ms |
214736 KB |
Output is correct |
43 |
Correct |
827 ms |
220192 KB |
Output is correct |
44 |
Correct |
1182 ms |
238972 KB |
Output is correct |
45 |
Correct |
1108 ms |
233980 KB |
Output is correct |
46 |
Correct |
1174 ms |
231564 KB |
Output is correct |
47 |
Correct |
879 ms |
220888 KB |
Output is correct |
48 |
Correct |
1064 ms |
240708 KB |
Output is correct |
49 |
Correct |
1074 ms |
232940 KB |
Output is correct |
50 |
Correct |
709 ms |
196036 KB |
Output is correct |
51 |
Correct |
1031 ms |
246348 KB |
Output is correct |
52 |
Correct |
191 ms |
154248 KB |
Output is correct |
53 |
Correct |
879 ms |
210824 KB |
Output is correct |
54 |
Correct |
1121 ms |
251152 KB |
Output is correct |
55 |
Correct |
1170 ms |
230604 KB |
Output is correct |
56 |
Correct |
866 ms |
212284 KB |
Output is correct |
57 |
Correct |
971 ms |
221124 KB |
Output is correct |
58 |
Correct |
947 ms |
218096 KB |
Output is correct |
59 |
Correct |
950 ms |
215164 KB |
Output is correct |
60 |
Correct |
828 ms |
215436 KB |
Output is correct |
61 |
Correct |
1116 ms |
245140 KB |
Output is correct |
62 |
Correct |
999 ms |
231748 KB |
Output is correct |
63 |
Correct |
1102 ms |
230800 KB |
Output is correct |
64 |
Correct |
860 ms |
220708 KB |
Output is correct |
65 |
Correct |
1041 ms |
240960 KB |
Output is correct |
66 |
Correct |
1027 ms |
233084 KB |
Output is correct |
67 |
Correct |
147 ms |
149324 KB |
Output is correct |
68 |
Correct |
570 ms |
188480 KB |
Output is correct |
69 |
Correct |
769 ms |
196812 KB |
Output is correct |
70 |
Correct |
891 ms |
211428 KB |
Output is correct |
71 |
Correct |
1165 ms |
251068 KB |
Output is correct |
72 |
Correct |
1101 ms |
230828 KB |
Output is correct |
73 |
Correct |
859 ms |
212616 KB |
Output is correct |
74 |
Correct |
1096 ms |
221308 KB |
Output is correct |
75 |
Correct |
1000 ms |
216524 KB |
Output is correct |
76 |
Correct |
1012 ms |
215624 KB |
Output is correct |
77 |
Correct |
798 ms |
216904 KB |
Output is correct |
78 |
Correct |
1193 ms |
243040 KB |
Output is correct |
79 |
Correct |
1066 ms |
238860 KB |
Output is correct |
80 |
Correct |
1121 ms |
230088 KB |
Output is correct |
81 |
Correct |
749 ms |
220960 KB |
Output is correct |
82 |
Correct |
1082 ms |
241048 KB |
Output is correct |
83 |
Correct |
1081 ms |
233276 KB |
Output is correct |
84 |
Correct |
878 ms |
211624 KB |
Output is correct |
85 |
Correct |
1200 ms |
251688 KB |
Output is correct |
86 |
Correct |
1163 ms |
231452 KB |
Output is correct |
87 |
Correct |
753 ms |
212648 KB |
Output is correct |
88 |
Correct |
1099 ms |
222156 KB |
Output is correct |
89 |
Correct |
1054 ms |
217456 KB |
Output is correct |
90 |
Correct |
1024 ms |
216264 KB |
Output is correct |
91 |
Correct |
748 ms |
217236 KB |
Output is correct |
92 |
Correct |
1302 ms |
250812 KB |
Output is correct |
93 |
Correct |
1156 ms |
228936 KB |
Output is correct |
94 |
Correct |
1161 ms |
224468 KB |
Output is correct |
95 |
Correct |
712 ms |
221528 KB |
Output is correct |
96 |
Correct |
1099 ms |
241696 KB |
Output is correct |
97 |
Correct |
1114 ms |
233904 KB |
Output is correct |
98 |
Correct |
934 ms |
212436 KB |
Output is correct |
99 |
Correct |
1345 ms |
252052 KB |
Output is correct |
100 |
Correct |
1179 ms |
232200 KB |
Output is correct |
101 |
Correct |
851 ms |
213828 KB |
Output is correct |
102 |
Correct |
1043 ms |
220360 KB |
Output is correct |
103 |
Correct |
976 ms |
217632 KB |
Output is correct |
104 |
Correct |
1008 ms |
216608 KB |
Output is correct |
105 |
Correct |
761 ms |
218044 KB |
Output is correct |
106 |
Correct |
1259 ms |
237860 KB |
Output is correct |
107 |
Correct |
1314 ms |
239012 KB |
Output is correct |
108 |
Correct |
1098 ms |
227240 KB |
Output is correct |
109 |
Correct |
871 ms |
222124 KB |
Output is correct |
110 |
Correct |
1298 ms |
242312 KB |
Output is correct |
111 |
Correct |
1194 ms |
234524 KB |
Output is correct |