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);
        }
    }
  • 相关阅读:
    小、快、简、易、强的“银弹”— fastm
    使用Creative suite 3和Flex Builder3实现Flex 3的换肤
    Apache HTTP Server 与 Tomcat 的三种连接方式介绍
    iframe自动适应付窗口的大小变换
    Flash网络游戏开发入门经验共享
    比较详细的 Linux Top 命令解析
    HttpContext是干什么的
    asp.net,cookie,写cookie,取cookie 的方法
    为什么我们不要 .NET 程序员
    在Ubuntu上安装使用深度影音&深度音乐(推荐)
  • 原文地址:https://www.cnblogs.com/zxhl/p/5690652.html
Copyright © 2011-2022 走看看