poj1655 balancing Act (tree DP)

Source: Internet
Author: User

Balancing ACT
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11176 Accepted: 4702

Description

Consider a tree T with N (1 <= n <= 20,000) nodes numbered 1...N. Deleting all node from the tree yields a forest:a Collection of one or more trees. Define the balance of a node to being 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-trees whose member nodes is {5} and {1,2,3,6,7}. The larger of these and trees have 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 the these trees has a nodes, so the balance of node 1 is a.

For each input tree, calculate the node, the minimum balance. If multiple nodes has equal balance, output the one with the lowest number.

Input

The first line of input contains a single integer t (1 <= t <=), the number of test cases. The first line of all test case contains an integer n (1 <= n <= 20,000), the number of congruence. The next N-1 lines each contains and space-separated node numbers that is the endpoints of an edge in the tree. No Edge would be listed twice, and all edges would be listed.

Output

For each test case, print a line containing-integers, the number of the node with minimum balance and the balance of T Hat node.

Sample Input

172 61 21 44 53 73 1

Sample Output

1 2

Source

POJ monthly--2004.05.15 IOI 2003 Sample Task
Test instructions: Given a tree of n nodes, the fewest nodes in any subtree remaining after a node is deleted. (expression is not very good, in fact, and poj3107 the same) analysis: and poj3107 the same, just change the input and output can be, poj3107
#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <cmath> #include <algorithm> Using namespace Std;const double eps = 1e-6;const double pi = ACOs ( -1.0); const int INF = 0x3f3f3f3f;const int MOD = 100000    0007; #define LL Long Long#define CL (A, B) memset (A,b,sizeof (a)) #define MAXN 40010struct node{int v; Node *next;}    TREE[MAXN], *head[maxn];int n,pre,dp[maxn];int ans,num,tot;void init () {ans = INF;    Pre = 1;    CL (DP, 0); CL (head, NULL);}    void Add (int x, int y) {tree[pre].v = y; Tree[pre].next = Head[x];    HEAD[X] = &tree[pre++];    TREE[PRE].V = x; Tree[pre].next = Head[y]; Head[y] = &tree[pre++];}    void Dfs (int son, int father) {node *p = Head[son];    Dp[son] = 1;            while (P! = NULL) {if (p->v! = father) {dfs (p->v, son);        Dp[son] + = dp[p->v]; } p = P-> next;    }}void dp_tree (int son, int father) {int maxx = 0;    Node *p = Head[son];            while (P! = NULL) {if (p->v! = father) {Dp_tree (p->v, son);        Maxx = Max (Maxx, Dp[p->v]);    } p = p->next;    } Maxx = Max (Maxx, N-dp[son]);        if (Maxx < ans)//contrast poj3107, also change here, only the smallest save down on the line {ans = Maxx;    num = son;    } else if (Maxx = = ans) {if (num > son) num = son;    }}int Main () {int t,a,b;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Init ();            for (int i=1; i<n; i++) {scanf ("%d%d", &a,&b);        Add (A, b);        } DFS (1, 0);        Dp_tree (1, 0);    printf ("%d%d\n", num, ans); } return 0;}


poj1655 balancing Act (tree DP)

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.