poj-1655 Balancing Act (center of gravity + tree DP)

Source: Internet
Author: User

Topic Links:

Balancing ACT

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11845 Accepted: 4993

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

Test instructions

Which node is the center of gravity of the given tree, and the maximum value of the number of connected block nodes is removed from the node;

Ideas:

DFS, find out the number of child tree nodes of all nodes, and then find out the maximum number of nodes after removing the node to update the answer.

AC Code:

//#include <bits/stdc++.h>#include <vector>#include<iostream>#include<queue>#include<cmath>#include<map>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;#defineRiep (n) for (int i=1;i<=n;i++)#defineRIOP (n) for (int i=0;i<n;i++)#defineRJEP (n) for (int j=1;j<=n;j++)#defineRJOP (n) for (int j=0;j<n;j++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedefLong Longll;template<classT>voidRead (t&num) {    CharCH;BOOLf=false;  for(Ch=getchar (); ch<'0'|| Ch>'9'; f= ch=='-', ch=GetChar ());  for(num=0; ch>='0'&&ch<='9'; num=num*Ten+ch-'0', ch=GetChar ()); F&& (num=-num);}intstk[ -], Tp;template<classT> Inlinevoidprint (T p) {if(!p) {Puts ("0");return; }  while(p) stk[++ TP] = p%Ten, p/=Ten;  while(TP) Putchar (stk[tp--] +'0'); Putchar ('\ n');}ConstLL mod=1e9+7;Const DoublePi=acos (-1.0);ConstLL inf=1e18;Const intn=2e5+Ten;Const intmaxn=1005;intN,son[n],ans,num;vector<int>Ve[n];voidDfsintXintFA) {    intLen=ve[x].size (), mmax=0; SON[X]=1;  for(intI=0; i<len;i++)    {        inty=Ve[x][i]; if(Y==FA)Continue;        DFS (Y,X); SON[X]+=Son[y]; if(son[y]>Mmax) Mmax=Son[y]; }    intD=max (mmax,n-son[x]); if(d<=num) {        if(d==num) {            if(X<ans) ans=x; }        Elseans=x; Num=D; }}intMain () {intT;    Read (t);  while(t--) {read (n);  for(intI=0; i<=n;i++) ve[i].clear (); intx, y;  for(intI=1; i<n;i++{read (x); Read (y);            Ve[x].push_back (y);        Ve[y].push_back (x); } ans=1; Num=50000000; DFS (1,-1); cout<<ans<<" "<<num<<"\ n"; }        return 0;}

poj-1655 Balancing Act (center of gravity + 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.