P3478 [POI2008]STA-Station
题目描述
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.
输入输出样例
8
1 4
5 6
4 5
6 7
6 8
2 4
3 4
7
//好吧,理解错了题意
//题目让着求一个点,使得以这个点为根时,所有点的深度和最大
//以为是求一个最大深度,其实是求根
//简单题
//随便找一个点当根处理出所有点的深度
//考虑转移方程:
//当根转移到它的儿子上时,它的儿子以及它的儿子的子树的深度会-1
//他儿子的子树之外的点的深度会+1
//所以,用一个size[]记录子树的大小,fa[]记录父亲,dep[]记录深度
//dp[i]表示以i为根的所有点的深度之和
//dp[i]=(dp[fa[i]]-size[i])+(n-size[i])=dp[fa[i]-size[i]*2+n
//第一个括号里就是说i和它的子树的深度都-1了
//第二个括号就是除了i的子树,别的点的深度都+1了
//因为dp数组是从父亲转移来的,所以可以在dfs中传参调用
//然后取最优解 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int N=1e6+; int n;
int head[N],num_edge;
struct Edge
{
int v,nxt;
}edge[N<<]; inline int read()
{
char c=getchar();int num=;
for(;!isdigit(c);c=getchar());
for(;isdigit(c);c=getchar())
num=num*+c-'';
return num;
} inline void add_edge(int u,int v)
{
edge[++num_edge].v=v;
edge[num_edge].nxt=head[u];
head[u]=num_edge;
} int dep[N],fa[N],size[N];
long long ans;
void dfs(int u)
{
size[u]=;
for(int i=head[u],v;i;i=edge[i].nxt)
{
v=edge[i].v;
if(v==fa[u])
continue;
fa[v]=u;
dep[v]=dep[u]+;
ans+=dep[v];
dfs(v);
size[u]+=size[v];
}
} int poi;
void dfs2(int u,long long dep)
{
if(ans<dep||(dep==ans&&u<poi))
ans=dep,poi=u;
for(int i=head[u],v;i;i=edge[i].nxt)
{
v=edge[i].v;
if(v==fa[u])
continue;
dfs2(v,1ll*dep-size[v]*+n);
}
} int main()
{
n=read();
for(int i=,u,v;i<n;++i)
{
u=read(),v=read();
add_edge(u,v);
add_edge(v,u);
}
dfs();
poi=n;
dfs2(,ans);
printf("%d",poi);
return ;
}
P3478 [POI2008]STA-Station的更多相关文章
- 洛谷P3478 [POI2008]STA-Station
P3478 [POI2008]STA-Station 题目描述 The first stage of train system reform (that has been described in t ...
- BZOJ 1131: [POI2008]Sta( dfs )
对于一棵树, 考虑root的答案向它的孩子转移, 应该是 ans[son] = (ans[root] - size[son]) + (n - size[son]). so , 先 dfs 预处理一下, ...
- 1131: [POI2008]Sta
1131: [POI2008]Sta Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 783 Solved: 235[Submit][Status] ...
- BZOJ1131 POI2008 Sta 【树形DP】
BZOJ1131 POI2008 Sta Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=10 ...
- bzoj 1131 [POI2008]Sta 树形dp 转移根模板题
[POI2008]Sta Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1889 Solved: 729[Submit][Status][Discu ...
- [POI2008]Sta(树形dp)
[POI2008]Sta Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面 ...
- [BZOJ1131][POI2008] Sta 树的深度
Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Output ...
- 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<cst ...
- 洛谷 P3478 [POI2008]STA-Station
题目描述 The first stage of train system reform (that has been described in the problem Railways of the ...
- [BZOJ1131/POI2008]Sta树的深度
Description 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Output ...
随机推荐
- stm32f103的HSI设置
HSI基本知识 HSI是8MRC震荡电路,精度1%. PLL的设置必须在其被激活前完成,输出必须被设置温48M或者72M LSE:通过在备份域控制寄存器(RCC_BDCR)里的LSEON位启动和关闭. ...
- mouseenter 与 mouseover 区别于选择
mouseover事件, 箭头在子元素移动会触发冒泡事件, 子元素的鼠标箭头可触父元素方法, 相反,mouseenter事件功能与mouseover类似, 但鼠标进入某个元素不会冒泡触发父元素方法. ...
- Angular 学习笔记 immer 使用
https://github.com/immerjs/immer#supported-object-types immer 是用来做 immutable 的. angular 的 change det ...
- VS.NET(C#)--2.3良构的XHTML
良构的XHTML 1.关闭所有标签 2.禁止标签嵌套 3.区分大小写 4.引号 所有属性值都要置于引号中 5.唯一的根元素<html></html> 6.保留字符 XML中五 ...
- CentOS 6.5 iptables原理详解以及功能说明
CentOS 6.5 iptables原理详解以及功能说明 来源 https://blog.51cto.com/tanxw/1389114 前言 iptables其实就是Linux下的一个开源的信息过 ...
- 浅谈javascript中变量作用域和内存(1)
先理解两个概念:基本类型和引用类型的值 1.基本类型和引用类型的值 (1)定义: 基本类型:指简单的数据段,比如按值访问的js五种基本数据类型undefined.null.boolean.number ...
- 【数字图像处理】gamma变换
论文:gamma校正的快速算法及其c语言实现 gamma变换实现过程 假设图像中有一个像素,值是 200 ,那么对这个像素进行校正必须执行如下步骤: 1. 归一化 :将像素值转换为 0 - 1 之 ...
- iOS开发 CGAffineTransform 让图片旋转, 旋转后获得图片旋转的角度
1.让图片旋转 UIImageView *imageView = [[UIImageView alloc]init]; imageView.frame = CGRectMake(50, 50, 200 ...
- python使用Pyinstaller打包
一.前言 python文件打包,将.py文件转化成.exe文件(windows平台),可以使用Pyinstaller来打包 Pyinstaller可以在全平台下使用,但是请注意打包生成的文件不能在全平 ...
- 善用#waring,#pragma mark 标记
在项目开发中,我们不可能对着需求一口气将代码都写好.开发过程中肯定遇到诸如需求变动,业务逻辑沟通,运行环境的切换等这些问题.当项目大的时候,如果木有形成统一的代码规范,在项目交接和开发人员沟通上将会带 ...