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了一发 最 ...
随机推荐
- poi读取Excel文件和图片
首先得说一下,Excel文件是有03版和07版的区别的,也就是.xls和.xlsx,这两个文件需要分开读取. 其它的废话就不说了,直接贴代码: package util; import java.io ...
- 0523 Scrum项目6.0
0523 团队项目6.0 一,组员任务完成情况 首页设计初步完成但是需要优化界面,只能简单的输出信息和在首页进行登录.界面极其简单.使用了Javabean的设计模式,进行改进,使得页面的里的代码看起来 ...
- ORA-06530: 引用未初始化的组合 ;
select * FROM TABLE(fun_test_1) : ORA-06530: 引用未初始化的组合ORA-06512: 在 "PCISS.FUN_TEST_1", lin ...
- C 语言assert使用
1.assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:#include <assert.h>void assert( i ...
- BZOJ5324 JXOI2018守卫(区间dp)
对于每个区间[l,r],显然右端点r是必须放置守卫的.考虑其不能监视到的点,构成一段段区间.一个非常显然但我就是想不到的性质是,对于这样的某个区间[x,y],在(y+1,r)内的点都是不能监视到这个区 ...
- 【刷题】BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- 【BZOJ 1098】办公楼(补图连通块个数,Bfs)
补图连通块个数这大概是一个套路吧,我之前没有见到过,想了好久都没有想出来QaQ 事实上这个做法本身就是一个朴素算法,但进行巧妙的实现,就可以分析出它的上界不会超过 $O(n + m)$. 接下来介绍一 ...
- 【bzoj2438】 中山市选2011—杀人游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=2438 (题目链接) 题意 n个点的有向图,其中有一个是杀手,每个人成为杀手的概率相同.警察询问一个人 ...
- Android Studio aidl文件路径自定义问题
1.aidl旧文件夹中添加的内容无法编译 sourceSets中主要是把把src/main/aidl文件也作为java.srcDirs, resources.srcDirs,这样当编译程序时,AIDL ...
- 理解 OAuth 2.0
理解OAuth 2.0 http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html 一.简介 OAuth是一个关于授权(authorization)的开 ...