zoukankan      html  css  js  c++  java
  • poj-1655 Balancing Act(树的重心+树形dp)

    题目链接:

    Balancing Act

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11845   Accepted: 4993

    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

    题意:

    问给的一棵树的重心是哪个节点以及把这个节点去掉后连通块节点个数的最大值;

    思路:

    dfs,找出所有节点的子树节点的个数;再找出去掉这个节点后最大连通块的节点数更新答案就好了;

    AC代码:

    //#include <bits/stdc++.h>
    #include <vector>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <map>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef long long LL;
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=2e5+10;
    const int maxn=1005;
    
    int n,son[N],ans,num;
    vector<int>ve[N];
    
    void dfs(int x,int fa)
    {
        int len=ve[x].size(),mmax=0;
        son[x]=1;
        for(int i=0;i<len;i++)
        {
            int y=ve[x][i];
            if(y==fa)continue;
            dfs(y,x);
            son[x]+=son[y];
            if(son[y]>mmax)
                mmax=son[y];
        }
    
        int d=max(mmax,n-son[x]);
        if(d<=num)
        {
            if(d==num)
            {
                if(x<ans)ans=x;
            }
            else ans=x;
            num=d;
        }
    }
    
    int main()
    {
        int t;
        read(t);
        while(t--)
        {
            read(n);
            for(int i=0;i<=n;i++)ve[i].clear();
            int x,y;
            for(int i=1;i<n;i++)
            {
                read(x);read(y);
                ve[x].push_back(y);
                ve[y].push_back(x);
            }
            ans=1;
            num=50000000;
            dfs(1,-1);
            cout<<ans<<" "<<num<<"
    ";
        }
            return 0;
    }
  • 相关阅读:
    Spring中 @PathVariable
    消息队列中点对点与发布订阅区别
    rabbitMQ下载地址
    当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
    String和StringBuilder、StringBuffer的区别?
    char 型变量中能不能存贮一个中文汉字,为什么?
    抽象类(abstract class)和接口(interface)有什么异同?
    静态嵌套类(Static Nested Class)和内部类(Inner Class)的不同?
    抽象的(abstract)方法是否可同时是静态的(static),是否可同时是本地方法(native),是否可同时被synchronized修饰?
    阐述静态变量和实例变量的区别。
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5616201.html
Copyright © 2011-2022 走看看