Luzhiled's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ei1333/library

:question: Tree-Diameter(木の直径)
(graph/tree/tree-diameter.hpp)

概要

木の直径を求める.

適当な頂点から DFS して最も遠い点 $u$ を求める, $u$ から DFS して最も遠い点 $v$ を見つけると, そのペア $(u, v)$ が直径の端点.

使い方

計算量

$O(V)$

Depends on

Verified with

Code

#pragma once

#include "../graph-template.hpp"

/**
 * @brief Tree-Diameter(木の直径)
 * @docs docs/tree-diameter.md
 */
template< typename T = int >
struct TreeDiameter : Graph< T > {
public:
  using Graph< T >::Graph;
  using Graph< T >::g;
  vector< Edge< T > > path;

  T build() {
    to.assign(g.size(), -1);
    auto p = dfs(0, -1);
    auto q = dfs(p.second, -1);

    int now = p.second;
    while(now != q.second) {
      for(auto &e : g[now]) {
        if(to[now] == e.to) {
          path.emplace_back(e);
        }
      }
      now = to[now];
    }
    return q.first;
  }

  explicit TreeDiameter(const Graph< T > &g) : Graph< T >(g) {}

private:
  vector< int > to;

  pair< T, int > dfs(int idx, int par) {
    pair< T, int > ret(0, idx);
    for(auto &e : g[idx]) {
      if(e.to == par) continue;
      auto cost = dfs(e.to, idx);
      cost.first += e.cost;
      if(ret < cost) {
        ret = cost;
        to[idx] = e.to;
      }
    }
    return ret;
  }
};
#line 2 "graph/tree/tree-diameter.hpp"

#line 2 "graph/graph-template.hpp"

/**
 * @brief Graph Template(グラフテンプレート)
 */
template< typename T = int >
struct Edge {
  int from, to;
  T cost;
  int idx;

  Edge() = default;

  Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}

  operator int() const { return to; }
};

template< typename T = int >
struct Graph {
  vector< vector< Edge< T > > > g;
  int es;

  Graph() = default;

  explicit Graph(int n) : g(n), es(0) {}

  size_t size() const {
    return g.size();
  }

  void add_directed_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es++);
  }

  void add_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es);
    g[to].emplace_back(to, from, cost, es++);
  }

  void read(int M, int padding = -1, bool weighted = false, bool directed = false) {
    for(int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      a += padding;
      b += padding;
      T c = T(1);
      if(weighted) cin >> c;
      if(directed) add_directed_edge(a, b, c);
      else add_edge(a, b, c);
    }
  }

  inline vector< Edge< T > > &operator[](const int &k) {
    return g[k];
  }

  inline const vector< Edge< T > > &operator[](const int &k) const {
    return g[k];
  }
};

template< typename T = int >
using Edges = vector< Edge< T > >;
#line 4 "graph/tree/tree-diameter.hpp"

/**
 * @brief Tree-Diameter(木の直径)
 * @docs docs/tree-diameter.md
 */
template< typename T = int >
struct TreeDiameter : Graph< T > {
public:
  using Graph< T >::Graph;
  using Graph< T >::g;
  vector< Edge< T > > path;

  T build() {
    to.assign(g.size(), -1);
    auto p = dfs(0, -1);
    auto q = dfs(p.second, -1);

    int now = p.second;
    while(now != q.second) {
      for(auto &e : g[now]) {
        if(to[now] == e.to) {
          path.emplace_back(e);
        }
      }
      now = to[now];
    }
    return q.first;
  }

  explicit TreeDiameter(const Graph< T > &g) : Graph< T >(g) {}

private:
  vector< int > to;

  pair< T, int > dfs(int idx, int par) {
    pair< T, int > ret(0, idx);
    for(auto &e : g[idx]) {
      if(e.to == par) continue;
      auto cost = dfs(e.to, idx);
      cost.first += e.cost;
      if(ret < cost) {
        ret = cost;
        to[idx] = e.to;
      }
    }
    return ret;
  }
};
Back to top page