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了一发 最 ...
随机推荐
- Cosplay之孩子的妈咪
很荣幸的担任“孩子妈妈”的角色,站在父母的角度去思考怎样的一个四则运算的APP才算是符合要求,可以受到广大家长的喜爱.不外乎有这样一些功能:1.可以随时的给孩子出题目,而且可以很快的得出正确答案.分析 ...
- 结对项目作业GUI
一.Coding.Net项目地址:https://git.coding.net/zhengsh589/CoupleProject.git 二.PSP表格(完成前): PSP 任务内容 计划共完成需要的 ...
- docker搭建redis未授权访问漏洞环境
这是redis未授权访问漏洞环境,可以使用该环境练习重置/etc/passwd文件从而重置root密码 环境我已经搭好放在了docker hub 可以使用命令docker search ju5ton1 ...
- SpringBoot(四)_Spring Data JPA的使用
JPA 绝对是简化数据库操作的一大利器. 概念 首先了解 JPA 是什么? JPA(Java Persistence API)是 Sun 官方提出的 Java 持久化规范.它为 Java 开发人员提供 ...
- Dubbo学习(五) Dubbo 从下载到编译成功
DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广 ...
- jmeter提取正则表达式中所有关联值-----我想获取所有的ID
[{ "ID": 1, "Name": "张三" }, { "ID": 2, "Name": &qu ...
- 题解 CF762A 【k-th divisor】
emmm...不能说是水题吧...小金羊以为考的是STL(手动滑稽)... 行,这个题说让分解因数(不一定非得质因数). 我们考虑到有第k个数有可能有\(x\cdot x=n\)的毒瘤情况, 并且题目 ...
- [洛谷P4819][中山市选]杀人游戏
题目大意:有一张$n$个点$m$条边的有向图,有一个关键点,如果你访问一个点,你会知道它连出的边中有没有关键点,以及若有的话是哪个.问最优策略下不访问关键点而知道关键点的概率 题解:发现若一个点不是关 ...
- Android性能优化:手把手带你全面了解 内存泄露 & 解决方案
. 简介 即 ML (Memory Leak)指 程序在申请内存后,当该内存不需再使用 但 却无法被释放 & 归还给 程序的现象2. 对应用程序的影响 容易使得应用程序发生内存溢出,即 OOM ...
- BZOJ4727 [POI2017]Turysta 【竞赛图哈密顿路径/回路】
题目链接 BZOJ4727 题解 前置芝士 1.竞赛图存在哈密顿路径 2.竞赛图存在哈密顿回路,当且仅当它是强联通的 所以我们将图缩点后,拓扑排序后一定是一条链,且之前的块内的点和之后块内的点的边一定 ...