zoukankan      html  css  js  c++  java
  • POJ 1655

    Description

    Consider a tree T with N ((1 le N le 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:
    1.1
    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 le t le 20)), the number of test cases. The first line of each test case contains an integer N ((1 le N le 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

    Resume

    定义树上节点的balance为删除该点后产生的森林中最大的树的节点数,求给定树的所有节点的balance的最小值。

    Analysis

    建图,转化成有根树,然后树形dp。
    (size[i])为以节点(i)为根的子树节点数,(f[i])为节点(i)的balance值。则状态转移方程为$$f[i] = max(N-size[i],max_{j in s[i]}size[j]) quad ext{where (s[i]) is the set of the son nodes of node i.}$$也就是说每一个节点的balance值是 其上方节点数之和 与 所有以子节点为根的子树节点数 中的最大值。在遍历过程中记录最小的balance值即可。

    技巧:通过BFS转化为有根树时产生的队列可以作为dp顺序使用。

    Code

    //////////////////////////////////////////////////////////////////////
    //Target: POJ 1655 - Balancing Act
    //@Author: Pisceskkk
    //Date: 2019-2-21
    //////////////////////////////////////////////////////////////////////
    
    #include<cstdio>
    #include<cstring>
    #define N (int)2e5+10
    #define INF 1e9
    #define Max(a,b) (a>b?a:b)
    using namespace std;
    
    int t,n,s[N],ans[N],min_i,min_b;
    int q[N],d[N];
    
    int he[N];
    struct edge{
        int to,ne;
    }e[N<<1];
    
    void add_2(int x,int y,int i){
        i<<=1;
        e[i].ne = he[x];
        he[x] = i;
        e[i].to = y;
        i++;
        e[i].ne = he[y];
        he[y] = i;
        e[i].to = x;
    }
    
    int bfs(int s){
        int h=0,t=0;
        memset(d,0,sizeof(d));
        memset(q,0,sizeof(q));
        q[h++]=s;
        d[s] = 1;
        while(t<h){
            int x=q[t++],y;
            for(int i=he[x];i;i=e[i].ne){
                y = e[i].to;
                if(!d[y]){
                    d[y] = 1;
                    q[h++]=y;
                }
            }
        }
        return h;
    }
    
    int main(){
        scanf("%d",&t);
        while(t--){
            memset(e,0,sizeof(e));
            memset(he,0,sizeof(he));
            memset(s,0,sizeof(s));
            memset(ans,0,sizeof(ans));
            min_i = min_b = INF;
            scanf("%d",&n);
            int x,y;
            for(int i=1;i<n;i++){
                scanf("%d %d",&x,&y);
                add_2(x,y,i);
            }
            bfs(1);
            for(int i=n-1;i>=0;i--){
                int x = q[i];
                s[x] = 1;
                for(int i=he[x];i;i=e[i].ne){
                    y = e[i].to;
                    s[x] += s[y];
                    ans[x] = Max(ans[x],s[y]);
                }
                ans[x] = Max(ans[x],n-s[x]);
                if(ans[x] < min_b){
                    min_b =  ans[min_i=x];
                }
                else if(ans[x] == min_b && x < min_i){
                    min_i = x;
                }
            }
            printf("%d %d
    ",min_i,min_b);
        }
    
    }
    
    我思故我在
  • 相关阅读:
    常用算法解析-动态规划
    转载-通过ApplicationContext 去获取所有的Bean
    什么是crud?
    new 关键字 和 newInstance() 方法的 区别
    Java反射简单使用--第一次细致阅读底层代码
    动态创建管理定时任务-已完成
    springboot mail整合freemark实现动态生成模板
    20190930开始记录每天学习状态,更新至20200125结束
    hibernate的对象/关系映射结果为空,exists查不到值的问题-20190823
    转载-Java中LinkedList的一些方法—addFirst addFirst getFirst geLast removeFirst removeLast
  • 原文地址:https://www.cnblogs.com/pisceskkk/p/10424031.html
Copyright © 2011-2022 走看看