POJ 1655 balancing Act tree DP Find the center of gravity of __ tree diameter, center of gravity, partition

Source: Internet
Author: User
Balancing ACT
Time Limit: 1000MS Memory Limit: 65536K
Total submissions: 10726 accepted: 4463

Description Consider a tree T with N (1 <= n <= 20,000) nodes numbered 1...N. deleting LDS a forest:a collection of one or more trees. Define the balance of a node to is the size of the largest tree in the forest T created by deleting so node from t.  ;
For example, consider of tree: 

Deleting node 4 yields two trees whose member nodes are {5} and {1, 2, 3,6,7}. The larger of 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 this trees has two nodes, so the balance of node 1 are two. 

for each input tree, calculate the nod E that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number. 

Input the "a" of input contains a single integer t (1 <= t <=), the number of test cases. The "a" 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 the "the" tree. No Edge would be listed twice, and all edges would be listed.

Output for each test case, print a line containing two integers, the number of the node with minimum balance and the Balan Ce of that node.

Sample Input

1
7
2 6 1 2 1 4 4 5 3 7 3-
1

Sample Output

1 2


The center of gravity of the tree: for any node in a tree I, we find all of its subtree in the most nodes of the subtree, if the number of nodes in this Shang tree as Dp[i], then the tree's center of gravity is all nodes in the lowest DP value node.


To give you a tree n dots and N-1, so that you can find the center of gravity number and the DP value of the center of gravity, if the same DP value, select a small number of output.



Train of thought: with node 1 as the root node of the tree, then traverse all nodes and use the tree DP to find all the DP values. Finally traverse all nodes to find the center of gravity can.


To find a DP value (1 as the root node)


One: The number of subtree nodes below can be obtained in the DFS process;


Two: Top subtree nodes = Total nodes N-(total number of subtree nodes below + 1).


AC Code:

#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 20000+100 #define MAXM 40000+100
0 using namespace Std;
struct Edge {int from, to, next;};
Edge EDGE[MAXM];
int HEAD[MAXN], edgenum;
int dp[maxn];//stores the largest number of nodes in all subtrees of which the node is the root node int num[maxn];//The number of nodes in the tree below which the node is the root node int N;
    void Init () {edgenum = 0;
Memset (Head,-1, sizeof (head));
    } void Addedge (int u, int v) {Edge E = {u, V, Head[u]};
    Edge[edgenum] = E;
Head[u] = edgenum++;
    } void Getmap () {int A, B;
        for (int i = 1; i < N; i++) {scanf ("%d%d", &a, &b);
        Addedge (A, b);
    Addedge (b, a);
    } void Dfs (int u, int fa) {Num[u] = 1, dp[u] = 0;
        for (int i = head[u]; I!=-1; i = edge[i].next) {int v = edge[i].to;
        if (v = = FA) continue;//can't come back for its parent node Dfs (v, u);//Find the node number of the subtree with the root node of v dp[u] = max (Dp[u], num[v);/update subtree below Num[u] + = num[v];//is added to the current node to find the number of subtree nodes above} Dp[u] = max (dp[u), N-num[u]);The above subtree} void solve () {DFS (1,-1) is updated;
    int ans = N;
    int node = 1;
    for (int i = 1; I <= N; i++) {if (ans > dp[i]) ans = dp[i], node = i;
printf ("%d%d\n", node, ans);
    int main () {int t;
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        Init ();
        Getmap ();
    Solve ();
return 0;

 }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.