hdu 2196 Computer 树的直径

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description

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

Author

scnu

题意:给你一棵树,求每个点到树上的最远距离;

思路:根据树的直径原理,每个点的最远到为直径的某个端点;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+10,M=1e6+10,inf=1e9+10;
const ll INF=1e18+10,MOD=2147493647;
struct is
{
    int v,w,nex;
}edge[N];
int head[N],edg;
int dis[N];
int deep,node1,node2;
void init()
{
    memset(head,-1,sizeof(head));
    memset(dis,0,sizeof(dis));
    edg=0;
    deep=0;
}
void add(int u,int v,int w)
{
    edg++;
    edge[edg].v=v;
    edge[edg].w=w;
    edge[edg].nex=head[u];
    head[u]=edg;
}

void dfs(int u,int fa,int val,int &node)
{
    dis[u]=max(dis[u],val);
    if(val>deep)
    {
        deep=val;
        node=u;
    }
    for(int i=head[u];i!=-1;i=edge[i].nex)
    {
        int v=edge[i].v;
        int w=edge[i].w;
        if(v==fa)continue;
        dfs(v,u,val+w,node);
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        init();
        for(int i=2;i<=n;i++)
        {
            int v,w;
            scanf("%d%d",&v,&w);
            add(i,v,w);
            add(v,i,w);
        }
        dfs(1,-1,0,node1);
        deep=0;
        dfs(node1,-1,0,node2);
        deep=0;
        dfs(node2,-1,0,node1);
        for(int i=1;i<=n;i++)
            printf("%d\n",dis[i]);
    }
    return 0;
}
时间: 2024-12-18 18:35:14

hdu 2196 Computer 树的直径的相关文章

HDOJ 2196 Computer 树的直径

由树的直径定义可得,树上任意一点到树的直径上的两个端点之一的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3522    Accepted Submission(s): 1784 Problem Description A school bought

hdu 2196 computer 树状dp

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3731    Accepted Submission(s): 1886 Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du

HDU 2196 Computer 经典树形DP

一开始看错题了,后来发现原来是在一颗带权的树上面求出距离每一个点的最长距离,做两次dfs就好,具体的看注释? #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #incl

hdu2196 Computer(树的直径||树中的最长路径)

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5232    Accepted Submission(s): 2640 Problem Description A school bought the first computer some time ago(so this computer's id is 1). D

HDU 4123(树的直径+单调队列)

Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2833    Accepted Submission(s): 917 Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble i

HDU - 2196 Computer 经典树型DP

题目大意:给出一棵树,每条边都有一个权值,要求求出从每个点出发的最大权值和 解题思路:无根树先转化成有根树,以1为根 先dfs一次,求出以某个点为根,通过子节点所能到得到的最大权值和,和次大权值和,并纪录求得最大权值和的那个子节点 这样我们只考虑了通过子节点得到最大权值和的情况,但要求最大权值和,当然还要判断通过父节点得到最大权值和是否会大于通过子节点得到最大权值和,怎么判断呢? 设dp[i]为i节点通过父节点得到的最大权值和 那么分两种情况讨论 1.子节点是求得最大权值和的那个节点 那么dp[

HDU 2196 Computer 树形DP经典题

链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问每台电脑和其它电脑的最远距离是多少. 思路:这是一道树形DP的经典题目.须要两次DFS,第一次DFS找到树上全部的节点在不同子树中的最远距离和次远的距离(在递归中进行动态规划就可以),第二次DFS从根向下更新出终于答案.对于每次更新到的节点u,他的最远距离可能是来自u的子树,或者是u的父亲节点的最远

HDU 2196 Computer( 树上节点的最远距离 )

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4080    Accepted Submission(s): 2043 Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du

HDU 2196 Computer 树形DP 经典题

给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权,放在数组cost中 令tree(i)表示以节点i为根的子树 对于节点i,离该节点最远的点要不就是在tree(i)中,要不就是在father(i)上面 令: dp[i][1] : 在子树tree(i)中,离i最远的距离 dp[i][2] : 在子树tree(i)中,离i第二远的距离 (递推的时候需要)