POJ 1655 Balancing Act
来源:互联网 发布:ubuntu 笔记本 编辑:程序博客网 时间:2024/05/18 01:59
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.
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.
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.
For each test case, print a line containing two integers, the number of the node with minimum balance and the balance of that node.
172 61 21 44 53 73 1
1 2
树形DP
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<vector>#include<stdio.h>#include<cmath>using namespace std;vector<int> a[40005];int N, T;int sizex[40005], sizey[40005], ans;void dp(int m, int n){ sizex[m] = 1; for(int i = 0; i < a[m].size(); i++) { int y = a[m][i]; if(y == n) continue; dp(y, m); sizex[m] += sizex[y]; sizey[m] = max(sizey[m], sizex[y]); } sizey[m] = max(sizey[m], N - sizex[m]); if(sizey[m] < sizey[ans]) ans = m; if(sizey[m] == sizey[ans]) ans = min(ans, m);}int main(){ scanf("%d", &T); while(T--) { scanf("%d", &N); ans = 0; sizey[0] = 0x7fffffff; for(int i = 1; i < N; i++) { int x, y; scanf("%d %d", &x, &y); a[y].push_back(x); a[x].push_back(y); } dp(1, 0); cout<<ans<<" "<<sizey[ans]<<endl; for(int i = 1; i <= N; i++) a[i].clear(); memset(sizex, 0, sizeof(sizex)); memset(sizey, 0, sizeof(sizey)); } return 0;}
阅读全文
0 0
- poj 1655 Balancing Act
- poj 1655 Balancing Act
- poj 1655 Balancing Act
- POJ 1655 Balancing Act
- poj 1655 Balancing Act
- poj 1655 Balancing Act
- POJ 1655 Balancing Act
- poj 1655 Balancing Act
- POJ 1655:Balancing Act
- Poj 1655 Balancing Act
- POJ 1655 Balancing Act
- POJ 1655 Balancing Act
- POJ 1655 Balancing Act
- POJ 1655 Balancing Act
- POJ 1655 Balancing Act
- POJ 1655 Balancing Act
- POJ-1655-Balancing Act
- [POJ] 1655 Balancing Act
- test命令的文件比较功能
- POJ 3104 Drying
- spring定时任务的配置及注释
- 5并行数据加载
- 二级指针输入模型(三种内存模型)
- POJ 1655 Balancing Act
- eclipse中使用git
- Java操作Excel基本
- 交换排序(2)--快速排序3种实现方案及其优化
- C++引用/枚举/typedef/头文件/预编译器/自定义头文件
- stl-set
- Scala Extractor Objects
- [数论] LOJ #510. 「LibreOJ NOI Round #1」北校门外的回忆
- 计算机图形学 学习笔记(五):多边形裁剪(Suther land-Hodgeman),文字裁剪