题目大意:
  给定一棵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. 对Office文档进行授权

    Microsoft.Office.Interop.Word.ApplicationClass app = new Microsoft.Office.Interop.Word.ApplicationCl ...

  2. es6快速排序

    let qsort = fn =>([x,...xn]) => x == null ? [] : [ ...qsort(fn)(xn.filter(a=>fn(a,x))), x, ...

  3. HDU5748---(记录每个元素的 最长上升子序列 nlogn)

    分析: 给一个序列,求出每个位置结尾的最长上升子序列 O(n^2) 超时 #include "cstdio" #include "algorithm" #def ...

  4. Linux echo命令打印带有颜色的字

    一.命令格式如下: echo -e "\033[字背景颜色;文字颜色m字符串\033[0m"            例如:                      echo -e ...

  5. 动态规划:状压DP-斯坦纳树

    最小生成树是最小斯坦纳树的一种特殊情况 最小生成树是在给定的点集和边中寻求最短网络使所有点连通 而最小斯坦纳树允许在给定点外增加额外的点,使生成的最短网络开销最小 BZOJ2595 题意是给定一个棋盘 ...

  6. 【HDU4405】Aeroplane chess [期望DP]

    Aeroplane chess Time Limit: 1 Sec  Memory Limit: 32 MB[Submit][Stataus][Discuss] Description Hzz lov ...

  7. 【HDU3853】LOOPS [期望DP]

    LOOPS Time Limit: 5 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description Akemi Homura is a ...

  8. 【洛谷 P4289】[HAOI2008]移动玩具(搜索)

    其实这题可以不用状压.. 提供一种新思路. 我们在读入目标棋盘的时候,把当前位置的数和当前棋盘进行比较,如果不一样,如果当前是\(1\),目标是\(0\),那么我们就把当前位置加入\(needmove ...

  9. windows注册表存储位置

    win7/8/10 通常情况: HKEY_LOCAL_MACHINE \SYSTEM : \system32\config\system HKEY_LOCAL_MACHINE \SAM : \syst ...

  10. Python爬虫学习 - day2 - 站点登陆

    利用Python完成简单的站点登陆 最近学习到了爬虫,瞬时觉得很高大上,想取什么就取什么,感觉要上天.这里分享一个简单的登陆抽屉新热榜的教程(因为它不需要验证码,目前还没有学会图像识别.哈哈),供大家 ...