多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > poj 1655 Balancing Act(樹的重心)

poj 1655 Balancing Act(樹的重心)

來源:程序員人生   發布時間:2015-06-24 08:25:13 閱讀次數:2616次

Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9913   Accepted: 4069

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⑴ 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

#include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<queue> #include<vector> using namespace std; #define ll long long #define N 21000 #define mem(a,t) memset(a,t,sizeof(a)) const int inf=1000005; int cnt,n; struct node { int v,next; }e[N*2]; int head[N]; int num[N]; int ans,index; void add(int u,int v) { e[cnt].v=v; e[cnt].next=head[u]; head[u]=cnt++; } void dfs(int u,int len,int fa) { int i,v,tmp=0; num[u]=1; for(i=head[u];i!=⑴;i=e[i].next) { v=e[i].v; if(v!=fa) { dfs(v,len+1,u); tmp=max(tmp,num[v]); num[u]+=num[v]; } } int t=max(tmp,n-num[u]); if(t<=ans) { if(u<index||t<ans) { ans=t; index=u; } } } int main() { //freopen("in.txt","r",stdin); int i,u,v,t; scanf("%d",&t); while(t--) { scanf("%d",&n); cnt=0; mem(head,⑴); for(i=1;i<n;i++) { scanf("%d%d",&u,&v); add(u,v); add(v,u); } mem(num,0); ans=n; dfs(1,0,⑴); printf("%d %d ",index,ans); } return 0; }






生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲精品国产字幕久久不卡 | 免费午夜视频在线观看 | 欲色网站 | 99久久精品国产麻豆 | 欧美午夜网| 精品日韩一区二区三区视频 | 一级毛片一级毛片a毛片欧美 | 黄色a网站 | 亚洲国产高清一区二区三区 | 欧美亚洲国产片在线观看 | 最近的最新的中文字幕视频 | 香蕉在线精品亚洲第一区 | 欧美国产三级 | a毛片全部播放免费视频完整18 | 91成人国产福利 | 中文字幕第二十页 | 精品久久亚洲 | 天堂在线v| 美国一级黄色毛片 | 18一20岁一级毛片 | 色综合网站在线 | 日韩 欧美 国产 亚洲 中文 | 一级白嫩美女毛片免费 | 国产精品福利在线观看秒播 | 国产尤物在线观看 | 特一级黄色片 | 国产精品人成 | 久久乐精品 | 波多野结衣中出在线 | 中文乱码精品一区二区三区 | 国产三级在线观看播放 | 久久久精品456亚洲影院 | 国产丝袜一区二区三区在线观看 | 亚洲人成综合网站在线 | 国产v精品成人免费视频71sao | 日本不卡一区二区三区四区 | 免费观看影视传媒公司 | 香蕉tv亚洲专区在线观看 | 亚洲性网 | 亚洲播放 | 欧美日韩在线一区二区三区 |