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);
        }
    }
  • 相关阅读:
    字符串常量池
    JDK1.8-java.lang.Object类源码阅读
    请给你的Mysql(InnoDB)表加上主键吧
    java线程占多大的内存,占哪里的内存?
    c# 多线程
    c# 遍历文件夹及其所有文件
    T-SQL 查询语句总结
    SQL Server使用LIKE运算符进行匹配查询
    sql server中关于批处理与脚本的简单介绍
    关于SQL Server数据表的五中约束
  • 原文地址:https://www.cnblogs.com/zxhl/p/5690652.html
Copyright © 2011-2022 走看看