zoukankan      html  css  js  c++  java
  • SPOJ:Eagle and Dogs(求树上每个点最远可以走到哪里---树的直径||DP)

    Eagle (AKA Mohamed Ahmed) lives in a city consists of n intersections connected by n-1 roads, in a way that can go from any intersection to any other intersection moving along some of these roads.

    Every day he starts walking in the city following a simple strategy; if he's at some intersection he has to pick one of the roads connected to it at random such that he hasn't walked through it before and walk through it and and if there isn't any, he stops and goes home.

    His only problem is that he's afraid of dogs. He doesn't even like seeing dogs. So he's wondering in the worst scenario, how many dogs he'll have to see during his walk until he stops if he starts walking at some intersection. Can you help him?

    Input

    The input starts with an integer T (1 <= T <= 10), the number of test cases. following T blocks describing each test case.

    Each block starts with a line containing an integer n (2 <= n <= 105), the number of intersections in the city. Intersections are numbers 1 through n.

    Followed by n-1 lines each containing integers u, v, (1 <= u, v <= n) and d (1 <= d <= 109), the numbers of intersections at the end of this road and the number od dogs Eagle will see walking in this road.

    Output

    For each test case print a line containing n integers, the ith integer represents the maximum number of dogs Eagle might see if he starts his walk at intersection i.

    Example

    Input:
    1
    4
    1 2 3
    3 2 4
    3 4 5
    Output:
    12 9 7 12

    题意:问树上每个点最远可以走到哪里,不能回走。

    结论:先走树的直径,那么最远路的终点一定是直径的端点,所以从树的直径的端点dfs两次得到距离,较大的一个就是最远距离。

     (不过我队友用DP过了此题,ORZ,后面附图。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=200010;
    int Laxt[maxn],Next[maxn<<1],To[maxn<<1],cost[maxn<<1],cnt,S,T;
    long long ans[maxn],dis[maxn];
    void read(int &x){
        x=0; char c=getchar();
        while(c>'9'||c<'0') c=getchar();
        while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();
    }
    void add(int u,int v,int d)
    {
        Next[++cnt]=Laxt[u];
        Laxt[u]=cnt;
        To[cnt]=v;
        cost[cnt]=d;
    }
    void dfs(int u,int fa)
    {
        for(int i=Laxt[u];i;i=Next[i]){
            if(To[i]!=fa){
                dis[To[i]]=dis[u]+cost[i];
                dfs(To[i],u);
            }
        }
    }
    int main()
    {
        int Case,N,u,v,d,i,j;
        scanf("%d",&Case);
        while(Case--){
            scanf("%d",&N); cnt=0; S=T=0;
            for(i=1;i<=N;i++) ans[i]=Laxt[i]=0;
            for(i=1;i<N;i++){
                read(u); read(v); read(d);
                add(u,v,d); add(v,u,d);
            }
            dis[1]=0; dfs(1,0);
            for(i=1;i<=N;i++) if(dis[i]>dis[S]) S=i;
            dis[S]=0; dfs(S,0);
            for(i=1;i<=N;i++) {
                if(dis[i]>dis[T]) T=i;
                if(dis[i]>ans[i]) ans[i]=dis[i];
            }
            dis[T]=0; dfs(T,0);
            for(i=1;i<=N;i++) 
               if(dis[i]>ans[i]) ans[i]=dis[i];
            for(i=1;i<N;i++) printf("%lld ",ans[i]);
            printf("%lld
    ",ans[N]);
        }
        return 0;
    }

     

  • 相关阅读:
    [Linux] 设置系统时区
    [Python] 当猎头遇上 Guido van Rossum
    [Ubuntu] LightDM 轻量级桌面显示管理器
    [Java] Apache Ant 构建基础教程
    [Python] pip 简明指南
    .NET Core下的Spring Cloud——前言和概述
    福州首届.NET开源社区技术交流会圆满成功
    【福州活动】| "福州首届.NET开源社区线下技术交流会"(2018.11.10)
    使用CoreRT将.NET Core发布为Native应用程序
    使用.NET Core快速开发一个较正规的命令行应用程序
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9011084.html
Copyright © 2011-2022 走看看