1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once


#include <utility>
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "snippet/aliases.hpp"
#include "graph/tree_diamiter.hpp"


namespace uni {


template<class Graph>
auto tree_centers(const Graph& tree) {
    std::vector<typename Graph::node_type> prev(std::ranges::size(tree), -1);
    auto [ diam, v ] = tree_diamiter(tree, &prev);

    std::vector<typename Graph::node_type> res;

    {
        for(typename Graph::size_type i = 0; i < diam / 2; ++i) {
            v = prev[v];
        }
        res.push_back(v);
        if(diam % 2 == 1) res.push_back(prev[v]);
    }

    {
        auto rest = std::ranges::unique(res);
        res.erase(std::ranges::begin(rest), std::ranges::end(rest));
    }

    return std::make_pair(diam, res);
}


template<class Graph>
std::size_t tree_hash(const Graph& tree, typename Graph::node_type v, typename Graph::node_type p = -1) {
    static std::map<std::vector<typename Graph::node_type>, std::size_t> vals;

    std::vector<typename Graph::node_type> children;

    for(const auto nv : tree[v]) {
        if(nv == p) continue;
        children.emplace_back(tree_hash(tree, nv, v));
    }

    std::ranges::sort(children);

    if(!vals.contains(children)) {
        vals[children] = 0;
        vals[children] = std::ranges::size(vals);
    }

    return vals[children];
}


} // namespace uni