This documentation is automatically generated by online-judge-tools/verification-helper
#include "src/tree/tree_diameter.hpp"
pair<T, vector<int>> tree_diameter(Graph<T> g)
$n$ 頂点の木 g
を与えると,その木の直径のうち1つを選んで,パス長と,パスの頂点を順に格納した配列を返します.
木の直径になりうるパスが複数あるとき,どのパスが選ばれるかは未定義です.
制約
T
は int / uint / ll / ull / double / long double
g
は木計算量
#pragma once
#include "../template/template.hpp"
#include "../graph/graph_template.hpp"
template <typename T>
pair<T, vector<int>> tree_diameter(const Graph<T>& g) {
const int n = g.size();
vector<T> depth(n);
vector<int> par(n);
auto dfs = [&](const auto& dfs, const int v, const int p, const T& d) -> void {
depth[v] = d;
par[v] = p;
for(const Edge<T>& e : g[v]) {
if(e.to == p) continue;
dfs(dfs, e.to, v, d + e.cost);
}
};
int s = 0;
for(int i = 0; i < 2; ++i) {
dfs(dfs, s, -1, 0);
T ma = -1;
for(int j = 0; j < n; ++j) {
if(depth[j] > ma) {
ma = depth[j];
s = j;
}
}
}
vector<int> path;
const T diameter = depth[s];
while(s != -1) {
path.emplace_back(s);
s = par[s];
}
return {diameter, path};
}
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
SetupIO() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(30);
}
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
int from, to;
T cost;
int idx;
Edge()
: from(-1), to(-1), cost(-1), idx(-1) {}
Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
: from(from), to(to), cost(cost), idx(idx) {}
operator int() const {
return to;
}
};
template <typename T>
struct Graph {
Graph(const int N)
: n(N), es(0), g(N) {}
int size() const {
return n;
}
int edge_size() const {
return es;
}
void add_edge(const int from, const int to, const T& cost = 1) {
assert(0 <= from and from < n);
assert(0 <= to and to < n);
g[from].emplace_back(from, to, cost, es);
g[to].emplace_back(to, from, cost, es++);
}
void add_directed_edge(const int from, const int to, const T& cost = 1) {
assert(0 <= from and from < n);
assert(0 <= to and to < n);
g[from].emplace_back(from, to, cost, es++);
}
inline vector<Edge<T>>& operator[](const int& k) {
assert(0 <= k and k < n);
return g[k];
}
inline const vector<Edge<T>>& operator[](const int& k) const {
assert(0 <= k and k < n);
return g[k];
}
private:
int n, es;
vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/tree/tree_diameter.hpp"
template <typename T>
pair<T, vector<int>> tree_diameter(const Graph<T>& g) {
const int n = g.size();
vector<T> depth(n);
vector<int> par(n);
auto dfs = [&](const auto& dfs, const int v, const int p, const T& d) -> void {
depth[v] = d;
par[v] = p;
for(const Edge<T>& e : g[v]) {
if(e.to == p) continue;
dfs(dfs, e.to, v, d + e.cost);
}
};
int s = 0;
for(int i = 0; i < 2; ++i) {
dfs(dfs, s, -1, 0);
T ma = -1;
for(int j = 0; j < n; ++j) {
if(depth[j] > ma) {
ma = depth[j];
s = j;
}
}
}
vector<int> path;
const T diameter = depth[s];
while(s != -1) {
path.emplace_back(s);
s = par[s];
}
return {diameter, path};
}