Skip to content

Commit

Permalink
refactor: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiphereth-A committed Jan 11, 2024
1 parent 7e1ab31 commit 8ec441c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 74 deletions.
4 changes: 2 additions & 2 deletions src/code/graph/dfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ constexpr void dfs_(G const& fg, u32 u, u32 fa, Fb&& init, Fp&& pre_dfs, Fs&& po
}
} else {
if (v.first != fa) {
pre_dfs(v.first, v.second, u);
pre_dfs(v.first, u, v.second);
dfs_(fg, v.first, u, std::forward<Fb>(init), std::forward<Fp>(pre_dfs), std::forward<Fs>(post_dfs), std::forward<Fr>(before_return));
post_dfs(v.first, v.second, u);
post_dfs(v.first, u, v.second);
}
}
before_return(u, fa);
Expand Down
61 changes: 25 additions & 36 deletions src/code/tree/dfs_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,31 @@ struct tree_dfs_info {
if constexpr (state & td_dis) dis = vec<weight_type>(n);

u32 cnt = 0;

auto init = [&](u32 u, u32 f) {
if constexpr (state & td_dfn) dfn[u] = cnt;
if constexpr (state & td_euler) euler[cnt] = u;
if constexpr (state & (td_dfn | td_maxdfn | td_euler)) ++cnt;
if constexpr (state & (td_sz | td_maxson)) sz[u] = 1;
if constexpr (state & td_fa) fa[u] = f;
if constexpr (state & td_go) {
go[u][0] = f;
for (u32 i = 1; i <= 20u && go[u][i - 1] < n; i++) go[u][i] = go[go[u][i - 1]][i - 1];
}
};
auto pre = [&](u32 to, u32 u) {
if constexpr (state & td_dep) dep[to] = dep[u] + 1;
if constexpr (state & td_dis) dis[to] = dis[u] + 1;
};
auto prew = [&](u32 to, weight_type const& w, u32 u) {
if constexpr (state & td_dep) dep[to] = dep[u] + 1;
if constexpr (state & td_dis) dis[to] = dis[u] + w;
};
auto post = [&](u32 to, u32 u) {
if constexpr (state & (td_sz | td_maxson)) sz[u] += sz[to];
if constexpr (state & td_maxson)
if (maxson[u] == n || sz[to] > sz[maxson[u]]) maxson[u] = to;
};
auto postw = [&](u32 to, weight_type const&, u32 u) {
if constexpr (state & (td_sz | td_maxson)) sz[u] += sz[to];
if constexpr (state & td_maxson)
if (maxson[u] == n || sz[to] > sz[maxson[u]]) maxson[u] = to;
};
auto ret = [&](u32 u, u32) {
if constexpr (state & td_maxdfn) maxdfn[u] = cnt - 1;
};

if constexpr (std::is_base_of_v<alist, G>) dfs(tree, tree.root, init, pre, post, ret);
else dfs(tree, tree.root, init, prew, postw, ret);
dfs(
tree, tree.root,
[&](u32 u, u32 f) {
if constexpr (state & td_dfn) dfn[u] = cnt;
if constexpr (state & td_euler) euler[cnt] = u;
if constexpr (state & (td_dfn | td_maxdfn | td_euler)) ++cnt;
if constexpr (state & (td_sz | td_maxson)) sz[u] = 1;
if constexpr (state & td_fa) fa[u] = f;
if constexpr (state & td_go) {
go[u][0] = f;
for (u32 i = 1; i <= 20u && go[u][i - 1] < n; i++) go[u][i] = go[go[u][i - 1]][i - 1];
}
},
[&](u32 to, u32 u, weight_type const& w = 1) {
if constexpr (state & td_dep) dep[to] = dep[u] + 1;
if constexpr (state & td_dis) dis[to] = dis[u] + w;
},
[&](u32 to, u32 u, weight_type const& = 1) {
if constexpr (state & (td_sz | td_maxson)) sz[u] += sz[to];
if constexpr (state & td_maxson)
if (maxson[u] == n || sz[to] > sz[maxson[u]]) maxson[u] = to;
},
[&](u32 u, u32) {
if constexpr (state & td_maxdfn) maxdfn[u] = cnt - 1;
});
return *this;
}
};
Expand Down
41 changes: 15 additions & 26 deletions src/code/tree/diam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,23 @@ constexpr auto tree_diam(G const& tree) {

u32 n = (u32)tree.g.size();
vec<T> mdis(n), mdis2(n);
vec<u32> midx(n), midx2(n);
vec<u32> midx(n), midx2;
std::iota(midx.begin(), midx.end(), 0), midx2 = midx;

auto init = [](u32, u32) {};
auto pre = [](u32, u32) {};
auto prew = [](u32, T const&, u32) {};
auto post = [&](u32 to, u32 u) {
if (T _ = mdis[to] + 1; _ > mdis[u]) {
mdis2[u] = mdis[u], midx2[u] = midx[u];
mdis[u] = _, midx[u] = midx[to];
} else if (_ > mdis2[u]) {
mdis2[u] = _, midx2[u] = midx2[to];
if (midx[u] == u) midx[u] = midx[to];
}
};
auto postw = [&](u32 to, T const& w, u32 u) {
if (T _ = mdis[to] + w; _ > mdis[u]) {
mdis2[u] = mdis[u], midx2[u] = midx[u];
mdis[u] = _, midx[u] = midx[to];
} else if (_ > mdis2[u]) {
mdis2[u] = _, midx2[u] = midx2[to];
if (midx[u] == u) midx[u] = midx[to];
}
};
auto ret = [](u32, u32) {};

if constexpr (std::is_base_of_v<alist, G>) dfs(tree, tree.root, init, pre, post, ret);
else dfs(tree, tree.root, init, prew, postw, ret);
dfs(
tree, tree.root,
[](u32, u32) {},
[](u32, u32, T const& = 1) {},
[&](u32 to, u32 u, T const& w) {
if (T _ = mdis[to] + w; _ > mdis[u]) {
mdis2[u] = mdis[u], midx2[u] = midx[u];
mdis[u] = _, midx[u] = midx[to];
} else if (_ > mdis2[u]) {
mdis2[u] = _, midx2[u] = midx2[to];
if (midx[u] == u) midx[u] = midx[to];
}
},
[](u32, u32) {});

u32 u = midx[0], v = midx2[0];
T d = mdis[0] + mdis2[0];
Expand Down
8 changes: 3 additions & 5 deletions src/code/tree/tree_hash_rooted.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ namespace tifa_libs::graph {

template <class Hash, u64 OFFSET = 1>
constexpr vec<u64> tree_hash_rooted(tree const &tr, Hash &&hasher) {
vec<u64> hash(tr.g.size());
vec<u64> hash(tr.g.size(), OFFSET);
dfs(
tr, tr.root,
[&](u32 u, u32) {
hash[u] = OFFSET;
},
[](u32, u32) {},
[&](u32 to, u32 u) {
[](u32, u32, u32 = 1) {},
[&](u32 to, u32 u, u32 = 1) {
hash[u] += hasher(hash[to]);
},
[](u32, u32) {});
Expand Down
8 changes: 3 additions & 5 deletions src/code/tree/tree_sumvw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ namespace tifa_libs::graph {

template <class G, class T>
constexpr vec<T> tree_sumvw(G const &tr, vec<T> const &v_weight) {
vec<T> sumvw(tr.g.size());
vec<T> sumvw = v_weight;
dfs(
tr, tr.root,
[&](u32 u, u32) {
sumvw[u] = v_weight[u];
},
[](u32, u32) {},
[&](u32 to, u32 u) {
[](u32, u32, u32 = 1) {},
[&](u32 to, u32 u, u32 = 1) {
sumvw[u] += sumvw[to];
},
[](u32, u32) {});
Expand Down

0 comments on commit 8ec441c

Please sign in to comment.