AcWing 1073:树的中心 ← 树形DP

【题目来源】
https://www.acwing.com/problem/content/1075/

【题目描述】
给定一棵树,树中包含 n 个结点(编号1~n)和 n−1 条无向边,每条边都有一个权值。
请你在树中找到一个点,使得该点
到树中其他结点的最远距离最近

【输入格式】
第一行包含整数 n。
接下来 n−1 行,每行包含三个整数 ai,bi,ci,表示点 ai 和 bi 之间存在一条权值为 ci 的边。

【输出格式】
输出一个整数,表示所求点到树中其他结点的最远距离。

【数据范围】
1≤n≤10000,
1≤ai,bi≤n,
1≤ci≤10^5

【输入样例】

2 1 1 
3 2 1 
4 3 1 
5 1 1

【输出样例】
2

【算法分析】
● 树形 DP
(1)树形 DP 是建立在树上的 DP。
(2)树形 DP 常用的数组
d1[u]:从当前结点 u
向下走的最长路径长度
d2[u]:从当前结点 u
向下走的次长路径长度
up[u]:从当前结点 u 向上走的最长路径长度
p1[u]:从当前结点 u
向下走的最长路径是从哪个结点开始的
p2[u]:从当前结点 u
向下走的次长路径是从哪个结点开始的
(3)树是一种特殊的图,具有
无环的特点,这使得树形 DP 在很多问题上比普通 DP 更为直观高效。在树形 DP 问题中,通常需要处理与结点相关的状态,并利用树的层次结构,“向下走”或“向上走”递归地解决问题。“向下走”很容易,直接 DFS 即可。怎么“向上走”呢?其实,在本题中,“向上走”就是求某个结点 j 的父结点 u 不经过该结点 j 的最长路径长度。一般地,一个结点 j 的向上最长路径长度,就是它的父结点 u 的向上最长路径长度与向下最长路径长度的最大值。但是,如果父结点 u 的向下最长路径经过了结点 j,那么结点 j 的向上最长路径长度,就是它的父结点 u 的向上最长路径长度与向下次长路径长度的最大值。示意图如下所示:

代码如下所示:

if(p1[u]==j) up[j]=max(up[u],d2[u])+val[i];
else up[j]=max(up[u],d1[u])+val[i];

● 链式前向星:https://blog.csdn.net/hnjzsyjyj/article/details/139369904
val[idx]:存储编号为 idx 的边的
e[idx]:存储编号为 idx 的结点的
ne[idx]:存储编号为 idx 的结点指向的结点的编号
h[a]:存储头结点 a 指向的结点的编号

(1)加边操作
无权图的链式前向星的加边操作核心代码如下:

void add(int a,int b) {
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

有权图的链式前向星的加边操作核心代码如下: 

void add(int a,int b,int w) {
    val[idx]=w,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

(2)基于链式前向星的深度优先搜索(DFS)的核心代码

void dfs(int u) {
    cout<<u<<" ";
    st[u]=true;
    for(int i=h[u]; ~i; i=ne[i]) { //~i; equivalent to i!=-1;
        int j=e[i];
        if(!st[j]) {
            dfs(j);
        }
    }
}

(3)基于链式前向星的广度优先搜索(BFS)的核心代码

void bfs(int u) {
    queue<int>q;
    st[u]=true;
    q.push(u);
    while(!q.empty()) {
        int t=q.front();
        q.pop();
        cout<<t<<" ";
        for(int i=h[t]; ~i; i=ne[i]) { //~i; equivalent to i!=-1;
            int j=e[i];
            if(!st[j]) {
                q.push(j);
                st[j]=true; //need to be flagged immediately after being queued
            }
        }
    }
}


【算法代码】
下面代码来源于:
https://www.acwing.com/problem/content/video/1075/

/*
d1[u]:从当前结点 u 向下走的最长路径长度
d2[u]:从当前结点 u 向下走的次长路径长度
up[u]:从当前结点 u 向上走的最长路径长度
p1[u]:从当前结点 u 向下走的最长路径是从哪个结点开始的
p2[u]:从当前结点 u 向下走的次长路径是从哪个结点开始的
*/

#include <bits/stdc++.h>
using namespace std;

const int inf=0x3f3f3f3f;
const int N=1e4+5;
const int M=N<<1;
int val[M],e[M],ne[M],h[N],idx;
int d1[N],d2[N],up[N],p1[N],p2[N];
int n;

void add(int a,int b,int w) {
    val[idx]=w,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

int dfs_d(int u,int fa) { //go down
    d1[u]=d2[u]=-inf;
    for(int i=h[u]; i!=-1; i=ne[i]) {
        int j=e[i];
        if(j==fa) continue;
        int d=dfs_d(j,u)+val[i];
        if(d>=d1[u]) {
            d2[u]=d1[u],d1[u]=d;
            p2[u]=p1[u],p1[u]=j;
        } else if(d>d2[u]) d2[u]=d,p2[u]=j;

    }
    if(d1[u]==-inf) d1[u]=d2[u]=0;
    return d1[u];
}

void dfs_u(int u,int fa) { //go up
    for(int i=h[u]; i!=-1; i=ne[i]) {
        int j=e[i];
        if(j==fa) continue;
        if(p1[u]==j) up[j]=max(up[u],d2[u])+val[i];
        else up[j]=max(up[u],d1[u])+val[i];
        dfs_u(j,u);
    }
}

int main() {
    cin>>n;
    memset(h,-1,sizeof(h));
    for(int i=0; i<n-1; i++) {
        int a,b,c;
        cin>>a>>b>>c;
        add(a,b,c),add(b,a,c);
    }

    dfs_d(1,-1);
    dfs_u(1,-1);

    int ans=inf;
    for(int i=1; i<=n; i++) {
        ans=min(ans,max(d1[i],up[i]));
    }
    cout<<ans;

    return 0;
}

/*
in:
5
2 1 1
3 2 1
4 3 1
5 1 1

out:
2
*/




【参考文献】
https://blog.csdn.net/m0_63997099/article/details/137436898
https://zhuanlan.zhihu.com/p/657767879
https://blog.csdn.net/qq_57150526/article/details/128521583
https://www.acwing.com/solution/content/6825/
https://www.acwing.com/problem/content/1077/
https://www.acwing.com/solution/content/163892/
https://www.cnblogs.com/ljy-endl/p/11612275.html




 

  • 41
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值