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 ...
随机推荐
- 学习java的第二十五天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- 学习java 7.19
学习内容: 接口的组成中加入了默认方法,静态方法,私有方法 接口中默认方法:public default 返回值类型 方法名(参数列表){ } public default void show() ...
- JavaScript的数据结构快速学-链表的实现
1-单项链表 function LinkedList() { let Node = function(element) { // 辅助类,表示要添加到链表中的项 this.element = elem ...
- 【STM32】WS2812介绍、使用SPI+DMA发送数据
这篇要使用到SPI+DMA,需要了解的话,可以参考我另两篇博客 时钟:https://www.cnblogs.com/PureHeart/p/11330967.html SPI+DMA通信:https ...
- 转 Android应用开发必备的20条技能
https://blog.csdn.net/u012269126/article/details/52433237 有些andorid开发人员感觉很迷茫,接下来该去看系统源码还是继续做应用,但是感觉每 ...
- [学习总结]3、Android---Scroller类(左右滑动效果常用的类)
参考资料:http://blog.csdn.net/vipzjyno1/article/details/24592591 非常感谢这个兄弟! 在android学习中,动作交互是软件中重要的一部分,其中 ...
- 什么是javaScript闭包
闭包是与函数有着紧密的关系,它是函数的代码在运行过程中的一个动态环境,是一个运行期的概念. 所谓闭包,是指词法表示包括不必计算的变量的函数.也就是说,该函数能够使用函数外定义的变量. 在程序语言中,所 ...
- 【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式
bean的生命周期:创建---初始化---销毁. Spring中声明的Bean的初始化和销毁方法有3种方式: @Bean的注解的initMethod.DestroyMethod属性 bean实现Ini ...
- Linux上Zookeeper集群搭建
一.官网 https://zookeeper.apache.org/ 二.下载安装 (1)下载 复制链接地址 http://mirror.bit.edu.cn/apache/zookeeper/zo ...
- 解决PLSQL查不到带中文条件的记录
原因: PLSQL乱码问题皆是ORACLE服务端字符集编码与PLSQL端字符集编码不一致引起.类似乱码问题都可以从编码是否一致上面去考虑. 解决: 1. 查询Oracle服务端字符集编码,获取NLS_ ...