ja / en
Overview

getGraphDiameter(木の直径を求める)

2024/07/30
1 min read

Purpose

Find the diameter of a tree.

Time Complexity

O(n)O(n)

Depends

Usage

Declaration

auto res = getGraphDiameter(g,s);

Get the diameter using res.cost. Get the vertices forming the diameter using res.u and res.v.

Implementation

struct getGraphDiameter_return {
int u, v, cost;
};
getGraphDiameter_return getGraphDiameter(DirectedGraph &_g) {
auto u = getFarthestVertex(_g, 0);
auto v = getFarthestVertex(_g, u.v);
return {u.v, v.v, v.cost};
}

Verify👍

https://atcoder.jp/contests/typical90/submissions/57586680