博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ - 1655 Balancing Act (树的重心)
阅读量:4970 次
发布时间:2019-06-12

本文共 1860 字,大约阅读时间需要 6 分钟。

题目链接:

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

题意:就是去掉树上的一个节点,看看剩下的子树中节点总数最大的是多少,

然后在这些最大值中求一个最小值,如果有多个点都是最小值,那么找一个序号最小的节点,
输出节点号,和最小值。

思路:定义dp[i]是以节点i为根的树的节点总数,则去掉节点i后的树的节点总数为(n-dp[i]),从小到大枚举节点i,最后取max(dp[i],n-dp[i])即可。

AC代码:

#include
#include
#include
#include
using namespace std;const int maxn=20000+10;vector
p[maxn];int dp[maxn],n,min_node,max_sum;void DFS(int u,int fa){ int sum=0; dp[u]=1; for(int i=0;i

  

 

转载于:https://www.cnblogs.com/djh0709/p/9606848.html

你可能感兴趣的文章
[Voice communications] 声音的滤波
查看>>
BZOJ.3139.[HNOI2013]比赛(搜索 Hash)
查看>>
json在线解析
查看>>
存储设备形成的层次结构
查看>>
源码阅读 - java.util.concurrent (三)ConcurrentHashMap
查看>>
Daily Scrum 10.30
查看>>
SQL语言之概述(一)
查看>>
数据库表 copy
查看>>
LinkedList源码解析
查看>>
SignalR循序渐进(一)简单的聊天程序
查看>>
MyServer
查看>>
Learning Cocos2d-x for XNA(2)——深入剖析Hello World
查看>>
软件建模——第9章 毕业论文管理系统—面向对象方法
查看>>
Http协议
查看>>
手机端web开发必备代码
查看>>
[SDOI2008]洞穴勘测
查看>>
NOI2014 购票
查看>>
Difference between Linearizability and Serializability
查看>>
电影《绿皮书》
查看>>
IDEA使用操作文档
查看>>