POJ1655 Balancing Art
2024-09-06 13:03:01
Balancing Act
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13865 | Accepted: 5880 |
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 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.
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.
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
Source
【题解】
此题用所谓“DP”?求重心即可,更新时时刻注意更新最小节点
一直不理解为啥求重心也叫DP啊!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib> inline void read(int &x)
{
x = ;char ch = getchar();char c = ch;
while(ch > '' || ch < '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
}
inline int max(int a, int b){return a > b ? a : b;}
inline int min(int a, int b){return a < b ? a : b;} const int INF = 0x3f3f3f3f;
const int MAXN = + ; struct Edge
{
int u,v,next;
}edge[MAXN << ];
int t,n,head[MAXN],cnt,b[MAXN],dp[MAXN],ma,g;
inline void insert(int a, int b){edge[++cnt] = Edge{a,b,head[a]};head[a] = cnt;} void dfs(int u)
{
int pos, tmp = -;
dp[u] = ;
for(pos = head[u];pos;pos = edge[pos].next)
{
int v = edge[pos].v;
if(!b[v])
{
b[v] = true;
dfs(v);
dp[u] += dp[v];
tmp = max(tmp, dp[v]);
}
}
tmp = max(tmp, n - dp[u]);
if(tmp < ma)
ma = tmp,g = u;
if(tmp == ma)
g = min(g, u);
} int main()
{
read(t);
register int i,tmp1,tmp2;
for(;t;--t)
{
ma = INF,g = INF;
memset(edge, , sizeof(edge));
memset(head, , sizeof(head));
cnt = ;memset(dp, , sizeof(dp));
memset(b, , sizeof(b));
read(n);
for(i = ;i < n;++ i)
{
read(tmp1);read(tmp2);
insert(tmp1, tmp2);insert(tmp2, tmp1);
}
b[] = true;
dfs();
printf("%d %d\n", g, ma);
}
return ;
}
最新文章
- SQL Server-聚焦使用视图若干限制/建议、视图查询性能问题,你懵逼了?(二十五)
- 基数树与RCU锁
- [outlook]打开以后就自动进入安全模式的解决方法。Outlook start in safe mode.
- [转发]dsdt解决睡眠唤醒死机
- Hibernate详解(5)——Hibernate核心接口和工作原理
- cobol语言基础培训教程
- JavaScript window.setTimeout() 的详细用法
- day03 文件操作
- semantic-ui 输入框
- 将 Windows 虚拟机从非托管磁盘转换为托管磁盘
- 373. Find K Pairs with Smallest Sums (java,优先队列)
- Android 程序打包及签名(转)
- CSS3组件化之菊花loading
- 学习 C++,关键是要理解概念,而不应过于深究语言的技术细节
- html5 data属性的使用
- spring 包的依赖关系
- 2017-2018-1 20179202《Linux内核原理与分析》第六周作业
- Jenkins Slave Nodes – using the Swarm Plugin
- flash读写学习笔记与spi接口及简单测试验证(三)
- Java for LeetCode 086
热门文章
- for update行级锁的作用
- 新增的Java MapReduce API
- Luogu P3496 [POI2010]GIL-Guilds(贪心+搜索)
- odoo many2one
- Vue简单评星效果与单张图片上传
- 深入浅出 Java Concurrency (18): 并发容器 part 3 ConcurrentMap (3)[转]
- Django REST Framework概述
- finger 工具:用来查询用户信息,侧重用户家目录、登录SHELL等
- Leetcode504.Base 7七进制数
- TCP/TP:DNS区域(Zone)