http://www.lydsy.com/JudgeOnline/problem.php?id=1131

dp[i]=dp[fa[i]]-son[i]+n-son[i]

#include<cstdio>
#include<iostream> using namespace std; #define N 1000001 int front[N],to[N<<],nxt[N<<],tot; int fa[N];
bool vis[N]; int dep[N],son[N]; long long dp[N]; int head=,tail=;
int q[N]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void add(int u,int v)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot;
} void bfs()
{
q[]=;
vis[]=true;
int now,t;
while(head<=tail)
{
now=q[head++];
for(int i=front[now];i;i=nxt[i])
{
t=to[i];
if(!vis[t])
{
vis[t]=true;
fa[t]=now;
q[++tail]=t;
}
}
}
} void dfs(int x,int d)
{
dep[x]=d; son[x]=;
for(int i=front[x];i;i=nxt[i])
if(!dep[to[i]])
{
dfs(to[i],d+);
son[x]+=son[to[i]];
}
} int main()
{
int n;
read(n);
int u,v;
for(int i=;i<n;++i)
{
read(u);
read(v);
add(u,v);
}
bfs();
dfs(,);
for(int i=;i<=n;++i) dp[]+=dep[i];
int ans=;
long long mx=dp[];
for(int i=;i<=n;++i)
{
u=q[i];
dp[u]=dp[fa[u]]-son[u]+n-son[u];
if(dp[u]>mx)
{
mx=dp[u];
ans=u;
}
else if(dp[u]==mx && u<ans) ans=u;
}
cout<<ans;
}

题目描述

The first stage of train system reform (that has been described in the problem Railways of the third stage of 14th Polish OI.

However, one needs not be familiar with that problem in order to solve this task.) has come to an end in Byteotia. The system consists of bidirectional segments of tracks that connect railway stations. No two stations are (directly) connected by more than one segment of tracks.

Furthermore, it is known that every railway station is reachable from every other station by a unique route. This route may consist of several segments of tracks, but it never leads through one station more than once.

The second stage of the reform aims at developing train connections.

Byteasar count on your aid in this task. To make things easier, Byteasar has decided that:

one of the stations is to became a giant hub and receive the glorious name of Bitwise, for every other station a connection to Bitwise and back is to be set up, each train will travel between Bitwise and its other destination back and forth along the only possible route, stopping at each intermediate station.

It remains yet to decide which station should become Bitwise. It has been decided that the average cost of travel between two different stations should be minimal.

In Byteotia there are only one-way-one-use tickets at the modest price of  bythaler, authorising the owner to travel along exactly one segment of tracks, no matter how long it is.

Thus the cost of travel between any two stations is simply the minimum number of tracks segments one has to ride along to get from one stations to the other.

Task Write a programme that:

reads the description of the train system of Byteotia, determines the station that should become Bitwise, writes out the result to the standard output.

给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大

输入输出格式

输入格式:

The first line of the standard input contains one integer  () denoting the number of the railway stations. The stations are numbered from  to . Stations are connected by  segments of tracks. These are described in the following  lines, one per line. Each of these lines contains two positive integers  and  (), separated by a single space and denoting the numbers of stations connected by this exact segment of tracks.

输出格式:

In the first and only line of the standard output your programme should print out one integer - the optimum location of the Bitwise hub.

If more than one optimum location exists, it may pick one of them arbitrarily.

输入输出样例

输入样例#1: 复制

8
1 4
5 6
4 5
6 7
6 8
2 4
3 4
输出样例#1: 复制

7

bzoj千题计划151:bzoj1131: [POI2008]Sta的更多相关文章

  1. bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块

    http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...

  2. bzoj千题计划196:bzoj4826: [Hnoi2017]影魔

    http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...

  3. bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪

    http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...

  4. bzoj千题计划177:bzoj1858: [Scoi2010]序列操作

    http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...

  5. bzoj千题计划317:bzoj4650: [Noi2016]优秀的拆分(后缀数组+差分)

    https://www.lydsy.com/JudgeOnline/problem.php?id=4650 如果能够预处理出 suf[i] 以i结尾的形式为AA的子串个数 pre[i] 以i开头的形式 ...

  6. bzoj千题计划304:bzoj3676: [Apio2014]回文串(回文自动机)

    https://www.lydsy.com/JudgeOnline/problem.php?id=3676 回文自动机模板题 4年前的APIO如今竟沦为模板,,,╮(╯▽╰)╭,唉 #include& ...

  7. bzoj千题计划292:bzoj2244: [SDOI2011]拦截导弹

    http://www.lydsy.com/JudgeOnline/problem.php?id=2244 每枚导弹成功拦截的概率 = 包含它的最长上升子序列个数/最长上升子序列总个数 pre_len ...

  8. bzoj千题计划291:bzoj3640: JC的小苹果

    http://www.lydsy.com/JudgeOnline/problem.php?id=3640 dp[i][j] 表示i滴血到达j的概率 dp[i][j] = Σ dp[i+val[i]][ ...

  9. bzoj千题计划278:bzoj4590: [Shoi2015]自动刷题机

    http://www.lydsy.com/JudgeOnline/problem.php?id=4590 二分 这么道水题 没long long WA了两发,没判-1WA了一发,二分写错WA了一发 最 ...

随机推荐

  1. Linux手动添加系统环境共享库路径

    1.在以下目录 #/etc/ld.so.conf.d 添加相应的xxx.conf 2.写入所要共享库的路径 如:/usr/lib/ 3.导入共享库配置 $ldconfig

  2. Java第二天——标识符命名规则、Java的知识、快捷键的使用、Scanner获取值的常用方法

    1.标识符命名规则 字母.下划线.数字.美元符号($)由这四个部分组成. 标识符=首字母+其他 首字母:字母.下划线.美元符号($) 其他:字母.下划线.数字.美元符号($) 注意: 1.首字母不能为 ...

  3. 【贪心算法】POJ-3040 局部最优到全局最优

    一.题目 Description As a reward for record milk production, Farmer John has decided to start paying Bes ...

  4. 10th 规格说明书练习——吉林一日游

    活动规格说明书 吉林市一日游 版本:1.0 编订:王东涵 团队:2016级计算机技术全体同学 日期:2016-11-20 目录 1.引言 1.1 编写目的 1.2 背景 1.3 定义 1.4 参考资料 ...

  5. MYSQL中可以实现类似IF判断的方法

    MYSQL中可以实现类似IF判断的方法 新建一张客户表,如下:sex:1-男,2-女,3-未知:level是客户的级别:1-超级VIP客户,2-VIP客户,3-普通客户 方式一:case函数:流程控制 ...

  6. PHP TS 和 NTS 版本选择

    在PHP 开发和生产环境搭建过程中,需要安装PHP语言解析器.官方提供了2种类型的版本,线程安全(TS)版和非线程安全(NTS)版,有时后我们开发环境和实际生产的环境有所不同,因此也需要选择安装对应的 ...

  7. 再谈MySql索引

    一.索引简介 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 索引分单列索引(主键索引.唯一索引.普通索引)和组合索引.单列索引,即一个索引只包含单个列 ...

  8. Windows下python 3.0版本django的安装、配置、与启动

    使用的环境是Windows操作系统,python的环境是3.6,django是官网上最新的版本1.10.6,本文介绍从安装python之后怎样用过pip管理工具安装django,以及django的项目 ...

  9. 【刷题】洛谷 P4319 变化的道路

    题目描述 小 w 和小 c 在 H 国,近年来,随着 H 国的发展,H 国的道路也在不断变化着 根据 H 国的道路法,H 国道路都有一个值 \(w\) ,表示如果小 w 和小 c 通过这条道路,那么他 ...

  10. Centos7搭建redis集群及安装sentinel

    准备三个节点,系统版本为CentOS7.3 11.0.8.15 master 11.0.8.16 slave01 11.0.8.17 slave02 1.安装redis # yum install - ...