//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;
constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
random_device rd;
mt19937 rnd32(0);
mt19937_64 rnd64(0);
template<typename T>
bool assign_max(T& a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_min(T& a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
T square(T a) {
return a * a;
}
template<>
struct std::hash<pair<ll, ll>> {
ll operator() (pair<ll, ll> p) const {
return ((__int128)p.first * MOD + p.second) % INF64;
}
};
struct node {
pair<ll, ll> x;
ll y;
ll sz;
node *l, *r;
node(pair<ll, ll> x = pair<ll, ll>(0, 0), node *l = nullptr, node *r = nullptr): x(x), l(l), r(r) {
y = rnd32();
sz = 1;
}
};
ll get(node *t) {
if (t == nullptr) {
return 0;
}
return t->sz;
}
void upd(node *t) {
t->sz = get(t->l) + get(t->r) + 1;
}
pair<node*, node*> split(node *t, pair<ll, ll> x) {
if (t == nullptr) {
return make_pair(nullptr, nullptr);
}
if (t->x > x) {
auto[l, r] = split(t->l, x);
t->l = r;
upd(t);
return make_pair(l, t);
} else {
auto[l, r] = split(t->r, x);
t->r = l;
upd(t);
return make_pair(t, r);
}
}
node* merge(node *l, node *r) {
if (l == nullptr) {
return r;
}
if (r == nullptr) {
return l;
}
if (l->y > r->y) {
l->r = merge(l->r, r);
upd(l);
return l;
} else {
r->l = merge(l, r->l);
upd(r);
return r;
}
}
struct cartesian_tree {
node* root;
vector<vector<pair<ll, node*>>> b;
vector<ll> ad;
vector<ll> last;
ll a = 0;
cartesian_tree(ll c) {
last.resize(c, 0);
ad.push_back(0);
root = nullptr;
b.push_back(vector<pair<ll, node*>>(0));
}
void add() {
a++;
ad.back()++;
}
void make() {
b.push_back(vector<pair<ll, node*>>(0));
ad.push_back(0);
}
void back() {
a -= ad.back();
ad.pop_back();
reverse(b.back().begin(), b.back().end());
for (auto[bb, t] : b.back()) {
if (bb == 1) {
root = merge(t, root);
} else {
pair<ll, ll> x = t->x;
last[x.second] = bb;
auto[l, r] = split(root, x);
x.second--;
auto[l_l, l_r] = split(l, x);
root = merge(l_l, r);
}
}
b.pop_back();
}
ll getsz() {
return get(root);
}
void del(ll x) {
x -= a;
auto[l, r] = split(root, make_pair(x, INF32));
b.back().emplace_back(1, l);
root = r;
}
void insert(ll c) {
auto[l, r] = split(root, make_pair(last[c], c));
auto[l_l, l_r] = split(l, make_pair(last[c], c - 1));
if (get(l_r) == 0) {
l_r = new node(make_pair(-a, c));
b.back().emplace_back(last[c], l_r);
last[c] = -a;
root = merge(l_l, r);
auto[nl, nr] = split(root, l_r->x);
l_l = nl;
r = nr;
}
root = merge(merge(l_l, l_r), r);
}
};
ll get_md(vector<vector<ll>>& tree, ll v) {
vector<ll> dist(tree.size(), INF32);
queue<ll> q;
dist[v] = 0;
q.push(v);
ll lst = v;
while (!q.empty()) {
ll x = q.front();
q.pop();
lst = x;
for (auto i : tree[x]) {
if (assign_min(dist[i], dist[x] + 1)) {
q.push(i);
}
}
}
return lst;
}
bool find(ll v, ll p, vector<vector<ll>>& tree, ll x, vector<ll>& now) {
now.push_back(v);
if (v == x) {
return true;
}
for (auto i : tree[v]) {
if (i != p) {
if (find(i, v, tree, x, now)) {
return true;
}
}
}
now.pop_back();
return false;
}
void dfs(ll v, vector<bool>& used, vector<vector<ll>>& tree, vector<ll>& h) {
h[v] = 0;
used[v] = true;
for (auto i : tree[v]) {
if (!used[i]) {
dfs(i, used, tree, h);
assign_max(h[v], h[i]);
}
}
h[v]++;
}
void dfs2(ll v, ll p, vector<vector<ll>>& tree, vector<ll>& h, cartesian_tree& ct, vector<ll>& ans, vector<ll>& c) {
vector<pair<ll, ll>> all;
for (auto i : tree[v]) {
if (i != p) {
all.emplace_back(h[i], i);
}
}
sort(all.begin(), all.end());
reverse(all.begin(), all.end());
ct.make();
if (all.size() > 1) {
ct.del(all[1].first);
}
ct.insert(c[v]);
ct.add();
if (!all.empty()) {
dfs2(all[0].second, v, tree, h, ct, ans, c);
}
ct.back();
ct.make();
if (!all.empty()) {
ct.del(all[0].first);
}
ans[v] = ct.getsz();
ct.insert(c[v]);
ct.add();
if (!all.empty()) {
all.erase(all.begin());
}
for (auto[_, i] : all) {
dfs2(i, v, tree, h, ct, ans, c);
}
ct.back();
}
void solve() {
ll n, m;
cin >> n >> m;
vector<vector<ll>> tree(n);
for (ll i = 1; i < n; i++) {
ll a, b;
cin >> a >> b;
a--;
b--;
tree[a].push_back(b);
tree[b].push_back(a);
}
ll l = get_md(tree, 0);
ll r = get_md(tree, l);
vector<ll> way;
find(l, l, tree, r, way);
vector<ll> c(n);
for (ll i = 0; i < n; i++) {
cin >> c[i];
c[i]--;
}
vector<ll> ans(n, -1);
vector<bool> used(n, false);
for (auto i : way) {
used[i] = true;
}
vector<ll> h(n, 0);
for (auto i : way) {
dfs(i, used, tree, h);
}
for (ll i = 0; i < n; i++) {
used[i] = false;
}
for (auto i : way) {
used[i] = true;
}
ll mid = way[way.size() / 2];
bool b = false;
cartesian_tree ct(m);
for (ll i = 0; i < way.size(); i++) {
if (way[i] == mid) {
b = true;
}
if (b) {
ct.make();
ct.del(way.size() - i - 1);
ans[way[i]] = ct.getsz();
ct.insert(c[way[i]]);
ct.add();
for (auto j : tree[way[i]]) {
if (!used[j]) {
dfs2(j, way[i], tree, h, ct, ans, c);
}
}
ct.back();
}
ct.del(h[way[i]] - 1);
ct.insert(c[way[i]]);
ct.add();
}
ct = cartesian_tree(m);
mid = way[way.size() / 2 - 1];
reverse(way.begin(), way.end());
b = false;
for (ll i = 0; i < way.size(); i++) {
if (way[i] == mid) {
b = true;
}
if (b) {
ct.make();
ct.del(way.size() - i - 1);
ans[way[i]] = ct.getsz();
ct.insert(c[way[i]]);
ct.add();
for (auto j : tree[way[i]]) {
if (!used[j]) {
dfs2(j, way[i], tree, h, ct, ans, c);
}
}
ct.back();
}
ct.del(h[way[i]] - 1);
ct.insert(c[way[i]]);
ct.add();
}
for (auto i : ans) {
cout << i << '\n';
}
}
int main() {
if (IS_FILE) {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
if (IS_TEST_CASES) {
cin >> t;
}
for (ll i = 0; i < t; i++) {
solve();
}
}
Compilation message
joi2019_ho_t5.cpp: In function 'void solve()':
joi2019_ho_t5.cpp:291:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
291 | for (ll i = 0; i < way.size(); i++) {
| ~~^~~~~~~~~~~~
joi2019_ho_t5.cpp:316:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
316 | for (ll i = 0; i < way.size(); i++) {
| ~~^~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:344:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
344 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:345:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
345 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
804 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
3 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
860 KB |
Output is correct |
28 |
Correct |
3 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
11976 KB |
Output is correct |
2 |
Correct |
79 ms |
25028 KB |
Output is correct |
3 |
Correct |
19 ms |
6288 KB |
Output is correct |
4 |
Correct |
144 ms |
21932 KB |
Output is correct |
5 |
Correct |
176 ms |
44344 KB |
Output is correct |
6 |
Correct |
201 ms |
42768 KB |
Output is correct |
7 |
Correct |
187 ms |
20448 KB |
Output is correct |
8 |
Correct |
181 ms |
23136 KB |
Output is correct |
9 |
Correct |
151 ms |
24628 KB |
Output is correct |
10 |
Correct |
151 ms |
23604 KB |
Output is correct |
11 |
Correct |
137 ms |
22136 KB |
Output is correct |
12 |
Correct |
169 ms |
35372 KB |
Output is correct |
13 |
Correct |
206 ms |
32292 KB |
Output is correct |
14 |
Correct |
174 ms |
37332 KB |
Output is correct |
15 |
Correct |
80 ms |
20904 KB |
Output is correct |
16 |
Correct |
180 ms |
39072 KB |
Output is correct |
17 |
Correct |
195 ms |
40840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
35156 KB |
Output is correct |
2 |
Correct |
530 ms |
80392 KB |
Output is correct |
3 |
Correct |
40 ms |
11364 KB |
Output is correct |
4 |
Correct |
234 ms |
48080 KB |
Output is correct |
5 |
Correct |
493 ms |
84008 KB |
Output is correct |
6 |
Correct |
346 ms |
70828 KB |
Output is correct |
7 |
Correct |
225 ms |
49400 KB |
Output is correct |
8 |
Correct |
346 ms |
55776 KB |
Output is correct |
9 |
Correct |
297 ms |
52792 KB |
Output is correct |
10 |
Correct |
284 ms |
51760 KB |
Output is correct |
11 |
Correct |
229 ms |
49616 KB |
Output is correct |
12 |
Correct |
503 ms |
77000 KB |
Output is correct |
13 |
Correct |
340 ms |
64192 KB |
Output is correct |
14 |
Correct |
356 ms |
70400 KB |
Output is correct |
15 |
Correct |
152 ms |
49980 KB |
Output is correct |
16 |
Correct |
368 ms |
76508 KB |
Output is correct |
17 |
Correct |
321 ms |
70500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
804 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
3 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
860 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
2 ms |
860 KB |
Output is correct |
26 |
Correct |
2 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
860 KB |
Output is correct |
28 |
Correct |
3 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
2 ms |
860 KB |
Output is correct |
33 |
Correct |
72 ms |
11976 KB |
Output is correct |
34 |
Correct |
79 ms |
25028 KB |
Output is correct |
35 |
Correct |
19 ms |
6288 KB |
Output is correct |
36 |
Correct |
144 ms |
21932 KB |
Output is correct |
37 |
Correct |
176 ms |
44344 KB |
Output is correct |
38 |
Correct |
201 ms |
42768 KB |
Output is correct |
39 |
Correct |
187 ms |
20448 KB |
Output is correct |
40 |
Correct |
181 ms |
23136 KB |
Output is correct |
41 |
Correct |
151 ms |
24628 KB |
Output is correct |
42 |
Correct |
151 ms |
23604 KB |
Output is correct |
43 |
Correct |
137 ms |
22136 KB |
Output is correct |
44 |
Correct |
169 ms |
35372 KB |
Output is correct |
45 |
Correct |
206 ms |
32292 KB |
Output is correct |
46 |
Correct |
174 ms |
37332 KB |
Output is correct |
47 |
Correct |
80 ms |
20904 KB |
Output is correct |
48 |
Correct |
180 ms |
39072 KB |
Output is correct |
49 |
Correct |
195 ms |
40840 KB |
Output is correct |
50 |
Correct |
161 ms |
35156 KB |
Output is correct |
51 |
Correct |
530 ms |
80392 KB |
Output is correct |
52 |
Correct |
40 ms |
11364 KB |
Output is correct |
53 |
Correct |
234 ms |
48080 KB |
Output is correct |
54 |
Correct |
493 ms |
84008 KB |
Output is correct |
55 |
Correct |
346 ms |
70828 KB |
Output is correct |
56 |
Correct |
225 ms |
49400 KB |
Output is correct |
57 |
Correct |
346 ms |
55776 KB |
Output is correct |
58 |
Correct |
297 ms |
52792 KB |
Output is correct |
59 |
Correct |
284 ms |
51760 KB |
Output is correct |
60 |
Correct |
229 ms |
49616 KB |
Output is correct |
61 |
Correct |
503 ms |
77000 KB |
Output is correct |
62 |
Correct |
340 ms |
64192 KB |
Output is correct |
63 |
Correct |
356 ms |
70400 KB |
Output is correct |
64 |
Correct |
152 ms |
49980 KB |
Output is correct |
65 |
Correct |
368 ms |
76508 KB |
Output is correct |
66 |
Correct |
321 ms |
70500 KB |
Output is correct |
67 |
Correct |
29 ms |
6784 KB |
Output is correct |
68 |
Correct |
191 ms |
24236 KB |
Output is correct |
69 |
Correct |
213 ms |
36036 KB |
Output is correct |
70 |
Correct |
210 ms |
38968 KB |
Output is correct |
71 |
Correct |
224 ms |
43928 KB |
Output is correct |
72 |
Correct |
221 ms |
46100 KB |
Output is correct |
73 |
Correct |
198 ms |
40128 KB |
Output is correct |
74 |
Correct |
164 ms |
25728 KB |
Output is correct |
75 |
Correct |
193 ms |
28320 KB |
Output is correct |
76 |
Correct |
268 ms |
35740 KB |
Output is correct |
77 |
Correct |
162 ms |
40108 KB |
Output is correct |
78 |
Correct |
229 ms |
40500 KB |
Output is correct |
79 |
Correct |
190 ms |
41156 KB |
Output is correct |
80 |
Correct |
207 ms |
43708 KB |
Output is correct |
81 |
Correct |
131 ms |
41984 KB |
Output is correct |
82 |
Correct |
208 ms |
45940 KB |
Output is correct |
83 |
Correct |
206 ms |
45964 KB |
Output is correct |
84 |
Correct |
232 ms |
45296 KB |
Output is correct |
85 |
Correct |
296 ms |
45512 KB |
Output is correct |
86 |
Correct |
250 ms |
47628 KB |
Output is correct |
87 |
Correct |
262 ms |
45384 KB |
Output is correct |
88 |
Correct |
240 ms |
27128 KB |
Output is correct |
89 |
Correct |
248 ms |
41368 KB |
Output is correct |
90 |
Correct |
311 ms |
44336 KB |
Output is correct |
91 |
Correct |
215 ms |
45816 KB |
Output is correct |
92 |
Correct |
314 ms |
44056 KB |
Output is correct |
93 |
Correct |
261 ms |
37504 KB |
Output is correct |
94 |
Correct |
290 ms |
41748 KB |
Output is correct |
95 |
Correct |
131 ms |
45996 KB |
Output is correct |
96 |
Correct |
219 ms |
46620 KB |
Output is correct |
97 |
Correct |
263 ms |
47644 KB |
Output is correct |
98 |
Correct |
223 ms |
47916 KB |
Output is correct |
99 |
Correct |
366 ms |
45680 KB |
Output is correct |
100 |
Correct |
393 ms |
66324 KB |
Output is correct |
101 |
Correct |
267 ms |
46544 KB |
Output is correct |
102 |
Correct |
296 ms |
51760 KB |
Output is correct |
103 |
Correct |
283 ms |
50124 KB |
Output is correct |
104 |
Correct |
243 ms |
50080 KB |
Output is correct |
105 |
Correct |
179 ms |
46504 KB |
Output is correct |
106 |
Correct |
476 ms |
59156 KB |
Output is correct |
107 |
Correct |
348 ms |
45320 KB |
Output is correct |
108 |
Correct |
362 ms |
61276 KB |
Output is correct |
109 |
Correct |
159 ms |
48380 KB |
Output is correct |
110 |
Correct |
439 ms |
51188 KB |
Output is correct |
111 |
Correct |
328 ms |
64468 KB |
Output is correct |