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);
        }
    }
  • 相关阅读:
    WPF在XAML的资源中定义空字符串String.Empty
    WPF中定义TabItem的可选区域(特别是当使用Label来呈现Header时)
    反转ListBox的ListBoxItem(控件级别,不是数据的反转)
    WPF获取相对位置、坐标的方法
    WPF为ItemsControl设置ItemsPanelTemplate
    WPF中List的Add()与Insert()方法的区别
    返回表达式列表中最小值least(exp1,exp2,exp3,……,expn)
    oracle_基本SQL语言
    JS 实现的浏览器系统通知 iNotify.js
    RHCE认证考前辅导
  • 原文地址:https://www.cnblogs.com/zxhl/p/5690652.html
Copyright © 2011-2022 走看看