『Balancing Act 树的重心』
<更新提示>
<第一次更新>
<正文>
树的重心
我们先来认识一下树的重心。
树的重心也叫树的质心。找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡。
根据树的重心的定义,我们可以通过树形DP来求解树的重心。
设\(Max_i\)代表删去i节点后树中剩下子树中节点最多的一个子树的节点数。由于删去节点i至少将原树分为两部分,所以满足\(\ \frac{1}{2}n \leq Max_i\),我们要求的就是一个\(i\),使得\(Max_i\)最小。
对于Max数组,我们可以列出如下状态转移方程:
\]
size数组即为节点个数(树的大小),可以在树形DP中顺带求解。
\(Code:\)
inline void dp(int r,int f)
{
size[r]=1;
for(int i=0;i<Link[r].size();i++)
{
int Son=Link[r][i];
if(Son==f)continue;
dp(Son,r);
size[r]+=size[Son];
Max[r]=max(Max[r],size[Son]);
}
Max[r]=max(Max[r],n-size[r]);
if(Max[r]==Max[ans]&&r<ans)ans=r;
if(Max[r]<Max[ans])ans=r;
}
还是通过一道例题来认识一下。
Balancing Act(POJ1655)
Description
The city consists of intersections and streets that connect them.
Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that have to be cleaned of snow. These streets are chosen such that the number of streets is as small as possible but still every two intersections to be connected i.e. between every two intersections there will be exactly one path. The winter service consists of two snow plovers and two drivers, Mirko and Slavko, and their starting position is on one of the intersections.
The snow plover burns one liter of fuel per meter (even if it is driving through a street that has already been cleared of snow) and it has to clean all streets from the list in such order so the total fuel spent is minimal. When all the streets are cleared of snow, the snow plovers are parked on the last intersection they visited. Mirko and Slavko don’t have to finish their plowing on the same intersection.
Write a program that calculates the total amount of fuel that the snow plovers will spend.
Input Format
The first line of the input contains two integers: N and S, 1 <= N <= 100000, 1 <= S <= N. N is the total number of intersections; S is ordinal number of the snow plovers starting intersection. Intersections are marked with numbers 1...N.
Each of the next N-1 lines contains three integers: A, B and C, meaning that intersections A and B are directly connected by a street and that street's length is C meters, 1 <= C <= 1000.
Output Format
Write to the output the minimal amount of fuel needed to clean all streets.
Sample Input
5 2
1 2 1
2 3 2
3 4 2
4 5 1
Sample Output
6
解析
这个就是树的重心的模板题了嘛。
还有题目的第二问就是重心子树中节点数最多的子树的节点数。嗯!刚好符合我们Max数组的定义,直接输出就可以了。
\(Code:\)
#include<cstdio>
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
#define mset(name,val) memset(name,val,sizeof name)
using namespace std;
const int N=20000+50;
int n,size[N],Max[N],ans,cnt;
vector < int > Link[N];
inline void input(void)
{
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
Link[x].push_back(y);
Link[y].push_back(x);
}
}
inline void dp(int r,int f)
{
size[r]=1;
for(int i=0;i<Link[r].size();i++)
{
int Son=Link[r][i];
if(Son==f)continue;
dp(Son,r);
size[r]+=size[Son];
Max[r]=max(Max[r],size[Son]);
}
Max[r]=max(Max[r],n-size[r]);
if(Max[r]==Max[ans]&&r<ans)ans=r;
if(Max[r]<Max[ans])ans=r;
}
int main(void)
{
int T;
scanf("%d",&T);
while(T--)
{
mset(Max,0x00);
mset(size,0x00);
ans=0;cnt=0;Max[0]=0x3f3f3f3f;
input();
dp(1,0);
printf("%d %d\n",ans,Max[ans]);
for(int i=1;i<=n;i++)
Link[i].clear();
}
}
<后记>
『Balancing Act 树的重心』的更多相关文章
- POJ 1655 Balancing Act 树的重心
Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...
- 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 ...
- PKU 1655 Balancing Act(树+树的重心)
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 20005 using na ...
- 『左偏树 Leftist Tree』
新增一道例题 左偏树 Leftist Tree 这是一个由堆(优先队列)推广而来的神奇数据结构,我们先来了解一下它. 简单的来说,左偏树可以实现一般堆的所有功能,如查询最值,删除堆顶元素,加入新元素等 ...
- POJ 1655 - Balancing Act 树型DP
这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include&l ...
- poj1655 Balancing Act 找树的重心
http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- 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 ...
随机推荐
- gitlab使用过程中的需求与解决
序言 在git使用过程中发现指令实在太多,就算记忆后不长用的话很快也会忘记掉,所以编写本文的初衷是为了记录在使用git指令的过程中所遇到的需求与解决方法,毕竟使用git的需求也就那么一些,范围不大,所 ...
- redis与memacache的区别(转)
redis和memecache的不同在于:1.存储方式:memecache 把数据全部存在内存之中,断电后会挂掉,数据不能超过内存大小redis有部份存在硬盘上,这样能保证数据的持久性.2.数据支持类 ...
- kvm虚拟机迁移
一.迁移简介 迁移: 系统的迁移是指把源主机上的操作系统和应用程序移动到目的主机,并且能够在目的主机上正常运行.在没有虚拟机的时代,物理机之间的迁移依靠的是系统备份和恢复技术.在源主机上实时备份操作系 ...
- BZOJ2143: 飞飞侠
2143: 飞飞侠 题意: 给出两个 n ∗ m 的矩阵 A,B,以及 3 个人的坐标 在 (i, j) 支付 Ai,j 的费用可以弹射到曼哈顿距离不超过 Bi,j 的位置 问三个人汇合所需要的最小总 ...
- python3 通过邮件发送测试报告
通过之前的学习,了解到了如何利用excel进行读取数据,如何采用DDT数据驱动,如何使用unittest.下面是将之前所学进行结合,并发送邮件-->leader,废话不多说,上代码: email ...
- 百度TTS的来由
#### https://home-assistant.io/components/tts.baidu/#### https://github.com/charleyzhu/HomeAssistant ...
- dedecms自定义模型内容调用多个Ueditor
关于dedecms后台如何整合百度编辑器(ueditor)网上有很多了,本站就不再赘述了,主要问题是,涉及到如果有内容模型的修改,则按照网络上介绍的方法会发现有BUG.当修改过默认的文章模型或者其他模 ...
- Deepin下配置JDK8
下载JDK 首先在http://www.oracle.com/technetwork/java/javase/downloads/index.html下载对应的JDK 本人下载的是JDK8 解压tar ...
- [LeetCode] Buddy Strings 伙计字符串
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- 自己封装element-ui树组件的过滤
前言:vue开发项目时用到了element-ui的树组件,但是发现一执行过滤事件,树就全部都展开了,为了解决这个问题,只能自己先过滤数剧,再赋值给树组件的data,就避免了一上来全部展开的尴尬. 一. ...