zoukankan      html  css  js  c++  java
  • POJ 1655 Balancing Act 树的重心

    Balancing Act
     

    Description

    Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting that node from T. 
    For example, consider the tree: 

    Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two. 

    For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number. 

    Input

    The first line of input contains a single integer t (1 <= t <= 20), the number of test cases. The first line of each test case contains an integer N (1 <= N <= 20,000), the number of congruence. The next N-1 lines each contains two space-separated node numbers that are the endpoints of an edge in the tree. No edge will be listed twice, and all edges will be listed.
     

    Output

     

    For each test case, print a line containing two integers, the number of the node with minimum balance and the balance of that node.
     

    Sample Input

     

    1
    7
    2 6
    1 2
    1 4
    4 5
    3 7
    3 1

    Sample Output

     

    1 2

    题意:

      给你n点的树

      让你删去一个点,剩下的 子树中节点数最多的就是删除这个点的 价值

      求删除哪个点 价值最小就是重心 

    题解:

      来来来

      认识一下什么重心

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    using namespace std;
    const int N = 1e5+20, M = 1e2+10, mod = 1e9+7, inf = 1e9+1000;
    typedef long long ll;
    
    vector<int > G[N];
    int T,n,siz[N],mx,mx1;
    void dfs(int u,int fa) {
        siz[u] = 1;
        int ret = 0;
        for(int i=0;i<G[u].size();i++) {
            int to = G[u][i];
            if(to == fa) continue;
            dfs(to,u);
            siz[u] += siz[to];
            ret = max(ret , siz[to]);
        }
        if(u!=1) ret = max(ret , n - siz[u]);
        if(ret <= mx) {
            mx1 = u;
            mx = ret;
        }
    }
    int main()
    {
        scanf("%d",&T);
        while(T--) {
            scanf("%d",&n);
            for(int i=0;i<N;i++) G[i].clear();
            for(int i=1;i<n;i++) {
                int a,b;
                scanf("%d%d",&a,&b);
                G[a].push_back(b);
                G[b].push_back(a);
            }
            mx = inf;
            dfs(1,-1);
            printf("%d %d
    ",mx1 , mx);
        }
    }
  • 相关阅读:
    Netty源码解析与实战
    什么是序列化id?
    关于idea下tomcat输出日志的问题
    利用jstack 找到异常代码
    mysql 异常 Lock wait timeout exceeded; try restarting transaction;expc=java.sql.SQLExcept
    Spark-Hadoop、Hive、Spark 之间是什么关系?(转)
    转(数据分析的意义)
    按位取反~100=-101
    知识总汇
    前端(以Vue为例)webpack打包后dist文件包如何部署到django后台中
  • 原文地址:https://www.cnblogs.com/zxhl/p/5690652.html
Copyright © 2011-2022 走看看