zoukankan      html  css  js  c++  java
  • 洛谷 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 //本题相当于洛谷P2986 [USACO10MAR]伟大的奶牛聚集   而且是简化版的题目。。。 
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 #define ll long long
     5 #define man 1000010
     6 ll son[man],dis[man],n,f[man];
     7 struct edge
     8 {    ll next,to;}e[man<<2];
     9 ll head[man<<2],num=0;
    10 inline void add(ll from,ll to)
    11 {    e[++num].next=head[from];e[num].to=to;head[from]=num;}
    12 ll precalc(int u,int fa)
    13 {    int tot=0;
    14     for(ll i=head[u];i;i=e[i].next)
    15     {    ll to=e[i].to;
    16         if(to==fa) continue;
    17         ll child=precalc(to,u);
    18         dis[u]+=dis[to]+child;
    19         tot+=child;
    20         }
    21     return son[u]=tot+1;
    22     }
    23 void dfs(int u,int fa)
    24 {    for(int i=head[u];i;i=e[i].next)
    25     {    ll to=e[i].to;
    26         if(to==fa)continue;
    27         f[to]=f[u]-son[to]*1+(n-son[to])*1;
    28         dfs(to,u);
    29         }
    30     }
    31 void ad(int elem)
    32 {    elem+=dis[1];}
    33 int main()
    34 {    scanf("%lld",&n);
    35     for(int i=1;i<n;i++)
    36     {    ll u,v;
    37         scanf("%lld%lld",&u,&v);
    38         add(u,v);add(v,u);
    39         }
    40     memset(f,0,sizeof(f));
    41     precalc(1,-1);//预处理每个节点的子节点的个数和每个子树的距离 
    42     dfs(1,-1);
    43     for_each(f+1,f+n+1,ad);//相当于f[i]+=dis[1]; 
    44     cout<<(max_element(f+1,f+n+1)-f)<<endl;
    45     return 0;
    46     }
  • 相关阅读:
    SDUT 2128 树结构练习——排序二叉树的中序遍历
    SDUT 2137 数据结构实验之求二叉树后序遍历和层次遍历
    SDUT 3341 数据结构实验之二叉树二:遍历二叉树
    总结:串和数组的学习
    SDUT 3347 数据结构实验之数组三:快速转置
    SDUT 3348 数据结构实验之数组二:稀疏矩阵
    SDUT 1500 Message Flood
    SDUT 2463 学密码学一定得学程序
    SDUT 2125 数据结构实验之串二:字符串匹配
    mybatis学习(七)(choose的学习)
  • 原文地址:https://www.cnblogs.com/Slager-Z/p/7695939.html
Copyright © 2011-2022 走看看