Skip to content

Commit 9b39418

Browse files
committed
remove cd asserts
1 parent f5ff9d2 commit 9b39418

File tree

4 files changed

+34
-50
lines changed

4 files changed

+34
-50
lines changed

library/graphs/strongly_connected_components/offline_incremental_scc.hpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,25 @@
1313
vi offline_incremental_scc(vector<array<int, 2>> eds,
1414
int n) {
1515
int m = sz(eds);
16-
vi ids(n, -1), joins(m, m), idx(m);
16+
vi ids(n, -1), joins(m, m), idx(m), vs(n), scc_id;
1717
iota(all(idx), 0);
1818
auto divide_and_conquer = [&](auto&& self, auto el,
1919
auto er, int tl, int tr) {
20-
int mid = midpoint(tl, tr);
21-
vi vs;
20+
int mid = midpoint(tl, tr), p = 0;
2221
vector<vi> adj;
2322
for (auto it = el; it != er; it++) {
2423
auto& [u, v] = eds[*it];
2524
for (int w : {u, v}) {
2625
if (ids[w] != -1) continue;
27-
ids[w] = sz(vs);
28-
vs.push_back(w);
26+
ids[w] = p;
27+
vs[p++] = w;
2928
adj.emplace_back();
3029
}
3130
u = ids[u], v = ids[v];
3231
if (*it <= mid) adj[u].push_back(v);
3332
}
34-
for (int v : vs) ids[v] = -1;
35-
auto scc_id = sccs(adj).scc_id;
33+
rep(i, 0, p) ids[vs[i]] = -1;
34+
scc_id = sccs(adj).scc_id;
3635
auto split = partition(el, er, [&](int i) {
3736
return scc_id[eds[i][0]] == scc_id[eds[i][1]];
3837
});
@@ -43,9 +42,7 @@ vi offline_incremental_scc(vector<array<int, 2>> eds,
4342
u = scc_id[u], v = scc_id[v];
4443
}
4544
// deallocate to avoid O(m log m) memory
46-
vi().swap(vs);
4745
vector<vi>().swap(adj);
48-
vi().swap(scc_id);
4946
self(self, el, split, tl, mid);
5047
self(self, split, er, mid, tr);
5148
};

library/trees/centroid_decomp_uncommon/count_paths_per_length.hpp

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,33 @@
1010
vector<ll> count_paths_per_length(const vector<vi>& adj) {
1111
vector<ll> num_paths(sz(adj));
1212
centroid(adj,
13-
[&](const vector<vi>& cd_adj, int cent, int) {
14-
vector<vector<double>> child_depths;
15-
for (int v : cd_adj[cent]) {
16-
child_depths.emplace_back(1, 0.0);
17-
for (queue<pii> q({{v, cent}}); !empty(q);) {
18-
child_depths.back().push_back(sz(q));
19-
queue<pii> new_q;
20-
while (!empty(q)) {
21-
auto [u, p] = q.front();
22-
q.pop();
23-
for (int w : cd_adj[u]) {
24-
if (w == p) continue;
25-
new_q.emplace(w, u);
26-
}
27-
}
28-
swap(q, new_q);
29-
}
30-
}
31-
ranges::sort(child_depths,
32-
[&](auto& x, auto& y) { return sz(x) < sz(y); });
33-
vector total_depth(1, 1.0);
34-
for (auto& cnt_depth : child_depths) {
35-
auto prod = conv(total_depth, cnt_depth);
36-
rep(i, 1, sz(prod)) num_paths[i] +=
37-
llround(prod[i]);
38-
total_depth.resize(sz(cnt_depth));
39-
rep(i, 1, sz(cnt_depth)) total_depth[i] +=
40-
cnt_depth[i];
41-
}
42-
});
13+
[&](const vector<vi>& cd_adj, int cent, int) {
14+
vector<vector<double>> child_depths;
15+
for (int v : cd_adj[cent]) {
16+
child_depths.emplace_back(1, 0.0);
17+
for (queue<pii> q({{v, cent}}); !empty(q);) {
18+
child_depths.back().push_back(sz(q));
19+
queue<pii> new_q;
20+
while (!empty(q)) {
21+
auto [u, p] = q.front();
22+
q.pop();
23+
for (int w : cd_adj[u]) {
24+
if (w == p) continue;
25+
new_q.emplace(w, u);
26+
}
27+
}
28+
swap(q, new_q);
29+
}
30+
}
31+
ranges::sort(child_depths, {}, [&](auto& x) { return sz(x); });
32+
vector total_depth(1, 1.0);
33+
for (auto& cnt_depth : child_depths) {
34+
auto prod = conv(total_depth, cnt_depth);
35+
rep(i, 1, sz(prod)) num_paths[i] += llround(prod[i]);
36+
total_depth.resize(sz(cnt_depth));
37+
rep(i, 1, sz(cnt_depth)) total_depth[i] +=
38+
cnt_depth[i];
39+
}
40+
});
4341
return num_paths;
4442
}

tests/library_checker_aizu_tests/trees/count_paths_per_length.test.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#define PROBLEM \
22
"https://judge.yosupo.jp/problem/frequency_table_of_tree_distance"
33
#include "../template.hpp"
4-
#include "../cd_asserts.hpp"
54
#include "../../../library/trees/centroid_decomp_uncommon/count_paths_per_length.hpp"
65
int main() {
76
cin.tie(0)->sync_with_stdio(0);
@@ -14,7 +13,6 @@ int main() {
1413
adj[u].push_back(v);
1514
adj[v].push_back(u);
1615
}
17-
cd_asserts(adj);
1816
vector<ll> cnt_len = count_paths_per_length(adj);
1917
for (int i = 1; i < n; i++) cout << cnt_len[i] << " ";
2018
cout << '\n';

tests/library_checker_aizu_tests/trees/edge_cd_count_paths_per_length.test.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "../template.hpp"
44
#include "../edge_cd_asserts.hpp"
55
#include "../../../library/trees/edge_centroid_decomp_uncommon/count_paths_per_length.hpp"
6-
#include "../../../library/contest/random.hpp"
7-
#include "../../../library/trees/centroid_decomp_uncommon/count_paths_per_node.hpp"
86
int main() {
97
cin.tie(0)->sync_with_stdio(0);
108
int n;
@@ -18,13 +16,6 @@ int main() {
1816
}
1917
{ edge_cd(adj, edge_cd_asserts); }
2018
vector<ll> cnt_len = count_paths_per_length(adj);
21-
if (n >= 2) {
22-
int k = rnd(1, n - 1);
23-
vector<ll> count_paths = count_paths_per_node(adj, k);
24-
ll sum = accumulate(begin(count_paths), end(count_paths), 0LL);
25-
assert(sum % (k + 1) == 0);
26-
assert(sum / (k + 1) == cnt_len[k]);
27-
}
2819
for (int i = 1; i < n; i++) cout << cnt_len[i] << " ";
2920
cout << '\n';
3021
return 0;

0 commit comments

Comments
 (0)