Balancing Act(poj1655)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12703 | Accepted: 5403 |
Description
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
Output
Sample Input
1
7
2 6
1 2
1 4
4 5
3 7
3 1
Sample Output
1 2
题意:给你n个点,n-1条边形成一颗棵树,然后让你找树的重心;
思路:树形dp;
先dfs求出每个点所形成的子树的权值,然后再dfs求权值中的最大值更新dp。因为当前点的各个子树的权值都知道,那么只要求出当前节点父亲节点的权值,nod-sum[n];
复杂度O(n);
1 #include<stdio.h>
2 #include<math.h>
3 #include<queue>
4 #include<algorithm>
5 #include<string.h>
6 #include<iostream>
7 #include<stack>
8 #include<vector>
9 using namespace std;
10 typedef long long LL;
11 vector<int>vec[20005];
12 int dp[20005];
13 bool flag[20005];
14 int sum[20005];
15 void dfs(int n);
16 void dfs2(int n);
17 int nod;
18 int main(void)
19 {
20 int t;
21 scanf("%d",&t);
22 while(t--)
23 {
24 int n;
25 scanf("%d",&nod);
26 n = nod;
27 for(int i = 0;i < 20005;i++)
28 vec[i].clear();
29 for(int i = 0; i < n-1; i++)
30 {
31 int a,b;
32 scanf("%d %d",&a,&b);
33 vec[a].push_back(b);
34 vec[b].push_back(a);
35 }
36 memset(flag,0,sizeof(flag));
37 memset(dp,0,sizeof(dp));
38 memset(sum,0,sizeof(sum));
39 dfs(1);
40 memset(flag,0,sizeof(flag));
41 dfs2(1);
42 int id = 0;
43 int maxx = 1e9;
44 for(int i = 1; i <= n; i++)
45 {
46 if(maxx > dp[i])
47 maxx = dp[i],id = i;
48 }
49 printf("%d %d\n",id,maxx);
50 }
51 return 0;
52 }
53 void dfs(int n)
54 {
55 int i,j;
56 flag[n] = true;
57 for(i = 0; i < vec[n].size(); i++)
58 {
59 int id = vec[n][i];
60 if(!flag[id])
61 {
62 dfs(id);
63 sum[n]+=sum[id];
64 }
65 }
66 sum[n]++;
67 }
68 void dfs2(int n)
69 {
70 flag[n] = true;
71 int i,j;
72 for(i = 0; i < vec[n].size(); i++)
73 {
74 int id = vec[n][i];
75 if(!flag[id])
76 {
77 dp[n] = max(dp[n],sum[id]);
78 dfs2(id);
79 }
80 }
81 dp[n] = max(dp[n],nod-sum[n]);
82 }
Balancing Act(poj1655)的更多相关文章
- 『Balancing Act 树的重心』
树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可 ...
- poj1655 Balancing Act 找树的重心
http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ1655 Balancing Act(树的重心)
题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP ...
- poj-1655 Balancing Act(树的重心+树形dp)
题目链接: Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11845 Accepted: 4 ...
- poj1655 Balancing Act (dp? dfs?)
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14247 Accepted: 6026 De ...
- POJ 1655 Balancing Act 树的重心
Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...
- POJ 1655 Balancing Act【树的重心】
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14251 Accepted: 6027 De ...
- POJ 1655.Balancing Act 树形dp 树的重心
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14550 Accepted: 6173 De ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
随机推荐
- Oracle-SQL语句的语法顺序和执行顺序
SQL语句的语法顺序和执行顺序了,我们常见的SQL语法顺序如下: SELECT DISTINCT <Top Num> <select list>FROM [left_table ...
- SCRDet——对小物体和旋转物体更具鲁棒性的模型
引言 明确提出了三个航拍图像领域内面对的挑战: 小物体:航拍图像经常包含很多复杂场景下的小物体. 密集:如交通工具和轮船类,在航拍图像中会很密集.这个DOTA数据集的发明者也提到在交通工具和轮船类的检 ...
- [源码解析] PyTorch 分布式 Autograd (6) ---- 引擎(下)
[源码解析] PyTtorch 分布式 Autograd (6) ---- 引擎(下) 目录 [源码解析] PyTtorch 分布式 Autograd (6) ---- 引擎(下) 0x00 摘要 0 ...
- 从源码看RequestMappingHandlerMapping的注册与发现
1.问题的产生 日常开发中,大多数的API层中@Controller注解和@RequestMapping注解都会被使用在其中,但是为什么标注了@Controller和@RequestMapping注解 ...
- Java 性能优化的 50 个细节
在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著地提升程序性能. #尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间, ...
- 常见排序——Java实现
1 package struct; 2 3 /** 4 * 5 * @作者:dyy 6 * @公司:陕西科技大学 7 * @修改日期: 8 * @邮箱:1101632375@qq.com 9 * @描 ...
- rust常用技巧
允许未使用的方法,写在文件开头,可过滤过掉该项提示 #![allow(unused)]
- MBean代码例子
public class ServerImpl { public final long startTime; public ServerImpl() { startTime = System.curr ...
- 基于阿里云 ecs 使用 docker 方式部署 showDoc
官网文档:https://www.showdoc.cc/help?page_id=65610 (建议先看下这个) 首先说明一下,我 ecs 镜像是 CentOS 7.6 64位 1. 首先在 服务器上 ...
- maven管理本地jar包
maven作为包管理工具,好处不必多说.但是有些情况,比如需要引入第三方包,如快递鸟,支付宝,微信等jar包(当然有可能直接提供maven依赖),如果直接下载到本地之后,怎么整合到自己的maven工程 ...