hdu 2196 叶子节点最长距离(树DP)
http://www.cnblogs.com/kuangbin/archive/2012/08/28/2659915.html
求每个节点到叶子节点的最长距离
需要保存每个节点到叶子节点距离的最大值和次大值。第一次dfs求出到下面叶子节点的最大值,第二次dfs更新从父节点过来的最大值。
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 20000+5
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f #define ls (rt<<1)
#define rs (rt<<1|1) int n,m; int ptr = ,head[MAXN],a[MAXN],dp[MAXN][],vis[MAXN]; int mx[MAXN],mx2[MAXN],vx[MAXN],vx2[MAXN]; struct node
{
int y,val,next;
}tree[MAXN]; void add(int fa,int son,int val)
{
tree[ptr].y = son;
tree[ptr].val = val;
tree[ptr].next = head[fa];
head[fa] = ptr++;
} void cg(int &x,int &y)
{
int tmp = x;
x = y;
y = tmp;
} void dfs(int root,int fa)
{
for(int i=head[root];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(y == fa) continue;
dfs(y,root);
if(mx2[root] < mx[y]+tree[i].val)
{
mx2[root] = mx[y]+tree[i].val;
vx2[root] = y;
if(mx2[root]>mx[root])
{
swap(mx[root],mx2[root]);
swap(vx[root],vx2[root]);
}
}
}
} void dfs2(int root,int fa)
{
for(int i=head[root];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(y == fa) continue;
if(y == vx[root])
{
if(tree[i].val + mx2[root] > mx2[y])
{
mx2[y] = tree[i].val + mx2[root];
vx2[y] = root;
if(mx2[y]>mx[y])
{
swap(mx[y],mx2[y]);
swap(vx[y],vx2[y]);
}
}
}
else
{
if(tree[i].val + mx[root] > mx2[y])
{
mx2[y] = tree[i].val + mx[root];
vx2[y] = root;
if(mx2[y]>mx[y])
{
swap(mx[y],mx2[y]);
swap(vx[y],vx2[y]);
}
}
}
dfs2(y,root);
}
} int main()
{
int i,j,t,kase=;
while(~sf("%d",&n))
{
mem(tree,);
mem(head,-);
mem(mx,);
mem(mx2,);
mem(vx,);
mem(vx2,);
ptr = ;
int x,y;
for(i=;i<=n;i++)
{
sf("%d%d",&x,&y);
add(x,i,y);
add(i,x,y);
}
dfs(,-);
dfs2(,-);
for(i=;i<=n;i++) pf("%d\n",mx[i]);
}
return ;
}
hdu 2196 叶子节点最长距离(树DP)的更多相关文章
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- hdu 2196(方法1:经典树形DP+方法2:树的直径)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 5773 The All-purpose Zero 线段树 dp
The All-purpose Zero 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 Description ?? gets an seq ...
- HDU 4719Oh My Holy FFF 线段树+DP
/* ** 日期: 2013-9-12 ** 题目大意:有n个数,划分为多个部分,假设M份,每份不能多于L个.每个数有一个h[i], ** 每份最右边的那个数要大于前一份最右边的那个数.设每份最右边的 ...
- poj 3162 树DP+单调队列
http://acm.hust.edu.cn/vjudge/problem/11552 http://blog.csdn.net/woshi250hua/article/details/7727677 ...
- HDU 2196树形DP(2个方向)
HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...
- POJ 2342 Anniversary party (树dp)
题目链接:http://poj.org/problem?id=2342 有n个人,每个人有活跃值.下面n-1行u和v表示u的上司是v,有直接上司和下属的关系不能同时参加party,问你party最大的 ...
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- HDU 4267 线段树 离散点区间更新, 自叶子节点至根单点查询
题意: n个数字 下面n个数字表示数列 2个操作 1 [u, v] k add [u,v ]区间 (u点要计算)每隔k个位置,该数字+add 2 pos 询问 pos下标的值(下标从1开始) 思路 ...
随机推荐
- 【python】Python的logging模块封装
#!/usr/local/python/bin # coding=utf-8 '''Implements a simple log library. This module is a simple e ...
- Python3异常处理
一. 简介 在编程过程中为了增加友好性,在程序出现Bug时一般不会直接将错误信息展示给用户,而是提供一个友好的输出提示. 二. 使用 1. 异常基础 常用结构: try: pass except Ex ...
- Dancing Links算法(舞蹈链)
原文链接:跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题 作者:万仓一黍 出处:http://grenet.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但 ...
- ui-grid使用详解
HTML <pre name="code" class="html"><!--ui-grid css--> <link rel=& ...
- HDU6311 Cover (欧拉路径->无向图有最少用多少条边不重复的路径可以覆盖一个张无向图)
题意:有最少用多少条边不重复的路径可以覆盖一个张无向图 ,输出每条路径的边的序号 , 如果是反向就输出-id. 也就是可以多少次一笔画的方式画完这个无向图. 题解:我们已知最优胜的情况是整个图是欧拉图 ...
- BZOJ - 1941 最远估价
题意:求给出的所有点的最远点减最近点的最小差值 KD树的最远估价和最近估价略微不同,直接找最远垂线,反正xjb改一下就过了 #include<bits/stdc++.h> #define ...
- 03-树2 List Leaves (25 分)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- Oracle 11g 记录DML错误数据
[From] https://oracle-base.com/articles/10g/dml-error-logging-10gr2 需要记录下大量DML操作中出错的具体record,看到有两种方案 ...
- PIE SDK常用滤波
1. 算法功能简介 空间域滤波实在图像空间( x. y)对输入图像应用滤波函数(核.模板)来改进输出图像的处理方法,主要包括平滑和锐化处理,强调像素与其周围相邻像素的关系,常用的方法是卷积运算. 空间 ...
- python练习七十:图片生成
题目:使用python生成类似于下图的字母验证码图片 实现代码: from PIL import Image,ImageFont,ImageDraw,ImageFilter import random ...