题目大意:
  给定一棵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的更多相关文章

  1. 洛谷P3478 [POI2008]STA-Station

    P3478 [POI2008]STA-Station 题目描述 The first stage of train system reform (that has been described in t ...

  2. [LeetCode] Gas Station 加油站问题

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  3. [BZOJ1112][POI2008]砖块Klo

    [BZOJ1112][POI2008]砖块Klo 试题描述 N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另 ...

  4. [bzoj1122][POI2008]账本BBB

    1122: [POI2008]账本BBB Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 402  Solved: 202[Submit][Status ...

  5. 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 ...

  6. BZOJ 1113: [Poi2008]海报PLA

    1113: [Poi2008]海报PLA Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1025  Solved: 679[Submit][Statu ...

  7. BZOJ 1116: [POI2008]CLO

    1116: [POI2008]CLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 922  Solved: 514[Submit][Status][ ...

  8. BZOJ 1112: [POI2008]砖块Klo

    1112: [POI2008]砖块Klo Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1736  Solved: 606[Submit][Statu ...

  9. BZOJ 1124: [POI2008]枪战Maf

    1124: [POI2008]枪战Maf Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 617  Solved: 236[Submit][Status ...

随机推荐

  1. [04] css 选择器

    1.元素选择器 常见的html标签元素 h1 { color: red; } body { background: red; } 2.分组选择器 例如body和h2标签的字体颜色都是red,使用逗号将 ...

  2. es6+最佳入门实践(10)

    10.Generator 10.1.Generator是什么? Generator函数是ES6提供的一种异步编程解决方案.在它的内部封装了多个状态,因此,又可以理解为一种状态机,执行Generator ...

  3. Java并发(11)- 有关线程池的10个问题

    引言 在日常开发中,线程池是使用非常频繁的一种技术,无论是服务端多线程接收用户请求,还是客户端多线程处理数据,都会用到线程池技术,那么全面的了解线程池的使用.背后的实现原理以及合理的优化线程池的大小等 ...

  4. Activiti工作流引擎核心介绍

    引言 Activiti 作为一个遵从 Apache 许可的工作流和业务流程管理开源平台,其核心是基于 Java 的超快速.超稳定的 BPMN 2.0 流程引擎,强调流程服务的可嵌入性和可扩展性,同时更 ...

  5. HDU2057

    http://acm.hdu.edu.cn/showproblem.php?pid=2057 涉及到16进制内的加法,可以用%I64x直接来处理,要注意到16进制中负数是用补码来表示的.一个比较困惑的 ...

  6. vivo面试学习3(git和svn的区别)

    git和svn有什么区别? svn: 系统特点: 1).集中式版本控制系统(存在一个中央版本库,所有开发人员所使用的代码都是来源于版本库,提交代码也是这个中央版本库) 2).企业内部并行集中开发 3) ...

  7. SSL步骤

    SSL步骤 被认证的服务器 1.创建keystore 2.创建信任证书 3.导出信任证书供客户端使用 客户端 1.创建keystore(如果不存在) 2.导入信任证书

  8. python用户登录,密码错误3次则锁定

    需求: 1.输入用户名密码 2.认证成功后显示欢迎信息 3.输错三次后锁定 实现思路: 1.判断用户是否在黑名单,如果在黑名单提示账号锁定. 2.判断用户是否存在,如果不存在提示账号不存在. 3.判断 ...

  9. JAVA -- JDK JRE JAR

    转载:http://blog.csdn.net/wym19830218/article/details/5399401 JDK里面的工具也是用JAVA编写的,它们本身运行的时候也需要一套JRE,如C: ...

  10. Jmeter接口测试常见的乱码问题三种解决方法

    使用Jmeter时经常遇到中文乱码问题,下面总结三种常用的解决方式. 1. 2.在Jmeter安装文件bin中找到jmeter.properties,打开jmeter.properties,搜索“IS ...