前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ACMSGURU 134 - Centroid

ACMSGURU 134 - Centroid

作者头像
Reck Zhang
发布2021-08-11 11:00:56
2540
发布2021-08-11 11:00:56
举报
文章被收录于专栏:Reck Zhang

Centroid

Problem Description

You are given an undirected connected graph, with N vertices and N-1 edges (a tree). You must find the centroid(s) of the tree. In order to define the centroid, some integer value will be assosciated to every vertex. Let’s consider the vertex k. If we remove the vertex k from the tree (along with its adjacent edges), the remaining graph will have only N-1 vertices and may be composed of more than one connected components. Each of these components is (obviously) a tree. The value associated to vertex k is the largest number of vertices contained by some connected component in the remaining graph, after the removal of vertex k. All the vertices for which the associated value is minimum are considered centroids.

Input

The first line of the input contains the integer number N (1<=N<=16 000). The next N-1 lines will contain two integers, a and b, separated by blanks, meaning that there exists an edge between vertex a and vertex b.

Output

You should print two lines. The first line should contain the minimum value associated to the centroid(s) and the number of centroids. The second line should contain the list of vertices which are centroids, sorted in ascending order.

Sample Input

代码语言:javascript
复制
7
1 2
2 3
2 4
1 5
5 6
6 7

Sample Output

代码语言:javascript
复制
3 1
1

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>

int main() {
//    std::ios::sync_with_stdio(false);

    int n;
    std::cin >> n;

    std::vector<bool> is_root(n + 1, true);
    std::map<int, std::vector<int>> tree{};
    for(int i = 1; i < n; i++) {
        int a, b;
        std::cin >> a >> b;
        is_root[b] = false;
        tree[a].push_back(b);
    }
    for(int i = 1; i <= n; i++) {
        if(tree.find(i) == tree.end()) {
            tree[i] = std::vector<int>{};
        }
    }

    int root_index = -1;
    for(int i = 1; i <= n; i++) {
        if(is_root[i]) {
            root_index = i;
            break;
        }
    }

    std::vector<int> node_count(n + 1, 0);
    auto dfs_count = std::function<int(int)>{};
    dfs_count = [&dfs_count, &node_count, &tree](int index) -> int {
        if(tree.find(index) == tree.end()) {
            return 0;
        }
        int sum = 0;
        for(int next_index : tree[index]) {
            node_count[next_index] = dfs_count(next_index);
            sum += node_count[next_index];
        }
        return node_count[index] = sum + 1;
    };
    dfs_count(root_index);

    std::vector<int> node_centroid(n + 1, 1);
    int centroid_value = INT_MAX;
    auto dfs_get_centroid = std::function<void(int, int)>{};
    dfs_get_centroid = [&dfs_get_centroid, &node_count, &node_centroid, &centroid_value, &tree, &root_index](int index, int father_index) -> void {
        if(father_index != -1) {
            node_centroid[index] = std::max(node_centroid[index], node_count[root_index] - node_count[index]);
        }
        for(auto child_index : tree[index]) {
            node_centroid[index] = std::max(node_centroid[index], node_count[child_index]);
            dfs_get_centroid(child_index, index);
        }
        centroid_value = std::min(centroid_value, node_centroid[index]);
    };
    dfs_get_centroid(root_index, -1);

    std::vector<int> res{};
    for(int i = 1; i <= n; i++) {
        if(node_centroid[i] == centroid_value) {
            res.push_back(i);
        }
    }
    std::sort(res.begin(), res.end(), std::less<>{});

    std::cout << centroid_value << " " << res.size() << std::endl;
    for(auto index : res) {
        std::cout << index << " ";
    }

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-01-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Centroid
    • Problem Description
      • Input
        • Output
          • Sample Input
            • Sample Output
              • Solution
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档