#include "tree/dynamic-diameter.hpp"
#pragma once #include <algorithm> #include <cassert> #include <functional> #include <map> #include <utility> #include <vector> using namespace std; #include "../graph/graph-template.hpp" #include "static-top-tree-vertex-based.hpp" namespace DynamicDiameterImpl { template <typename T> struct HalfPath { T d; int u; friend HalfPath max(const HalfPath& lhs, const HalfPath& rhs) { if (lhs.d != rhs.d) return lhs.d > rhs.d ? lhs : rhs; return lhs.u > rhs.u ? lhs : rhs; } }; template <typename T> struct Path { T d; int u, v; friend Path max(const Path& lhs, const Path& rhs) { if (lhs.d != rhs.d) return lhs.d > rhs.d ? lhs : rhs; if (lhs.u != rhs.u) return lhs.u > rhs.u ? lhs : rhs; return lhs.v > rhs.v ? lhs : rhs; } }; template <typename T> struct L { Path<T> dia; HalfPath<T> d1, d2; }; template <typename T> struct H { Path<T> dia; HalfPath<T> pd, cd; T p_to_c; int p, c; }; template <typename T> H<T> vertex(T x, int i) { H<T> r; r.dia = {x, i, i}; r.pd = r.cd = {x, i}; r.p_to_c = x; r.p = r.c = i; return r; } template <typename T> H<T> compress(const H<T>& p, const H<T>& c) { H<T> r; r.dia = max(max(p.dia, c.dia), {p.cd.d + c.pd.d, p.cd.u, c.pd.u}); r.pd = max(p.pd, {p.p_to_c + c.pd.d, c.pd.u}); r.cd = max(c.cd, {c.p_to_c + p.cd.d, p.cd.u}); r.p_to_c = p.p_to_c + c.p_to_c; r.p = p.p, r.c = c.c; return r; } template <typename T> L<T> rake(const L<T>& a, const L<T>& b) { L<T> r; r.dia = max(a.dia, b.dia); if (a.d1.d > b.d1.d) { r.d1 = a.d1; r.d2 = max(a.d2, b.d1); } else { r.d1 = b.d1; r.d2 = max(b.d2, a.d1); } return r; } template <typename T> L<T> add_edge(const H<T>& a) { L<T> r; r.dia = a.dia; r.d1 = a.pd; r.d2 = {0, -1}; return r; } template <typename T> H<T> add_vertex(const L<T>& a, T x, int i) { H<T> r; r.dia = max(a.dia, {a.d1.d + x + a.d2.d, a.d1.u, a.d2.u}); r.pd = r.cd = {a.d1.d + x, a.d1.u}; r.p_to_c = x; r.p = r.c = i; return r; } template <typename T> struct Aux_Tree { int N, _buf; const WeightedGraph<T>& g; vector<vector<int>> aux; vector<T> w; map<pair<int, int>, int> e_to_id; Aux_Tree(const WeightedGraph<T>& _g) : g(_g) { N = g.size(); aux.resize(2 * N - 1); w.resize(2 * N - 1); _buf = N; dfs(0, -1); assert(_buf == 2 * N - 1); } void dfs(int c, int p) { for (auto& d : g[c]) { if (d == p) continue; int id = _buf++; aux[id].push_back(c), aux[c].push_back(id); aux[id].push_back(d), aux[d].push_back(id); w[id] = d.cost; e_to_id[minmax<int>(c, d)] = id; dfs(d, c); } } }; template <typename T> struct DynamicDiameter { const WeightedGraph<T>& g; int n; Aux_Tree<T> aux; HeavyLightDecomposition<vector<vector<int>>> hld; DPonStaticTopTreeVertexBased< vector<vector<int>>, H<T>, L<T>, function<H<T>(int)>, function<H<T>(const H<T>&, const H<T>&)>, function<L<T>(const L<T>&, const L<T>&)>, function<L<T>(const H<T>&)>, function<H<T>(const L<T>&, int)>> dp; DynamicDiameter(const WeightedGraph<T>& _g) : g(_g), n(g.size()), aux(g), hld(aux.aux), dp( hld, [&](int i) { return vertex(aux.w[i], i < n ? i : -1); }, [&](const H<T>& p, const H<T>& c) { return compress(p, c); }, [&](const L<T>& a, const L<T>& b) { return rake(a, b); }, [&](const H<T>& a) { return add_edge(a); }, [&](const L<T>& a, int i) { return add_vertex(a, aux.w[i], i < n ? i : -1); }) {} pair<T, pair<int, int>> get() { auto [d, u, v] = dp.get().dia; return make_pair(d, make_pair(u, v)); } void update(int u, int v, T x) { assert(aux.e_to_id.count(minmax(u, v))); int i = aux.e_to_id[minmax(u, v)]; aux.w[i] = x; dp.update(i); } }; } // namespace DynamicDiameterImpl using DynamicDiameterImpl::DynamicDiameter;
#line 2 "tree/dynamic-diameter.hpp" #include <algorithm> #include <cassert> #include <functional> #include <map> #include <utility> #include <vector> using namespace std; #line 2 "graph/graph-template.hpp" template <typename T> struct edge { int src, to; T cost; edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {} edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template <typename T> using Edges = vector<edge<T>>; template <typename T> using WeightedGraph = vector<Edges<T>>; using UnweightedGraph = vector<vector<int>>; // Input of (Unweighted) Graph UnweightedGraph graph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { UnweightedGraph g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; if (is_1origin) x--, y--; g[x].push_back(y); if (!is_directed) g[y].push_back(x); } return g; } // Input of Weighted Graph template <typename T> WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { WeightedGraph<T> g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; cin >> c; if (is_1origin) x--, y--; g[x].emplace_back(x, y, c); if (!is_directed) g[y].emplace_back(y, x, c); } return g; } // Input of Edges template <typename T> Edges<T> esgraph([[maybe_unused]] int N, int M, int is_weighted = true, bool is_1origin = true) { Edges<T> es; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; es.emplace_back(x, y, c); } return es; } // Input of Adjacency Matrix template <typename T> vector<vector<T>> adjgraph(int N, int M, T INF, int is_weighted = true, bool is_directed = false, bool is_1origin = true) { vector<vector<T>> d(N, vector<T>(N, INF)); for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; d[x][y] = c; if (!is_directed) d[y][x] = c; } return d; } /** * @brief グラフテンプレート * @docs docs/graph/graph-template.md */ #line 2 "tree/static-top-tree-vertex-based.hpp" #line 6 "tree/static-top-tree-vertex-based.hpp" using namespace std; #line 2 "tree/convert-tree.hpp" #line 4 "tree/convert-tree.hpp" template <typename T> struct has_cost { private: template <typename U> static auto confirm(U u) -> decltype(u.cost, std::true_type()); static auto confirm(...) -> std::false_type; public: enum : bool { value = decltype(confirm(std::declval<T>()))::value }; }; template <typename T> vector<vector<T>> inverse_tree(const vector<vector<T>>& g) { int N = (int)g.size(); vector<vector<T>> rg(N); for (int i = 0; i < N; i++) { for (auto& e : g[i]) { if constexpr (is_same<T, int>::value) { rg[e].push_back(i); } else if constexpr (has_cost<T>::value) { rg[e].emplace_back(e.to, i, e.cost); } else { assert(0); } } } return rg; } template <typename T> vector<vector<T>> rooted_tree(const vector<vector<T>>& g, int root = 0) { int N = (int)g.size(); vector<vector<T>> rg(N); vector<char> v(N, false); v[root] = true; queue<int> que; que.emplace(root); while (!que.empty()) { auto p = que.front(); que.pop(); for (auto& e : g[p]) { if (v[e] == false) { v[e] = true; que.push(e); rg[p].push_back(e); } } } return rg; } /** * @brief 根付き木・逆辺からなる木への変換 */ #line 2 "tree/heavy-light-decomposition.hpp" #line 4 "tree/heavy-light-decomposition.hpp" template <typename G> struct HeavyLightDecomposition { private: void dfs_sz(int cur) { size[cur] = 1; for (auto& dst : g[cur]) { if (dst == par[cur]) { if (g[cur].size() >= 2 && int(dst) == int(g[cur][0])) swap(g[cur][0], g[cur][1]); else continue; } depth[dst] = depth[cur] + 1; par[dst] = cur; dfs_sz(dst); size[cur] += size[dst]; if (size[dst] > size[g[cur][0]]) { swap(dst, g[cur][0]); } } } void dfs_hld(int cur) { down[cur] = id++; for (auto dst : g[cur]) { if (dst == par[cur]) continue; nxt[dst] = (int(dst) == int(g[cur][0]) ? nxt[cur] : int(dst)); dfs_hld(dst); } up[cur] = id; } // [u, v) vector<pair<int, int>> ascend(int u, int v) const { vector<pair<int, int>> res; while (nxt[u] != nxt[v]) { res.emplace_back(down[u], down[nxt[u]]); u = par[nxt[u]]; } if (u != v) res.emplace_back(down[u], down[v] + 1); return res; } // (u, v] vector<pair<int, int>> descend(int u, int v) const { if (u == v) return {}; if (nxt[u] == nxt[v]) return {{down[u] + 1, down[v]}}; auto res = descend(u, par[nxt[v]]); res.emplace_back(down[nxt[v]], down[v]); return res; } public: G& g; int root, id; vector<int> size, depth, down, up, nxt, par; HeavyLightDecomposition(G& _g, int _root = 0) : g(_g), root(_root), id(0), size(g.size(), 0), depth(g.size(), 0), down(g.size(), -1), up(g.size(), -1), nxt(g.size(), root), par(g.size(), root) { dfs_sz(root); dfs_hld(root); } pair<int, int> idx(int i) const { return make_pair(down[i], up[i]); } template <typename F> void path_query(int u, int v, bool vertex, const F& f) { int l = lca(u, v); for (auto&& [a, b] : ascend(u, l)) { int s = a + 1, t = b; s > t ? f(t, s) : f(s, t); } if (vertex) f(down[l], down[l] + 1); for (auto&& [a, b] : descend(l, v)) { int s = a, t = b + 1; s > t ? f(t, s) : f(s, t); } } template <typename F> void path_noncommutative_query(int u, int v, bool vertex, const F& f) { int l = lca(u, v); for (auto&& [a, b] : ascend(u, l)) f(a + 1, b); if (vertex) f(down[l], down[l] + 1); for (auto&& [a, b] : descend(l, v)) f(a, b + 1); } template <typename F> void subtree_query(int u, bool vertex, const F& f) { f(down[u] + int(!vertex), up[u]); } int lca(int a, int b) { while (nxt[a] != nxt[b]) { if (down[a] < down[b]) swap(a, b); a = par[nxt[a]]; } return depth[a] < depth[b] ? a : b; } int dist(int a, int b) { return depth[a] + depth[b] - depth[lca(a, b)] * 2; } }; /** * @brief Heavy Light Decomposition(重軽分解) * @docs docs/tree/heavy-light-decomposition.md */ #line 10 "tree/static-top-tree-vertex-based.hpp" namespace StaticTopTreeVertexBasedImpl { enum Type { Vertex, Compress, Rake, Add_Edge, Add_Vertex }; template <typename G> struct StaticTopTreeVertexBased { const HeavyLightDecomposition<G>& hld; vector<vector<int>> g; int root; // 元の木の root int tt_root; // top tree の root vector<int> P, L, R; vector<Type> T; StaticTopTreeVertexBased(const HeavyLightDecomposition<G>& _hld) : hld(_hld) { root = hld.root; g = rooted_tree(hld.g, root); int n = g.size(); P.resize(n, -1), L.resize(n, -1), R.resize(n, -1); T.resize(n, Type::Vertex); build(); } private: int add(int l, int r, Type t) { if (t == Type::Compress or t == Type::Rake) { assert(l != -1 and r != -1); } if (t == Type::Add_Edge) { assert(l != -1 and r == -1); } assert(t != Type::Vertex and t != Type::Add_Vertex); int k = P.size(); P.push_back(-1), L.push_back(l), R.push_back(r), T.push_back(t); if (l != -1) P[l] = k; if (r != -1) P[r] = k; return k; } int add2(int k, int l, int r, Type t) { assert(k < (int)g.size()); assert(t == Type::Vertex or t == Type::Add_Vertex); if (t == Type::Vertex) { assert(l == -1 and r == -1); } else { assert(l != -1 and r == -1); } P[k] = -1, L[k] = l, R[k] = r, T[k] = t; if (l != -1) P[l] = k; if (r != -1) P[r] = k; return k; } pair<int, int> merge(const vector<pair<int, int>>& a, Type t) { assert(!a.empty()); if (a.size() == 1) return a[0]; int sum_s = 0; for (auto& [_, s] : a) sum_s += s; vector<pair<int, int>> b, c; for (auto& [i, s] : a) { (sum_s > s ? b : c).emplace_back(i, s); sum_s -= s * 2; } auto [i, si] = merge(b, t); auto [j, sj] = merge(c, t); return {add(i, j, t), si + sj}; } pair<int, int> compress(int i) { vector<pair<int, int>> chs; while (true) { chs.push_back(add_vertex(i)); if (g[i].empty()) break; i = g[i][0]; } return merge(chs, Type::Compress); } pair<int, int> rake(int i) { vector<pair<int, int>> chs; for (int j = 1; j < (int)g[i].size(); j++) chs.push_back(add_edge(g[i][j])); if (chs.empty()) return {-1, 0}; return merge(chs, Type::Rake); } pair<int, int> add_edge(int i) { auto [j, sj] = compress(i); return {add(j, -1, Type::Add_Edge), sj}; } pair<int, int> add_vertex(int i) { auto [j, sj] = rake(i); return {add2(i, j, -1, j == -1 ? Type::Vertex : Type::Add_Vertex), sj + 1}; } void build() { auto [i, n] = compress(root); assert((int)g.size() == n); tt_root = i; } }; template <typename G, typename Path, typename Point, typename Vertex, typename Compress, typename Rake, typename Add_edge, typename Add_vertex> struct DPonStaticTopTreeVertexBased { const StaticTopTreeVertexBased<G> tt; vector<Path> path; vector<Point> point; const Vertex vertex; const Compress compress; const Rake rake; const Add_edge add_edge; const Add_vertex add_vertex; DPonStaticTopTreeVertexBased(const HeavyLightDecomposition<G>& hld, const Vertex& _vertex, const Compress& _compress, const Rake& _rake, const Add_edge& _add_edge, const Add_vertex& _add_vertex) : tt(hld), vertex(_vertex), compress(_compress), rake(_rake), add_edge(_add_edge), add_vertex(_add_vertex) { int n = tt.P.size(); path.resize(n), point.resize(n); dfs(tt.tt_root); } Path get() { return path[tt.tt_root]; } void update(int k) { while (k != -1) _update(k), k = tt.P[k]; } private: void _update(int k) { if (tt.T[k] == Type::Vertex) { path[k] = vertex(k); } else if (tt.T[k] == Type::Compress) { path[k] = compress(path[tt.L[k]], path[tt.R[k]]); } else if (tt.T[k] == Type::Rake) { point[k] = rake(point[tt.L[k]], point[tt.R[k]]); } else if (tt.T[k] == Type::Add_Edge) { point[k] = add_edge(path[tt.L[k]]); } else { path[k] = add_vertex(point[tt.L[k]], k); } } void dfs(int k) { if (tt.L[k] != -1) dfs(tt.L[k]); if (tt.R[k] != -1) dfs(tt.R[k]); _update(k); } }; } // namespace StaticTopTreeVertexBasedImpl using StaticTopTreeVertexBasedImpl::DPonStaticTopTreeVertexBased; using StaticTopTreeVertexBasedImpl::StaticTopTreeVertexBased; /* // template using Path = ; using Point = ; auto vertex = [&](int i) -> Path { }; auto compress = [&](const Path& p, const Path& c) -> Path { }; auto rake = [&](const Point& a, const Point& b) -> Point { }; auto add_edge = [&](const Path& a) -> Point { }; auto add_vertex = [&](const Point& a, int i) -> Path { }; HeavyLightDecomposition hld{g}; DPonStaticTopTreeVertexBased<vector<vector<int>>, Path, Point, decltype(vertex), decltype(compress), decltype(rake), decltype(add_edge), decltype(add_vertex)> dp(hld, vertex, compress, rake, add_edge, add_vertex); */ /** * @brief Static Top Tree */ #line 13 "tree/dynamic-diameter.hpp" namespace DynamicDiameterImpl { template <typename T> struct HalfPath { T d; int u; friend HalfPath max(const HalfPath& lhs, const HalfPath& rhs) { if (lhs.d != rhs.d) return lhs.d > rhs.d ? lhs : rhs; return lhs.u > rhs.u ? lhs : rhs; } }; template <typename T> struct Path { T d; int u, v; friend Path max(const Path& lhs, const Path& rhs) { if (lhs.d != rhs.d) return lhs.d > rhs.d ? lhs : rhs; if (lhs.u != rhs.u) return lhs.u > rhs.u ? lhs : rhs; return lhs.v > rhs.v ? lhs : rhs; } }; template <typename T> struct L { Path<T> dia; HalfPath<T> d1, d2; }; template <typename T> struct H { Path<T> dia; HalfPath<T> pd, cd; T p_to_c; int p, c; }; template <typename T> H<T> vertex(T x, int i) { H<T> r; r.dia = {x, i, i}; r.pd = r.cd = {x, i}; r.p_to_c = x; r.p = r.c = i; return r; } template <typename T> H<T> compress(const H<T>& p, const H<T>& c) { H<T> r; r.dia = max(max(p.dia, c.dia), {p.cd.d + c.pd.d, p.cd.u, c.pd.u}); r.pd = max(p.pd, {p.p_to_c + c.pd.d, c.pd.u}); r.cd = max(c.cd, {c.p_to_c + p.cd.d, p.cd.u}); r.p_to_c = p.p_to_c + c.p_to_c; r.p = p.p, r.c = c.c; return r; } template <typename T> L<T> rake(const L<T>& a, const L<T>& b) { L<T> r; r.dia = max(a.dia, b.dia); if (a.d1.d > b.d1.d) { r.d1 = a.d1; r.d2 = max(a.d2, b.d1); } else { r.d1 = b.d1; r.d2 = max(b.d2, a.d1); } return r; } template <typename T> L<T> add_edge(const H<T>& a) { L<T> r; r.dia = a.dia; r.d1 = a.pd; r.d2 = {0, -1}; return r; } template <typename T> H<T> add_vertex(const L<T>& a, T x, int i) { H<T> r; r.dia = max(a.dia, {a.d1.d + x + a.d2.d, a.d1.u, a.d2.u}); r.pd = r.cd = {a.d1.d + x, a.d1.u}; r.p_to_c = x; r.p = r.c = i; return r; } template <typename T> struct Aux_Tree { int N, _buf; const WeightedGraph<T>& g; vector<vector<int>> aux; vector<T> w; map<pair<int, int>, int> e_to_id; Aux_Tree(const WeightedGraph<T>& _g) : g(_g) { N = g.size(); aux.resize(2 * N - 1); w.resize(2 * N - 1); _buf = N; dfs(0, -1); assert(_buf == 2 * N - 1); } void dfs(int c, int p) { for (auto& d : g[c]) { if (d == p) continue; int id = _buf++; aux[id].push_back(c), aux[c].push_back(id); aux[id].push_back(d), aux[d].push_back(id); w[id] = d.cost; e_to_id[minmax<int>(c, d)] = id; dfs(d, c); } } }; template <typename T> struct DynamicDiameter { const WeightedGraph<T>& g; int n; Aux_Tree<T> aux; HeavyLightDecomposition<vector<vector<int>>> hld; DPonStaticTopTreeVertexBased< vector<vector<int>>, H<T>, L<T>, function<H<T>(int)>, function<H<T>(const H<T>&, const H<T>&)>, function<L<T>(const L<T>&, const L<T>&)>, function<L<T>(const H<T>&)>, function<H<T>(const L<T>&, int)>> dp; DynamicDiameter(const WeightedGraph<T>& _g) : g(_g), n(g.size()), aux(g), hld(aux.aux), dp( hld, [&](int i) { return vertex(aux.w[i], i < n ? i : -1); }, [&](const H<T>& p, const H<T>& c) { return compress(p, c); }, [&](const L<T>& a, const L<T>& b) { return rake(a, b); }, [&](const H<T>& a) { return add_edge(a); }, [&](const L<T>& a, int i) { return add_vertex(a, aux.w[i], i < n ? i : -1); }) {} pair<T, pair<int, int>> get() { auto [d, u, v] = dp.get().dia; return make_pair(d, make_pair(u, v)); } void update(int u, int v, T x) { assert(aux.e_to_id.count(minmax(u, v))); int i = aux.e_to_id[minmax(u, v)]; aux.w[i] = x; dp.update(i); } }; } // namespace DynamicDiameterImpl using DynamicDiameterImpl::DynamicDiameter;