题目大意:
  给定一棵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. [hihocoder 1050]求树的最长链

    题目链接:http://hihocoder.com/problemset/problem/1050 两种方法: 1. 两遍dfs,第一次随便找一个根,找到距离这个根最远的点,这个点必然是最长链的一端. ...

  2. Codeforces Round #526 (Div. 2) E. The Fair Nut and Strings

    E. The Fair Nut and Strings 题目链接:https://codeforces.com/contest/1084/problem/E 题意: 输入n,k,k代表一共有长度为n的 ...

  3. codeforces 799C Fountains

    C. Fountains time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. ubuntu安装GraphicsMagick

    一. sudo apt-get install graphicsmagick 二. http://www.cnblogs.com/cocowool/archive/2010/08/16/1800954 ...

  5. AngularJs学习——实现列表内容项的增加删除

    效果截图: 说明:引入bootstrap.min.css样式库和angular.min.js的静态资源库,实现列表内容的增加和删除操作. AngularJS代码: <script src=&qu ...

  6. [codevs3160]最长公共子串解题报告|后缀自动机

    给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 样例就觉得不能更眼熟啊...好像之前用后缀数组做过一次 然后发现后缀自动机真的好好写啊...(当然当时学后缀数组的时候也这么认为... 这 ...

  7. 【BZOJ】1827: [Usaco2010 Mar]gather 奶牛大集会

    [算法]树型DP||树的重心(贪心) [题解] 两遍DFS,第一次得到所有节点子树的路径和,第二次给出除了该子树外其它部分的路径和,时时计算答案. long long!!! #include<c ...

  8. bzoj 1016 深搜

    首先我们知道MST的一些性质,对于这道题来说就是,假设我们先求出一颗MST设为G,由已知边权相同的边最多会有10条,那么假设我们在这10条边中选取size条边∈G,那么我们在这边权相同的边集E中任意选 ...

  9. MYSQL5.7修改密码

    参考:https://www.cnblogs.com/activiti/p/7810166.html # alter user 'root'@'localhost' identified by '12 ...

  10. java servlet输出数据

    httpServletResponse.setContentType("text/html;charset=UTF-8"); httpServletResponse.getWrit ...