zoukankan      html  css  js  c++  java
  • HDU 2196 Computer

    Computer

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2196
    64-bit integer IO format: %I64d      Java class name: Main
    A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information. 


    Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
     

    Input

    Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.
     

    Output

    For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).
     

    Sample Input

    5
    1 1
    2 1
    3 1
    1 1

    Sample Output

    3
    2
    3
    4
    4

    解题:树形dp
    第一次dfs只能求出每个点最长的儿子和次长的儿子路径。。。第二次dfs就能算出各点在整个树中的最长路和次长路
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 11000;
     4 struct arc{
     5     int to,w,next;
     6     arc(int x = 0,int y = 0,int z = -1){
     7         to = x;
     8         w = y;
     9         next = z;
    10     }
    11 }e[maxn<<1];
    12 int head[maxn],dp[maxn][2],tot,n;
    13 void add(int u,int v,int w){
    14     e[tot] = arc(v,w,head[u]);
    15     head[u] = tot++;
    16 }
    17 void dfs(int u,int fa){
    18     dp[u][1] = dp[u][0] = 0;
    19     for(int i = head[u]; ~i; i = e[i].next){
    20         if(e[i].to == fa) continue;
    21         dfs(e[i].to,u);
    22         if(dp[u][1] < dp[e[i].to][1] + e[i].w){
    23             dp[u][0] = dp[u][1];
    24             dp[u][1] = dp[e[i].to][1] + e[i].w;
    25         }else if(dp[u][0] < dp[e[i].to][1] + e[i].w)
    26             dp[u][0] = dp[e[i].to][1] + e[i].w;
    27     }
    28 }
    29 void dfs2(int u,int fa){
    30     for(int i = head[u]; ~i; i = e[i].next){
    31         if(e[i].to == fa) continue;
    32         if(dp[e[i].to][1] + e[i].w == dp[u][1]){
    33             if(dp[e[i].to][1] < dp[u][0] + e[i].w){
    34                 dp[e[i].to][0] = dp[e[i].to][1];
    35                 dp[e[i].to][1] = dp[u][0] + e[i].w;
    36             }else if(dp[e[i].to][0] < dp[u][0] + e[i].w)
    37                 dp[e[i].to][0] = dp[u][0] + e[i].w;
    38         }else if(dp[e[i].to][1] + e[i].w == dp[u][0]){
    39             if(dp[e[i].to][1] < dp[u][1] + e[i].w){
    40                 dp[e[i].to][0] = dp[e[i].to][1];
    41                 dp[e[i].to][1] = dp[u][1] + e[i].w;
    42             }else if(dp[e[i].to][0] < dp[u][1] + e[i].w)
    43                 dp[e[i].to][0] = dp[u][1] + e[i].w;
    44         }else{
    45             if(dp[e[i].to][1] < dp[u][1] + e[i].w){
    46                 dp[e[i].to][0] = dp[e[i].to][1];
    47                 dp[e[i].to][1] = dp[u][1] + e[i].w;
    48             }else if(dp[e[i].to][0] < dp[u][1] + e[i].w)
    49                 dp[e[i].to][0] = dp[u][1] + e[i].w;
    50         }
    51         dfs2(e[i].to,u);
    52     }
    53 }
    54 int main(){
    55     int v,w;
    56     while(~scanf("%d",&n)){
    57         memset(head,-1,sizeof(head));
    58         tot = 0;
    59         for(int i = 2; i <= n; ++i){
    60             scanf("%d %d",&v,&w);
    61             add(i,v,w);
    62             add(v,i,w);
    63         }
    64         dfs(1,-1);
    65         dfs2(1,-1);
    66         for(int i = 1; i <= n; ++i)
    67             printf("%d
    ",max(dp[i][0],dp[i][1]));
    68     }
    69     return 0;
    70 }
    View Code

    第二种方法,bfs求树的直径,只需要三次bfs。。。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <queue>
     6 using namespace std;
     7 const int INF = 0x3f3f3f3f;
     8 const int maxn = 10010;
     9 struct arc{
    10     int to,w,next;
    11     arc(int x = 0,int y = 0,int z = -1){
    12         to = x;
    13         w = y;
    14         next = z;
    15     }
    16 }e[1000100];
    17 queue<int>q;
    18 int d[2][maxn],head[maxn],tot,n;
    19 void add(int u,int v,int w){
    20     e[tot] = arc(v,w,head[u]);
    21     head[u] = tot++;
    22 }
    23 int bfs(int u,int idx) {
    24     while(!q.empty()) q.pop();
    25     q.push(u);
    26     memset(d[idx],-1,sizeof d[idx]);
    27     d[idx][u] = 0;
    28     while(!q.empty()) {
    29         int u = q.front();
    30         q.pop();
    31         for(int i = head[u]; ~i; i = e[i].next) {
    32             if(d[idx][e[i].to] == -1) {
    33                 d[idx][e[i].to] = d[idx][u] + e[i].w;
    34                 q.push(e[i].to);
    35             }
    36         }
    37     }
    38     int id = 0,ret = -1;
    39     for(int i = 1; i <= n; ++i)
    40         if(ret < d[idx][i]) ret = d[idx][id = i];
    41     return id;
    42 }
    43 int main() {
    44     int u,v,w;
    45     while(~scanf("%d",&n)) {
    46         memset(head,-1,sizeof head);
    47         for(int i = 2; i <= n; ++i) {
    48             scanf("%d%d",&v,&w);
    49             add(i,v,w);
    50             add(v,i,w);
    51         }
    52         int a = bfs(1,0);
    53         int b = bfs(a,0);
    54         bfs(b,1);
    55         for(int i = 1; i <= n; ++i)
    56             printf("%d
    ",max(d[0][i],d[1][i]));
    57     }
    58     return 0;
    59 }
    View Code
  • 相关阅读:
    ESP8266 wifi钓鱼
    九,ESP8266 判断是断电上电(强制硬件复位)之后运行的内部程序还是内部软件复位之后运行的程序(基于Lua脚本语言)
    关于Http 传输二维json
    Android6.0权限大全和权限分类
    Android 多线程-----AsyncTask详解
    关于加密(转载文章)
    java基础之:匿名内部类
    系统吞吐量(TPS)、用户并发量、性能测试概念和公式
    java多线程之:SynchronousQueue队列
    Hibernate之:各种主键生成策略与配置详解
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4402357.html
Copyright © 2011-2022 走看看