Poj1655 balancing act tree DP

Source: Internet
Author: User
Balancing Act
Time limit:1000 ms   Memory limit:65536 K
Total submissions:7594   Accepted:3082

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

172 61 21 44 53 73 1

Sample output

1 2
It is almost the same as the previous question!
#include <iostream>#include <stdio.h>#include <string.h>#include <vector>using namespace std;#define MAXN 20005struct node {    int up,down;    vector<int > q;}queue[MAXN];int visit[MAXN],n;int fmax(int a,int b){    if(a>b)    return a;    return b;}void dfs(int x){    int temp,i;    visit[x]=1;    queue[x].down=1;    queue[x].up=0;    for(i=0;i<queue[x].q.size();i++)    {        temp=queue[x].q[i];        if(!visit[temp])        {            dfs(temp);            queue[x].down+=queue[temp].down;            queue[x].up=fmax(queue[x].up,queue[temp].down);        }    }    queue[x].up=fmax(queue[x].up,n-queue[x].down);}int main(){    int tcase,i,temp,a,b,tempval;    scanf("%d",&tcase);    while(tcase--)    {        scanf("%d",&n);        memset(visit,0,sizeof(visit));        for(i=0;i<=n;i++)        {            queue[i].q.clear();        }        for(i=1;i<n;i++)        {            scanf("%d%d",&a,&b);            queue[a].q.push_back(b);            queue[b].q.push_back(a);        }        dfs(1);        tempval=0x4f4f4f4f;        for(i=1;i<=n;i++)        {            if(queue[i].up<tempval)            {                temp=i;                tempval=queue[i].up;            }        }        printf("%d %d\n",temp,tempval);    }    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.