编程介的小学生 2019-10-03 20:15 采纳率: 20.5%
浏览 69

Balancing Act 正确的解决

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

1
7
2 6
1 2
1 4
4 5
3 7
3 1
Sample Output

1 2

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 NAO机器人的录音程序保存问题
    • ¥15 C#读写EXCEL文件,不同编译
    • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
    • ¥15 扩散模型sd.webui使用时报错“Nonetype”
    • ¥15 stm32流水灯+呼吸灯+外部中断按键
    • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
    • ¥15 NX MCD仿真与博途通讯不了啥情况
    • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
    • ¥15 gradio的web端页面格式不对的问题
    • ¥15 求大家看看Nonce如何配置