题目描述

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
 //本题相当于洛谷P2986 [USACO10MAR]伟大的奶牛聚集   而且是简化版的题目。。。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define man 1000010
ll son[man],dis[man],n,f[man];
struct edge
{ ll next,to;}e[man<<];
ll head[man<<],num=;
inline void add(ll from,ll to)
{ e[++num].next=head[from];e[num].to=to;head[from]=num;}
ll precalc(int u,int fa)
{ int tot=;
for(ll i=head[u];i;i=e[i].next)
{ ll to=e[i].to;
if(to==fa) continue;
ll child=precalc(to,u);
dis[u]+=dis[to]+child;
tot+=child;
}
return son[u]=tot+;
}
void dfs(int u,int fa)
{ for(int i=head[u];i;i=e[i].next)
{ ll to=e[i].to;
if(to==fa)continue;
f[to]=f[u]-son[to]*+(n-son[to])*;
dfs(to,u);
}
}
void ad(int elem)
{ elem+=dis[];}
int main()
{ scanf("%lld",&n);
for(int i=;i<n;i++)
{ ll u,v;
scanf("%lld%lld",&u,&v);
add(u,v);add(v,u);
}
memset(f,,sizeof(f));
precalc(,-);//预处理每个节点的子节点的个数和每个子树的距离
dfs(,-);
for_each(f+,f+n+,ad);//相当于f[i]+=dis[1];
cout<<(max_element(f+,f+n+)-f)<<endl;
return ;
}

洛谷 P3478 [POI2008]STA-Station的更多相关文章

  1. 洛谷P3478 [POI2008]STA-Station

    P3478 [POI2008]STA-Station 题目描述 The first stage of train system reform (that has been described in t ...

  2. BZOJ1123或洛谷3469 [POI2008]BLO-Blockade

    BZOJ原题链接 洛谷原题链接 若第\(i\)个点不是割点,那么只有这个点单独形成一个连通块,其它点依旧连通,则答案为\(2\times (n-1)\). 若第\(i\)个点是割点,那么去掉这个点相关 ...

  3. 题解【洛谷P3478】[POI2008]STA-Station

    题面 设\(dp_i\)表示以\(i\)为根节点时所有节点的深度之和. 首先以 \(1\) 为根求出所有点深度之和\(dp_1\),并预处理每个点的子树大小. 设 \(v\) 是 \(u\) 的孩子, ...

  4. 洛谷 P3477 [POI2008]PER-Permutation 解题报告

    P3477 [POI2008]PER-Permutation 题目描述 Multiset is a mathematical object similar to a set, but each mem ...

  5. 洛谷 P3467 [POI2008]PLA-Postering

    P3467 [POI2008]PLA-Postering 题目描述 All the buildings in the east district of Byteburg were built in a ...

  6. 洛谷 P3469 [POI2008]BLO-Blockade (Tarjan,割点)

    P3469 [POI2008]BLO-Blockade https://www.luogu.org/problem/P3469 题目描述 There are exactly nn towns in B ...

  7. 洛谷P3469[POI2008]BLO-Blockade

    题目 割点模板题. 可以将图中的所有点分成两部分,一部分是去掉之后不影响图的连通性的点,一部分是去掉之后影响连通性的点,称其为割点. 然后分两种情况讨论,如果该点不是割点,则最终结果直接加上2*(n- ...

  8. 洛谷 P3469 [POI2008]BLO-Blockade 题解

    一道经典的割点例题,用size数组记录该子树有多少个节点,sum是这棵搜索树上有多少个节点,sum*(n-sum-1)是将点删掉后的数对数量. #include<iostream> #in ...

  9. 「洛谷3469」「POI2008」BLO-Blockade【Tarjan求割点】

    题目链接 [洛谷传送门] 题解 很显然,当这个点不是割点的时候,答案是\(2*(n-1)\) 如果这个点是割点,那么答案就是两两被分开的联通分量之间求组合数. 代码 #include <bits ...

随机推荐

  1. ORACLE用户的加锁、解锁

    在DBA的日常工作中,经常遇到为Oracle用户解锁的操作:这篇文章给出在命令行下进行Oracle用户解锁的操作方法,通过几条简单的解锁语句就能完成此项工作.下面是具体的过程: 默认的scott用户是 ...

  2. cowboy的get和post的例子

    官方get和post的代码是有问题的,1.1下运行crash,这里修改了下,贴代码 创建工程 rebar-creator create-app testCowboy testCowboy_app.er ...

  3. erlang学习之自定义behaviour

    behaviour是啥,看了资料做了demo以后,感觉像接口,话不多说,祭代码 R15开始,回调模型使用callback来约定,更加好理解了 test_behavior.erl -module(tes ...

  4. 使用GDI+保存带Alpha通道的图像

    带Alpha通道的图像(ARBG)在通过GDIPlus::Bitmap::FromHBITMAP等转为GDI+位图,再存储时,透明区域会变成纯黑(也有可能是纯白?).   网上找了两段保持透明的实现代 ...

  5. java代码---------实现布尔型的功能,是否执行下一步的关键

    总结:灵活 package com.sads; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...

  6. 自己设计代理IP池

    大体思路 使用redis作为队列,买了一份蘑菇代理,但是这个代理每5秒可以请求一次,我们将IP请求出来,从redis列表队列的左侧插入,要用的时候再从右侧取出,请求成功证明该IP是可用的,将该代理IP ...

  7. Druid.io系列(九):数据摄入

    1. 概述 Druid的数据摄入主要包括两大类: 1. 实时输入摄入:包括Pull,Push两种 - Pull:需要启动一个RealtimeNode节点,通过不同的Firehose摄取不同种类的数据源 ...

  8. JavaScript笔录

    JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 1.JavaScri ...

  9. [Z]牛人林达华推荐有关机器学习的数学书籍

    1. 线性代数 (Linear Algebra): 我想国内的大学生都会学过这门课程,但是,未必每一位老师都能贯彻它的精要.这门学科对于Learning是必备的基础,对它的透彻掌握是必不可少的.我在科 ...

  10. sql之分段统计

    sql之分段统计 需求:获取一个县所有家庭人数在1-2人,3-4人,5-6人,6人以上的家庭数的数组 思路:通过CASE WHEN 将 CBFCYSL分组,然后统计数据条数. 语句: SELECT T ...