题目大意:
  给定一棵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. D. Relatively Prime Graph

    Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)∈E( ...

  2. Java输入输出流备忘

    重要博客: http://blog.csdn.net/hguisu/article/details/7418161 File dir = new File("\\root");   ...

  3. java基础学习(一)hashcode

    hashcode的作用 hashCode()方法是从Object类继承过来的,Object类中的hashCode()方法返回的是对象在内存中地址转换成的int值,如果对象没有重写hashCode()方 ...

  4. 前端导出文件功能document.execCommand命令

    参照 http://blog.csdn.net/woshinia/article/details/18664903

  5. bootstrap row 行间距

    <div class="clearfix" style="margin-bottom: 10px;"></div>清除浮动加个margi ...

  6. bzoj 1076 状压DP

    我们设w[i][s]为当前到第i关,手中的物品为s的时候,期望得分为多少,其中s为二进制表示每种物品是否存在. 那么就比较容易转移了w[i][s]=(w[i-1][s']+v[j]) *(1/k),其 ...

  7. nginx中fastcgi_params配置参数

    Nginx 的 fastcgi 模块提供了 fastcgi_param 指令来主要处理这些映射关系,下面 Ubuntu 下 Nginx 的一个配置文件,其主要完成的工作是将 Nginx 中的变量翻译成 ...

  8. go的websocket实现

    websocket分为握手和数据传输阶段,即进行了HTTP握手 + 双工的TCP连接 RFC协议文档在:http://tools.ietf.org/html/rfc6455 握手阶段 握手阶段就是普通 ...

  9. pymysql 线程安全pymysqlpool

    # -*-coding: utf-8-*- # Author : Christopher Lee # License: Apache License # File : test_example.py ...

  10. js判断处理undefined类型的数据

    code: resFlag = response.result.data.result; /查询客户为白名单用户时,将"*该企业已被列入黑名单"标记清除 if(typeof res ...