This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include "../../../src/template/template.hpp"
#include "../../../src/graph/graph_template.hpp"
#include "../../../src/tree/centroid.hpp"
#include "../../../src/tree/centroid_decomposition.hpp"
#include "../../../src/tree/rerooting.hpp"
#include "../../../src/tree/tree_diameter.hpp"
int main() {
Graph<int> empty(0);
assert(centroid(empty).empty());
{
auto [g, root] = centroid_decomposition(empty);
assert(g.size() == 0);
assert(root == -1);
}
{
auto [d, path] = tree_diameter(empty);
assert(d == 0);
assert(path.empty());
}
auto f1 = [](int a, int b) { return a + b; };
auto f2 = [](int a, int, int) { return a + 1; };
assert((rerooting<int, int>(empty, f1, f2, 0).empty()));
Graph<int> one(1);
assert(centroid(one) == vector<int>{0});
auto [d, path] = tree_diameter(one);
assert(d == 0);
assert(path == vector<int>{0});
int a, b;
cin >> a >> b;
cout << a + b << '\n';
}#line 1 "verify/unit_test/tree/tree_regression.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
#line 4 "src/template/template.hpp"
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/centroid.hpp"
template <typename T>
vector<int> centroid(const Graph<T>& g) {
const int n = g.size();
if(n == 0) return {};
stack<pair<int, int>> st;
st.emplace(0, -1);
vector<int> sz(n), par(n);
while(!st.empty()) {
const pair<int, int> p = st.top();
if(sz[p.first] == 0) {
sz[p.first] = 1;
for(const Edge<T>& e : g[p.first]) {
if(e.to != p.second) {
st.emplace(e.to, p.first);
}
}
} else {
for(const Edge<T>& e : g[p.first]) {
if(e.to != p.second) {
sz[p.first] += sz[e.to];
}
}
par[p.first] = p.second;
st.pop();
}
}
vector<int> ret;
int size = n;
for(int i = 0; i < n; ++i) {
int val = n - sz[i];
for(const Edge<T>& e : g[i]) {
if(e.to != par[i]) {
val = max(val, sz[e.to]);
}
}
if(val < size) size = val, ret.clear();
if(val == size) ret.emplace_back(i);
}
return ret;
}
#line 4 "src/tree/centroid_decomposition.hpp"
template <typename T>
pair<Graph<int>, int> centroid_decomposition(const Graph<T>& g) {
const int n = g.size();
if(n == 0) return {Graph<int>(0), -1};
vector<int> sub(n);
vector<bool> visited(n);
Graph<int> tree(n);
auto get_size = [&](const auto& get_size, const int cur, const int par) -> int {
sub[cur] = 1;
for(const Edge<T>& e : g[cur]) {
if(e.to == par or visited[e.to]) continue;
sub[cur] += get_size(get_size, e.to, cur);
}
return sub[cur];
};
auto get_centroid = [&](const auto& get_centroid, const int cur, const int par, const int mid) -> int {
for(const Edge<T>& e : g[cur]) {
if(e.to == par or visited[e.to]) continue;
if(sub[e.to] > mid) return get_centroid(get_centroid, e.to, cur, mid);
}
return cur;
};
auto dfs = [&](const auto& dfs, const int cur) -> int {
const int centroid = get_centroid(get_centroid, cur, -1, get_size(get_size, cur, -1) / 2);
visited[centroid] = true;
for(const Edge<T>& e : g[centroid]) {
if(visited[e.to]) continue;
const int nex = dfs(dfs, e.to);
if(centroid != nex) tree.add_directed_edge(centroid, nex);
}
visited[centroid] = false;
return centroid;
};
const int root = dfs(dfs, 0);
return {tree, root};
}
#line 4 "src/tree/rerooting.hpp"
template <typename DP, typename T, typename F1, typename F2>
vector<DP> rerooting(const Graph<T>& g, const F1& f1, const F2& f2, const DP& id) {
const int n = g.size();
if(n == 0) return {};
vector<DP> memo(n, id), dp(n, id);
auto dfs = [&](const auto& dfs, const int cur, const int par) -> void {
for(const Edge<T>& e : g[cur]) {
if(e.to == par) continue;
dfs(dfs, e.to, cur);
memo[cur] = f1(memo[cur], f2(memo[e.to], e.to, cur));
}
};
auto efs = [&](const auto& efs, const int cur, const int par, const DP& pval) -> void {
vector<DP> buf;
for(const Edge<T>& e : g[cur]) {
if(e.to == par) continue;
buf.emplace_back(f2(memo[e.to], e.to, cur));
}
vector<DP> head(buf.size() + 1), tail(buf.size() + 1);
head[0] = tail[buf.size()] = id;
for(int i = 0; i < (int)buf.size(); ++i) head[i + 1] = f1(head[i], buf[i]);
for(int i = (int)buf.size() - 1; i >= 0; --i) {
tail[i] = f1(tail[i + 1], buf[i]);
}
dp[cur] = par == -1 ? head.back() : f1(pval, head.back());
int idx = 0;
for(const Edge<T>& e : g[cur]) {
if(e.to == par) continue;
efs(efs, e.to, cur, f2(f1(pval, f1(head[idx], tail[idx + 1])), cur, e));
++idx;
}
};
dfs(dfs, 0, -1);
efs(efs, 0, -1, id);
return dp;
}
#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();
if(n == 0) return {T(0), {}};
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 8 "verify/unit_test/tree/tree_regression.test.cpp"
int main() {
Graph<int> empty(0);
assert(centroid(empty).empty());
{
auto [g, root] = centroid_decomposition(empty);
assert(g.size() == 0);
assert(root == -1);
}
{
auto [d, path] = tree_diameter(empty);
assert(d == 0);
assert(path.empty());
}
auto f1 = [](int a, int b) { return a + b; };
auto f2 = [](int a, int, int) { return a + 1; };
assert((rerooting<int, int>(empty, f1, f2, 0).empty()));
Graph<int> one(1);
assert(centroid(one) == vector<int>{0});
auto [d, path] = tree_diameter(one);
assert(d == 0);
assert(path == vector<int>{0});
int a, b;
cin >> a >> b;
cout << a + b << '\n';
}