hdu 2196 computer 求树上的任意最远点对 O(n)

题意:

给定n个结点,他们之间用n-1条边链接(这一点说明这个图的形状 就是一棵树 无环),给你一个结点,距离此节点最远的点与这个节点之间的距离。

解题思路:

经典的树上最长点对问题。不过带权,但是解决方法没有区别

首先找任意一个点,dfs()求出距离这个点的最远点END1 O(n)

然后从END1出发 再次dfs() 求出距离END1的最远点 期间经过每一个结点时,更新dist[i] (这个状态表示距离结点i的最远点到它的距离) 然后求出END2

从END2 再次出发 再遍历一遍所有结点 再次更新dist[i];

明确一个性质:可以证明 对于任意一个节点 距离它最远的结点 一定是END1 或 END2 。

code:

#include<cstdio>
#include<cstring>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
const int maxn = 10005;

vector<int> g[maxn];
vector<int> cost[maxn];
int dist[maxn];
int END,max_;
int n;

void init(){
    for(int i = 0; i <= maxn; i++){
        g[i].clear();
        cost[i].clear();
    }
    for(int i = 2; i <= n; i++){
        int u,w;
        scanf("%d%d",&u, &w);
        g[i].push_back(u);
        cost[i].push_back(w);
        g[u].push_back(i);
        cost[u].push_back(w);
    }
}
void dfs(int u, int fa, int len){
    dist[u] = max(dist[u], len);
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        int w = cost[u][i];
        if(v == fa) continue;
        dfs(v, u, len + w);
    }
    if(len >= max_){
        END = u;
        max_ = len;
    }
}
void solve(){
    memset(dist, 0, sizeof(dist));
    max_ = 0;
    dfs(1, -1, 0);
    max_ = 0;
    dfs(END, -1, 0);
    max_ = 0;
    dfs(END, -1, 0);

    for(int i = 1; i <= n; i++){
        printf("%d\n",dist[i]);
    }
}
int main(){
    while(scanf("%d",&n) != EOF){
        init();
        solve();
    }
    return 0;
}

一开始松弛操作的部分,忘记每次更新max_值。。好在很快发现 水...

后来交上题目,返回RE....简直无情

卧槽我居然傻逼手动扩栈。。。扩栈。。。栈。。简直无情 当然是毫无悬念的MT了...

实际上是我没有每次清空vector 。 忘了爱

时间: 2024-08-28 22:54:50

hdu 2196 computer 求树上的任意最远点对 O(n)的相关文章

HDU 2196 Computer 经典树形DP

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

HDU 2196——Computer(树形DP)

膜拜了NN个大神的代码,看了一整天,弱菜伤不起啊.求拜师啊 问题分析:求树上每个节点到其它节点的最远距离 每个节点到其它节点的最远距离就是以该节点为根的树所能达到的最大深度,这样子的话,要把每个节点转化为根,总共dfs的次数为节点数,肯定超时 于是~ 一个节点的最长路:1.从该节点往下取得最长路(子树部分)  2.从该节点往上取得的最长路(父节点往上的部分) 情况1:自下而上的dfs(先深搜后操作) 情况2:自上而下的dfs(先操作后深搜){ 如果节点u的子节点v在 以u为父亲的子树上的最长路,

HDU 2196 Computer 二次扫描与换根DP

题意:给定一棵树,求树上所有点到其最远点的距离. 数据范围: 1 <= N <= 100000 ------------------------------------------我是分割线------------------------------------------ 题解:对于每个节点u来说,其可能到达的最长距离为max{其子树内的最长距离,其父节点不经过u的子树内的最长距离}.于是,我们便可以在第一遍dfs中预处理节点x到其子树内的最长距离,顺带求一下次长距离,方便转移. // f[

HDU 2196 Computer(求树上每个节点到其它点的最远距离)

解题思路: 求出树的直径的两个端点,则树上每个节点到其他点的最远距离一定是到这两个端点的距离中最长的那一个. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <vector> #include <queue> #define LL long long using nam

HDU 2196 Computer(树形DP求最长路)

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 a

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[i][0] : 表示以i为根的子树中的结点与i的最大距离 dp[i][1] : 表示以i为根的子树中的结点与u的次大距离 dp[i][2] : 表示i往父亲节点方向走的最大距离 第一就是点 i 在以点 i 为根的子树中的最长距离,这个可以直接在点 i 的子树中求得: 第二就是点 i 朝父亲节点方向的最长距离,这个距离分为三种: 1) 点 i 在以 fa

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) 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