Balancing Act (树形dp+树的重心)
来源:互联网 发布:淘宝客佣金结算时间 编辑:程序博客网 时间:2024/06/05 18:51
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.
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
题目大概:
给出一棵树,找出它的重心是哪个节点,它的最小额度是多少(即最大子树的结点数)。
思路:
首先要知道重心定义,
树的重心是,与重心相连的所有子树中,结点数最多的子树 是所有节点的最多子树中最少的。
然后就dfs遍求出最小的额度。然后从1开始判断就可以了。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int n;int dp[20005];int d[20005];int head[20005];int minshu,no;int ans;struct shu{ int v; int next;}tr[40010];void add(int q,int w){ tr[ans].v=w; tr[ans].next=head[q]; head[q]=ans++;}void dfs(int x,int pa){ d[x]=1; int maxshu=-20005; for(int i=head[x];i!=-1;i=tr[i].next) { int son=tr[i].v; if(son!=pa) { dfs(son,x); d[x]+=d[son]; maxshu=max(maxshu,d[son]); } } dp[x]=max(maxshu,n-d[x]); if(minshu>dp[x]) { minshu=dp[x]; }}int main(){ int t; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); memset(d,0,sizeof(d)); memset(head,-1,sizeof(head)); ans=0; scanf("%d",&n); for(int i=1;i<n;i++) { int q,w; scanf("%d%d",&q,&w); add(q,w); add(w,q); } minshu=20005; no=0; dfs(1,0); for(int i=n;i>=1;i--) { if(minshu==dp[i]) { no=i; } } printf("%d %d\n",no,minshu); } return 0;}
阅读全文
0 0
- Balancing Act (树形dp+树的重心)
- POJ 1655 Balancing Act(求树的重心--树形DP)
- POJ 1655 Balancing Act (树形dp 树的重心)
- poj-1655-Balancing Act 树形dp/树的重心
- poj 1655 Balancing Act 【树形DP 求树的重心】
- poj1655 Balancing Act【树的重心+树形dp】
- poj 1655 Balancing Act(树的重心,树形dp)
- POJ1655-树的重心&树形dp-Balancing Act
- POJ.1655 Balancing Act (树形DP 树的重心)
- Balancing Act (树形dp 求树的重心板题)
- [POJ1655]Balancing Act(树的重心)
- poj1655Balancing 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 (树的重心)
- 网络基本概念之网络分层
- LintCode372: 在O(1)时间复杂度删除链表节点
- Linux下g++基本的编译
- Laravel安装第三方扩展包 mews/captcha
- 第十一周作业:GUI简单界面--浏览器
- Balancing Act (树形dp+树的重心)
- tf.argmax(input, axis=None, name=None, dimension=None)
- Eclipse如何连接数据库---JDBC
- 广搜法做暴力的奥数题
- CodeForces
- 给定N中物品和一个背包。物品i的重量是Wi,其价值位Vi ,背包的容量为C。问应该如何选择装入背包的物品,使得转入背包的物品的总价值为最大?对每种物品i只有两种选择,即装入背包和不装入背包
- 非模态对话框和模态对话框_创建
- Vue.js源码解析(九)【从template到DOM(Vue.js源码角度看内部运行机制)】
- LeetCode.682 Baseball Game