bzoj千题计划151:bzoj1131: [POI2008]Sta
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.
输入输出样例
bzoj千题计划151:bzoj1131: [POI2008]Sta的更多相关文章
- bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块
http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...
- bzoj千题计划196:bzoj4826: [Hnoi2017]影魔
http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...
- bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪
http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...
- bzoj千题计划177:bzoj1858: [Scoi2010]序列操作
http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...
- bzoj千题计划317:bzoj4650: [Noi2016]优秀的拆分(后缀数组+差分)
https://www.lydsy.com/JudgeOnline/problem.php?id=4650 如果能够预处理出 suf[i] 以i结尾的形式为AA的子串个数 pre[i] 以i开头的形式 ...
- bzoj千题计划304:bzoj3676: [Apio2014]回文串(回文自动机)
https://www.lydsy.com/JudgeOnline/problem.php?id=3676 回文自动机模板题 4年前的APIO如今竟沦为模板,,,╮(╯▽╰)╭,唉 #include& ...
- bzoj千题计划292:bzoj2244: [SDOI2011]拦截导弹
http://www.lydsy.com/JudgeOnline/problem.php?id=2244 每枚导弹成功拦截的概率 = 包含它的最长上升子序列个数/最长上升子序列总个数 pre_len ...
- 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]][ ...
- bzoj千题计划278:bzoj4590: [Shoi2015]自动刷题机
http://www.lydsy.com/JudgeOnline/problem.php?id=4590 二分 这么道水题 没long long WA了两发,没判-1WA了一发,二分写错WA了一发 最 ...
随机推荐
- 30_数据库_第30天java_jdbc_(DBUtils)_讲义
今日内容介绍 1.DBUtils 2.连接池 01DButils工具类的介绍个三个核心类 * A: DButils工具类的介绍个三个核心类 * a: 概述 * DBUtils是java编程中的数据库操 ...
- String 类 常用函数
构造方法摘要: String(byte[] bytes) 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String. String(char[] value) ...
- Sql Server自增ID与序号的使用
SQL 自增ID alter table a add id int identity(1,1) not null 这里为 a 表增加一个 id 字段,其中identity(1,1)代表自增,第一个1代 ...
- nginx负载均衡和tomcat热部署简单了解
简单说下几个名词 nginx 它是一个反向代理,实际上就是一台负责转发的代理服务器,貌似充当了真正服务器的功能,但实际上并不是,代理服务器只是充当了转发的作用,并且从真正的服务器那里取得返回的 ...
- 微信小程序入门一: 简易form、本地存储
实例内容 登陆界面 处理登陆表单数据 处理登陆表单数据(异步) 清除本地数据 实例一: 登陆界面 在app.json中添加登陆页面pages/login/login,并设置为入口. 保存后,自动生成相 ...
- BugScan插件编写高(gǎo)级(jī)教程
声明:本文最先发布在:http://q.bugscan.net/t/353 转载请注明出处 有问题可以和我交流 邮件(Medici.Yan@gmail.com) 个人博客地址:http://www.c ...
- 关于2013年1月21日的DNS故障分析文章
首页 资讯 小组 资源 注册 登录 首页 最新文章 业界 开发 IT技术 设计 创业 IT职场 访谈 在国外 经典回顾 更多 > - 导航条 - 首页 最新文章 业界 - Googl ...
- C# 窗体文件下的 MainForm.cs,MainForm.Designer.cs,MainForm.resx,是什么,干什么
Form.cs和Form.Designer.cs其实是一个类,Visual Studio为了让我们方便管理,用partial关键字把窗体类给拆开了, Form.Designer.cs存放的是窗体的布局 ...
- implement min heap
class MinHeap{ private ArrayList<Integer> arr; private int DEFAULT_LEN = 10; public MinHeap(){ ...
- 传说中的WCF:消息拦截与篡改
我们知道,在WCF中,客户端对服务操作方法的每一次调用,都可以被看作是一条消息,而且,可能我们还会有一个疑问:如何知道客户端与服务器通讯过程中,期间发送和接收的SOAP是什么样子.当然,也有人是通过借 ...