zoukankan      html  css  js  c++  java
  • 洛谷P3478 [POI2008]STA-Station

    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
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #define maxn 1000010
    using namespace std;
    int n,num,head[maxn],id,dep[maxn],mx;
    struct node{
        int to,pre;
    }e[maxn*2];
    void Insert(int from,int to){
        e[++num].to=to;
        e[num].pre=head[from];
        head[from]=num;
    }
    void dfs(int now,int father){
        dep[now]=dep[father]+1;
        if(dep[now]>mx||(dep[now]==mx&&id>now)){
            mx=dep[now];
            id=now;
        }
        for(int i=head[now];i;i=e[i].pre){
            int to=e[i].to;
            if(to==father)continue;
            dfs(to,now);
        }
    }
    int main(){
        scanf("%d",&n);
        int x,y;
        for(int i=1;i<n;i++){
            scanf("%d%d",&x,&y);
            Insert(x,y);Insert(y,x);
        }
        dfs(1,1);
        cout<<id;
    }
    70分 找树上最长链的一个端点
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define maxn 1000010
    using namespace std;
    int n,num,head[maxn],sz[maxn],fa[maxn],dep[maxn];
    long long f[maxn],ans;
    struct node{int to,pre;}e[maxn*2];
    void Insert(int from,int to){
        e[++num].to=to;
        e[num].pre=head[from];
        head[from]=num;
    }
    void dfs(int now,int father){
        dep[now]=dep[father]+1;
        sz[now]=1;
        for(int i=head[now];i;i=e[i].pre){
            int to=e[i].to;
            if(to==father)continue;
            dfs(to,now);
            sz[now]+=sz[to];
        }
    }
    void find(int now){
        for(int i=head[now];i;i=e[i].pre){
            int to=e[i].to;
            if(dep[to]>dep[now]){
                f[to]=f[now]+n-2*sz[to];
                find(to);
            }
        }
    }
    int main(){
        scanf("%d",&n);
        int x,y;
        for(int i=1;i<n;i++){
            scanf("%d%d",&x,&y);
            Insert(x,y);Insert(y,x);
        }
        dfs(1,0);
        for(int i=1;i<=n;i++)f[1]+=dep[i];
        find(1);
        int mark=0;
        for(int i=1;i<=n;i++)
            if(ans<f[i]){
                ans=f[i];
                mark=i;
            }
        cout<<mark;
        return 0;
    }
    100分 树形dp
  • 相关阅读:
    文件分段后,进行分片上传逻辑
    总结几个最近处理问题中使用http协议的代码
    openresty(nginx)中使用lua脚本获取请求IP地址的代码
    线上Storm的worker,executor,task参数调优篇
    async/await
    DataTables.Queryable Sample
    关闭 XXXXX 前你必须关闭所有会话框
    关于P/Invoke的闲话
    Windows 2008 Scheduled tasks result codes
    MySQL 8.0.13的使用心得
  • 原文地址:https://www.cnblogs.com/thmyl/p/7805819.html
Copyright © 2011-2022 走看看