树的中心(树形dp)

博客探讨了如何使用树形动态规划(DP)方法寻找树中最大距离最小的点。思路涉及考虑节点u的下层最远距离和上层最远距离,以及在向上遍历时处理已访问子节点的策略,通过p1和p2数组记录最大和次大距离路径。
摘要由CSDN通过智能技术生成

在这里插入图片描述

思路:要找一个最大距离最小的点,那么有2个方向1.这u点下面最远距离2.这个u点上面的最远距离,这里向上走有2种走法,一直是直接求u点的子节点没有访问过求向上和向下的最大值,访问过则求向上最大值和向下次大值的最大值,这里需要记录一下路径用p1[i]记录最大距离从i经过的话是由那个点过来的,p2[i]记录次大。

#pragma GCC optimize(2)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<iomanip>
#include<cstring>
#include<time.h>

using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=2e6+10;
const int M=1e3+10;
const int inf=0x3f3f3f3f;
const int maxx=2e5+7;
const double eps=1e-6;
int gcd(int a,int b)
{
   
    return b==0?a:gcd(b,a%b)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值