compro_library

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

View the Project on GitHub siro53/compro_library

:heavy_check_mark: 木の直径
(graph/tree/diameter.hpp)

Depends on

Required by

Verified with

Code

#pragma once

#include <algorithm>
#include <utility>
#include <vector>

#include "../graph_template.hpp"

template <typename Cost = int>
std::pair<Cost, std::vector<int>> get_diameter(const Graph<Cost> &G) {
    std::vector<Cost> depth(G.size());
    std::vector<int> par(G.size(), -1);
    auto dfs = [&](auto &&self, int u, int p, Cost d) -> void {
        depth[u] = d;
        par[u] = p;
        for(const auto &e : G[u]) {
            if(e.to == p) continue;
            self(self, e.to, u, d + e.cost);
        }
    };
    dfs(dfs, 0, -1, 0);
    int from = std::max_element(depth.begin(), depth.end()) - depth.begin();
    dfs(dfs, from, -1, 0);
    int to = std::max_element(depth.begin(), depth.end()) - depth.begin();
    std::vector<int> path = {to};
    while(1) {
        int nxt = par[path.back()];
        if(nxt == -1) break;
        path.push_back(nxt);
    }
    return std::make_pair(depth[to], path);
}
#line 2 "graph/tree/diameter.hpp"

#include <algorithm>
#include <utility>
#include <vector>

#line 2 "graph/graph_template.hpp"

#include <cassert>
#line 5 "graph/graph_template.hpp"

template <typename Cost = int> struct Edge {
    int from, to;
    Cost cost;
    int id;
    Edge() = default;
    explicit Edge(int from, int to, Cost cost = 1, int id = -1)
        : from(from), to(to), cost(cost), id(id) {}
    operator int() const { return to; }
};

template <typename Cost = int> class Graph {
  public:
    Graph() = default;
    explicit Graph(int N) : N(N), M(0), G(N) {}

    inline void add_directed_edge(int from, int to, Cost cost = 1) {
        assert(0 <= from && from < N);
        assert(0 <= to && to < N);
        G[from].emplace_back(from, to, cost, M++);
    }

    inline void add_undirected_edge(int from, int to, Cost cost = 1) {
        assert(0 <= from && from < N);
        assert(0 <= to && to < N);
        G[from].emplace_back(from, to, cost, M);
        G[to].emplace_back(to, from, cost, M++);
    }

    inline size_t size() const { return G.size(); }
    inline std::vector<Edge<Cost>> &operator[](const int &i) { return G[i]; }
    inline const std::vector<Edge<Cost>> &operator[](const int &i) const {
        return G[i];
    }

  protected:
    int N, M;
    std::vector<std::vector<Edge<Cost>>> G;
};

template <class Cost = int> using Edges = std::vector<Edge<Cost>>;
#line 8 "graph/tree/diameter.hpp"

template <typename Cost = int>
std::pair<Cost, std::vector<int>> get_diameter(const Graph<Cost> &G) {
    std::vector<Cost> depth(G.size());
    std::vector<int> par(G.size(), -1);
    auto dfs = [&](auto &&self, int u, int p, Cost d) -> void {
        depth[u] = d;
        par[u] = p;
        for(const auto &e : G[u]) {
            if(e.to == p) continue;
            self(self, e.to, u, d + e.cost);
        }
    };
    dfs(dfs, 0, -1, 0);
    int from = std::max_element(depth.begin(), depth.end()) - depth.begin();
    dfs(dfs, from, -1, 0);
    int to = std::max_element(depth.begin(), depth.end()) - depth.begin();
    std::vector<int> path = {to};
    while(1) {
        int nxt = par[path.back()];
        if(nxt == -1) break;
        path.push_back(nxt);
    }
    return std::make_pair(depth[to], path);
}
Back to top page