poj-1655-Balancing Act 树形dp/树的重心
来源:互联网 发布:上海美猴网络面试 编辑:程序博客网 时间:2024/06/06 07:28
Balancing Act
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10637 Accepted: 4418
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.
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
Source
POJ Monthly--2004.05.15 IOI 2003 sample task
树形dp经典模型。
任选一个结点作为根, 把无根树变成有根树, 设d[i]表示已i为根的子树的结点个数。d[i]=sum{d[j]}+1。删除i后的最大子树即为max(max{d[j]}, n-d[i]);
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<vector>#include<algorithm>using namespace std;const int maxn=1e6+7;#define ll long long#define rep(a, b) for(int i=a;i<b;i++)#define INF 1e9+7int T;int n;int d[maxn];int rt;int ans;int p;int vis[maxn];vector<int> e[maxn];void init(){ memset(d, 0, sizeof(d)); memset(e, 0, sizeof(e)); memset(vis, 0, sizeof(vis)); rt=0; ans=INF; p=INF;}void dfs(int cur){ d[cur]=vis[cur]=1; int len=e[cur].size(); int t=0; rep(0, len){ if(vis[e[cur][i]] == 0) { dfs(e[cur][i]); d[cur] += d[e[cur][i]]; t=max(t, d[e[cur][i]]); } } t=max(t, n-d[cur]); if(t !=0 && t<ans) { ans=t; p=cur; } else if(t == ans && cur<p) p=cur; }int main(){ scanf("%d", &T); while(T--){ int a=0, b=0; scanf("%d", &n); init(); rep(0, n-1){ scanf("%d%d", &a, &b); e[a].push_back(b); e[b].push_back(a); } rt=a; dfs(rt); printf("%d %d\n", p, ans); }}
0 0
- POJ 1655 Balancing Act(求树的重心--树形DP)
- POJ 1655 Balancing Act (树形dp 树的重心)
- poj-1655-Balancing Act 树形dp/树的重心
- poj 1655 Balancing Act 【树形DP 求树的重心】
- poj 1655 Balancing Act(树的重心,树形dp)
- POJ.1655 Balancing Act (树形DP 树的重心)
- POJ 1655 Balancing Act 树型DP 树的重心 板题
- poj 1655Balancing Act(找重心,树形dp)
- 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 (树的重心)
- QueryPerformanceFrequency()
- 电梯调度算法问题
- Springmvc 默认首页的问题
- android studio 运行代码时device不能识别genymotion设备
- linux grep 详解
- poj-1655-Balancing Act 树形dp/树的重心
- java 用 _id 查找 MongoDB 下的数据
- 在caffe上跑自己的数据
- 怎么在CSDN博客文章中添加公式
- ProgressDialog使用总结
- 每天一个Linux命令-9(Find–atime –ctime –mtime)
- POJ 3258 River Hopscotch(二分)
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem
- java 解析mp3信息-jid3lib