[POI2008]Station
题目大意:
给定一棵n个结点的树,求一个点x作为根,使得所有结点到x的距离和最小。
思路:
树形DP。
首先考虑将1作为根的情况。
很显然我们可以用一遍O(n)的DFS预处理出每个结点所对应子树大小size和子树内每个结点到这个结点的距离和sum。
这样也就相当于我们递推求出了以1作为根时各结点到它的距离和。
现在考虑将根往下转移。
假设我们现在要从u转移到v,那么显然在sum[u]的基础上,sum[v]需要变化的是(u,v)之间的连接的边。
也就是说sum[y]=sum[x]+n-size[y]*2。
这样我们就可以用O(n)时间将根往下转移。
总的时间复杂度还是O(n)的。
#include<cstdio>
#include<cctype>
typedef long long int64;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=;
int head[N],to[N<<],next[N<<],sz;
inline void add_edge(const int &u,const int &v) {
to[++sz]=v; next[sz]=head[u]; head[u]=sz;
to[++sz]=u; next[sz]=head[v]; head[v]=sz;
}
int64 sum[N],max;
int n,size[N],root;
void dfs(const int &x,const int &par) {
size[x]=;
for(int i=head[x];i;i=next[i]) {
const int &y=to[i];
if(y==par) continue;
dfs(y,x);
size[x]+=size[y];
sum[x]+=sum[y]+size[y];
}
}
void move(const int &x,const int &par) {
if(sum[x]>max) {
max=sum[x];
root=x;
} else if(sum[x]==max&&x<root) {
root=x;
}
for(int i=head[x];i;i=next[i]) {
const int &y=to[i];
if(y==par) continue;
sum[y]=sum[x]+n-size[y]*;
move(y,x);
}
}
int main() {
n=getint();
for(register int i=;i<n;i++) {
add_edge(getint(),getint());
}
dfs(,);
move(,);
printf("%d\n",root);
return ;
}
[POI2008]Station的更多相关文章
- 洛谷P3478 [POI2008]STA-Station
P3478 [POI2008]STA-Station 题目描述 The first stage of train system reform (that has been described in t ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [BZOJ1112][POI2008]砖块Klo
[BZOJ1112][POI2008]砖块Klo 试题描述 N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另 ...
- [bzoj1122][POI2008]账本BBB
1122: [POI2008]账本BBB Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 402 Solved: 202[Submit][Status ...
- PAT 1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- BZOJ 1113: [Poi2008]海报PLA
1113: [Poi2008]海报PLA Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1025 Solved: 679[Submit][Statu ...
- BZOJ 1116: [POI2008]CLO
1116: [POI2008]CLO Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 922 Solved: 514[Submit][Status][ ...
- BZOJ 1112: [POI2008]砖块Klo
1112: [POI2008]砖块Klo Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1736 Solved: 606[Submit][Statu ...
- BZOJ 1124: [POI2008]枪战Maf
1124: [POI2008]枪战Maf Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 617 Solved: 236[Submit][Status ...
随机推荐
- spring中@PropertySource注解的使用
概述: The @PropertySource annotation provides a convenient and declarative mechanism for adding aPrope ...
- namenode磁盘满引发recover edits文件报错
前段时间公司hadoop集群宕机,发现是namenode磁盘满了, 清理出部分空间后,重启集群时,重启失败. 又发现集群Secondary namenode 服务也恰恰坏掉,导致所有的操作log持续写 ...
- gitlab7.2安装
系统:centos6.4 1.安装依赖包 导入epel: useradd git wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-rel ...
- LOJ tangjz的背包
题目大意 有 \(n\) 个物品, 第 \(i\) 个物品的体积为 \(i\) 令 \(f(x)\) 为 选择 \(m\) 个物品, 体积和为 \(x\) 的方案数 令 \(V = \sum_{i=1 ...
- C语言编译各过程
1.预处理 此阶段主要完成#符号后面的各项内容到源文件的替换,往往一些莫名其妙的错误都是出现在头文件中的,要在工程中注意积累一些错误知识. (1).#ifdef等内容,完成条件编译内容的替换 (2). ...
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set
Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...
- 线程局部存储 TLS
C/C++运行库提供了TLS(线程局部存储),在多线程还未产生时,可以将数据与正在执行的线程关联.strtok()函数就是一个很好的例子.与它一起的还有strtok_s(),_tcstok_s()等等 ...
- linux基础——关于chmod用户权限和文件的相关操作
第一部分:1) 新建用户natasha,uid为1007,gid为555,备注信息为“master” 操作:useradd natasha新建natasha:修改uid是,usermod -u 100 ...
- RtlInitUnicodeString
代码1: WCHAR enumeratorName[] = {}; UNICODE_STRING unicodeEnumName; RtlInitUnicodeString(&unicodeE ...
- HTTP===http首部字段
HTTP 首部字段 HTTP 首部字段是构成 HTTP 报文的要素之一.在客户端与服务器之间以 HTTP 协议进行通信的过程中,无论是请求还是响应都会使用首部字段,它能起到传递额外重要信息的作用. 使 ...