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);
        }
    }
  • 相关阅读:
    flash动态加载多张图片
    使用ASPJPEG添加水印的方法
    利用数据库复制技术 实现数据同步更新
    ASP.NET 2.0中发送电子邮件
    .net2.0 自定义CheckBoxList验证控件
    c#动态创建存储过程中,提示'go' 附近有语法错误解决方案
    MSDB数据库置疑状态的解决方法
    ASP.NET发送邮件_相关参数
    SQL数据库msdb置疑(急)
    SQL2000数据库脱机/只读/紧急模式 修复
  • 原文地址:https://www.cnblogs.com/zxhl/p/5690652.html
Copyright © 2011-2022 走看看