zoukankan      html  css  js  c++  java
  • I

    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
    树形DP,注意考虑n-sum[u]这个搜索方向的联通点集
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<sstream>
    #include<algorithm>
    #include<queue>
    #include<deque>
    #include<iomanip>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<stack>
    #include<set>
    #include<fstream>
    #include<memory>
    #include<list>
    #include<string>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    #define MAXN 200099
    #define L 31
    #define INF 1000000009
    #define eps 0.00000001
    #define sf(a) scanf("%d",&a)
    /*
    dp[i] 记录i点除去偶的最大点集点数
    sum[i] 记录所有子节点数目
    */
    struct edge
    {
        int to, next;
    }E[MAXN];
    int sum[MAXN], dp[MAXN], head[MAXN];
    int t, n, cnt;
    void addedge(int f,int t)
    {
        E[cnt].to = t;
        E[cnt].next = head[f];
        head[f] = cnt++;
    }
    void init()
    {
        memset(sum, 0, sizeof(sum));
        memset(dp, 0, sizeof(dp));
        memset(head, -1, sizeof(head));
        cnt = 0;
    }
    void dfs(int u, int pre)
    {
        sum[u] = dp[u] = 1;
        for (int i = head[u]; i != -1; i = E[i].next)
        {
            int v = E[i].to;
            if (v == pre) continue;
            dfs(v, u);
            sum[u] += sum[v];
            dp[u] = max(dp[u], sum[v]);
        }
        dp[u] = max(dp[u], n - sum[u]);
    }
    int main()
    {
        sf(t);
        while (t--)
        {
            init();
            sf(n);
            for (int i = 0; i < n - 1; i++)
            {
                int a, b;
                sf(a), sf(b);
                addedge(a, b);
                addedge(b, a);
            }
            dfs(1, -1);
            int ans = INF, k = -1;
            for (int i = 1; i <= n; i++)
            {
                if (dp[i] < ans)
                {
                    k = i, ans = dp[i];
                }
            }
            printf("%d %d
    ", k, ans);
        }
    }
  • 相关阅读:
    Java之内部类
    java之对象的前世今生
    java之继承
    java之接口
    何为大学 大学何为?
    丁香般的姑娘
    MySQL创建数据库与创建用户以及授权
    CentOS6.8手动安装MySQL5.6
    linux 修改myql 编码配置等信息参考
    Centos6.8 Mysql5.6 安装配置教程
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7464819.html
Copyright © 2011-2022 走看看