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.

 

输入输出样例

输入样例#1: 复制
8
1 4
5 6
4 5
6 7
6 8
2 4
3 4
输出样例#1: 复制
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+5;

int n;
int head[N],num_edge;
struct Edge
{
    int v,nxt;
}edge[N<<1];

inline int read()
{
    char c=getchar();int num=0;
    for(;!isdigit(c);c=getchar());
    for(;isdigit(c);c=getchar())
        num=num*10+c-'0';
    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]=1;
    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]+1;
        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]*2+n);
    }
}

int main()
{
    n=read();
    for(int i=1,u,v;i<n;++i)
    {
        u=read(),v=read();
        add_edge(u,v);
        add_edge(v,u);
    }
    dfs(1);
    poi=n;
    dfs2(1,ans);
    printf("%d",poi);
    return 0;
}

 

posted @ 2018-03-31 10:48  whymhe  阅读(174)  评论(0编辑  收藏  举报