http://acm.hdu.edu.cn/showproblem.php?pid=2196
给定一个网络,是一个图,求每个节点到最远节点的距离。。
1 树的直径。
即树上的最长简单路(最长的一条线)。然后求出直径的两个端点,
在计算的时候记忆化一下,保存一下他们距离 端点的距离。然后取最远的。(
1 先求出距离一个端点的距离,用直径直接减去是不行的,,因为有的点并不在树的直径上。。
)
2 树形dp,
观察发现,最长的路,只有两种情况,①叶子节点,②父节点,
然后保留当前点为 开始,最长的距离和 次长的距离。
为什么要保留两个呢(因为如果两个邻接的点,他们的最长路径是相对的,就无法保存,会把他们之间的路径重复计算。)
① 是树的直径。
②是 两种状态。
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
/* 这一道题之所以叫 树形dp 而不叫树形ap 或者树形adc
其实是因为 他灵活的运用了后根 遍历和 前序遍历
来达到转移状态的 思路。
为啥用两种状态啊,你想啊,如果邻接的两个点,他们的状态
是相向的,那么他们不就计算错误了么。
*/
const int maxn=2e5;
int smax_siz[maxn];//次长的距离
int max_siz[maxn];// 最长的距离叶子的距离
int vec1[maxn];//最长的距离叶子的距离的方向(总共有两种,不是爸爸就是儿子)
int vec2[maxn];
struct Node
{ int to,next,w;
}node[maxn];
int len;
int head[maxn];
void add(int u,int v,int w){
node[len].to=v;
node[len].next=head[u];
node[len].w=w;
head[u]=len++;
}
int dfs1(int u,int pre){
max_siz[u]=0;
smax_siz[u]=0;
for(int i=head[u];i!=-1;i=node[i].next){
int to=node[i].to;
int w=node[i].w;
if(to==pre) continue;
dfs1(to,u);
if(max_siz[to]+w>smax_siz[u]){
smax_siz[u]=max_siz[to]+w;
vec2[u]=to;
if(smax_siz[u]>max_siz[u]){
swap(vec2[u],vec1[u]);//叶子节点方向
swap(smax_siz[u],max_siz[u]);
}
}
}
//cout<<u<<" "<<max_siz[u]<<" "<<max_siz[u]<<endl;
return 0;
}
int dfs2(int u,int pre){
for(int i=head[u];i!=-1;i=node[i].next){
int to=node[i].to;
int w=node[i].w;
if(to==pre) continue;
if(vec1[u]==to){
if(smax_siz[u]+w>smax_siz[to]){
smax_siz[to]=smax_siz[u]+w;
vec2[to]=u;
if(smax_siz[to]>max_siz[to]){
swap(smax_siz[to],max_siz[to]);
swap(vec1[to],vec2[to]);
}
}
}
else{
if(max_siz[u]+w>smax_siz[to]){
smax_siz[to]=max_siz[u]+w;
vec2[to]=u;
if(smax_siz[to]>max_siz[to]){
swap(smax_siz[to],max_siz[to]);
swap(vec1[to],vec2[to]);
}
}
}
dfs2(to,u);
}
}
void init(){
len=0;
memset(head,-1,sizeof(head));
}
int main()
{ int t,a,b;
while(~scanf("%d",&t)){
init();
for(int i=2;i<=t;i++){
scanf("%d%d",&a,&b);
add(a,i,b);
add(i,a,b);
}
dfs1(1,-1);
dfs2(1,-1);
for(int i=1;i<=t;i++){
printf("%d\n",max_siz[i]);
}
}
return 0;
}
#include <iostream>
#include <bits/stdc++.h>
/* 树形dp经典,具体做法是先求树的直径,然后确定两点。
然后树形dp的时候保留一个最大的子树。
1 开始以为找到一个点,然后用直径减去 距离一个点的距离,就是距离,但是其实有的点不在 树的直径啊。
*/
const int maxn=2e5+1000;
using namespace std;
int siz[maxn][2];
int ans[maxn];
// 求 树直的时候可以保留两个siz。最后找一个最大的qwq
struct Node
{ int next;
int to,w;
}node[maxn];
int len;
int head[maxn];
void add(int u,int v,int w){
node[len].to=v;
node[len].w=w;
node[len].next=head[u];
head[u]=len++;
}
int end1,first1;
int dfs1(int u,int pre,int len){
siz[u][0]=len;
if(siz[u][1]>siz[u][0]) swap(siz[u][1],siz[u][0]);
for(int i=head[u];i!=-1;i=node[i].next){
int to=node[i].to;
int w=node[i].w;
if(to==pre) continue;
dfs1(to,u,len+w);
}
return 0;
}
int dfs2(int u,int pre,int len){
siz[u][1]=len;
for(int i=head[u];i!=-1;i=node[i].next){
int to=node[i].to;
int w=node[i].w;
if(to==pre) continue;
dfs2(to,u,len+w);
}
return 0;
}
/*
void dfs3(int ss,int u,int pre,int len){
//cout<<end1<<endl;
if(u==end1||u==first1){
ans[ss]=max(ans[ss],len);;
// 本来想确定完 直径的两个点,然后再暴力找,但是这样妥妥超市啊
}
for(int i=head[u];i!=-1;i=node[i].next){
int to=node[i].to;
int w=node[i].w;
if(to==pre) continue;
//cout<<u<<"jingguo"<<to<<endl;
dfs3(ss,to,u,w+len);
}
}*/
void init(){
len=0;
memset(head,-1,sizeof(head));
memset(siz,0,sizeof(siz));
memset(ans,0,sizeof(ans));
}
int main()
{ int t,a,b;
while(~scanf("%d",&t)){
init();
for(int i=2;i<=t;i++){
scanf("%d%d",&a,&b);
add(i,a,b);
add(a,i,b);
}
dfs1(1,1,0);
end1=-1;
int max1=-1;
for(int i=1;i<=t;i++){
if(siz[i][0]>max1)
{ max1=siz[i][0];
end1=i;}
}
dfs2(end1,end1,0);
int all=-1;
int first1=0;
for(int i=1;i<=t;i++){
if(siz[i][1]>all){
all=siz[i][1];
first1=i;
}
}
dfs1(first1,first1,0);// 根据end1 查找另一个点,叫first1.
for(int i=1;i<=t;i++){
printf("%d\n",siz[i][0]);
}
}
return 0;
}