Skip to content

Commit 7283c15

Browse files
committed
fix
1 parent 8b4c40f commit 7283c15

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

library/trees/subtree_isomorphism.hpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,16 @@
88
//! isomorphic to subtree v
99
//! @time O(n log n)
1010
//! @space O(n)
11-
struct subtree_iso {
12-
int num_distinct_subtrees;
13-
vi iso_id;
14-
subtree_iso(const vector<vi>& adj): iso_id(sz(adj), -1) {
15-
map<vi, int> hashes;
16-
auto dfs = [&](auto&& self, int v, int p) -> int {
17-
vi ch_ids;
18-
for (int u : adj[v])
19-
if (u != p) ch_ids.push_back(self(self, u, v));
20-
ranges::sort(ch_ids);
21-
return iso_id[v] =
22-
hashes.try_emplace(ch_ids, sz(hashes))
23-
.first->second;
24-
};
25-
rep(i, 0, sz(adj)) if (iso_id[i] == -1) dfs(dfs, i, i);
26-
num_distinct_subtrees = sz(hashes);
27-
}
28-
};
11+
pair<int, vi> subtree_iso(const vector<vi>& adj) {
12+
vi iso_id(sz(adj), -1);
13+
map<vi, int> hashes;
14+
auto dfs = [&](auto&& self, int v, int p) -> int {
15+
vi ch_ids;
16+
for (int u : adj[v])
17+
if (u != p) ch_ids.push_back(self(self, u, v));
18+
ranges::sort(ch_ids);
19+
return iso_id[v] = hashes.try_emplace(ch_ids, sz(hashes)).first->second;
20+
};
21+
rep(i, 0, sz(adj)) if (iso_id[i] == -1) dfs(dfs, i, i);
22+
return {sz(hashes), iso_id};
23+
}

tests/library_checker_aizu_tests/trees/subtree_isomorphism.test.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define PROBLEM \
22
"https://judge.yosupo.jp/problem/rooted_tree_isomorphism_classification"
3+
#undef _GLIBCXX_DEBUG
34
#include "../template.hpp"
45
#include "../../../library/trees/subtree_isomorphism.hpp"
56
int main() {
@@ -18,11 +19,11 @@ int main() {
1819
adj_rooted[p].push_back(i);
1920
}
2021
auto [num_distinct_subtrees, iso_id] =
21-
subtree_iso(adj_unrooted);
22+
subtree_iso(adj_unrooted);
2223
auto [num_distinct_subtrees_rooted, iso_id_rooted] =
23-
subtree_iso(adj_rooted);
24+
subtree_iso(adj_rooted);
2425
assert(
25-
num_distinct_subtrees == num_distinct_subtrees_rooted);
26+
num_distinct_subtrees == num_distinct_subtrees_rooted);
2627
cout << num_distinct_subtrees << '\n';
2728
for (int i = 0; i < n; i++) {
2829
cout << iso_id[i] << " ";

0 commit comments

Comments
 (0)