当前位置:网站首页>图论(树的直径)
图论(树的直径)
2022-06-23 22:05:00 【Dαīsч】
一、基础
树中最远的两个结点之间的距离被称为树的直径,连接这两点的路径被称为树的最长链(也称直径)
二、两次遍历求直径
1、从任意一个结点出发,通过bfs或者dfs对树进行一次遍历,求出与出发点距离最远的结点,记为p,这就是树的直径的一端
2、从结点p出发,通过bfs和dfs对树再进行一次遍历,求出与p距离最远的结点,记为q
3、那么p到q的路径就是树的一条直径,因为p一定是直径的一端,否则总能找到一条更长的链。在第二步遍历过程中,可以记录下来每个点第一次被访问时的前驱节点,最后从q回溯到p即可得到直径的具体方案
4、注:通过搜索不能解决负边权的问题
int to[maxn << 1], nex[maxn << 1], edge[maxn << 1], head[maxn];
int cnt, n, m, r;
ll dis[maxn], maxd, maxv;
void add(int u, int v, int w)
{
to[++cnt] = v;
edge[cnt] = w;
nex[cnt] = head[u];
head[u] = cnt;
}
void dfs(int u, int fa)
{
if (maxd < dis[u])
{
maxd = dis[u];
maxv = u;
}
for (int i = head[u]; i; i = nex[i])
{
int v = to[i];
if (fa == v)
continue;
dis[v] = dis[u] + edge[i];
dfs(v, u);
}
}
void solve()
{
cin >> n;
for (int i = 0; i < n - 1; i++)
{
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
dfs(1, 0);
maxd = 0;
dis[maxv] = 0;
dfs(maxv, 0);
cout << maxd;
}三、树形dp求直径
1、设d[x]表示从结点x出发走向以x为根的子树,能够到达的最远节点的距离,即x向下走的最长链,显然有
![d[x]=\underset {1\leq i \leq t}{max}\{d[y_i]+w(x,y_i)]\}](http://img.inotgo.com/imagesLocal/202206/23/202206232006247781_0.gif)
2、那么我们可以考虑求出经过每个点x的最长链的长度f[x],那么整个树的直径就是最大的f[x]。对于x的任意两个结点yi,yj,经过结点x的最长链的长度由四部分组成,如下
![f[x]=\underset {1\leq j<i\leq t}{max}\{d[y_i]+d[y_j]+w(x,y_i)+w(x,y_j)\}](http://img.inotgo.com/imagesLocal/202206/23/202206232006247781_3.gif)
3、当子节点枚举到i时,d[x]正好保存了从x出发向以yi为根的子树能达到的最远的结点的距离,这个距离就是
,所以我们先用
更新f[x],再用
更新d[x]即可
int to[maxn << 1], nex[maxn << 1], edge[maxn << 1], head[maxn];
int vis[maxn];
int cnt, n, m, r;
ll ans = 0, d[maxn];
void add(int u, int v, int w)
{
to[++cnt] = v;
edge[cnt] = w;
nex[cnt] = head[u];
head[u] = cnt;
}
void dp(int u)
{
vis[u] = 1;
for (int i = head[u]; i; i = nex[i])
{
int v = to[i];
if (vis[v])
continue;
dp(v);
ans = max(ans, d[u] + d[v] + edge[i]);
d[u] = max(d[u], d[v] + edge[i]);
}
}
void solve()
{
cin >> n;
for (int i = 0; i < n - 1; i++)
{
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
dp(1);
cout << ans << '\n';
}边栏推荐
- The Sandbox 与 BAYZ 达成合作,共同带动巴西的元宇宙发展
- PHPMailer 发送邮件 PHP
- Section 30 high availability (HA) configuration case of Tianrongxin topgate firewall
- go语言学习
- AndroidKotlin全面详细类使用语法学习指南
- Micro build low code tutorial - Application creation
- Four traversals of map sets
- The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!
- How PostgreSQL creates partition tables
- 什么是免疫组织化学实验? 免疫组织化学实验
猜你喜欢

CS5213 HDMI转VGA带音频信号输出方案
How PostgreSQL creates partition tables

What is an immunohistochemical experiment? Immunohistochemical experiment

开发协同,高效管理 | 社区征文

How to write and read ASM file system data

Section 29 basic configuration case of Tianrongxin topgate firewall

The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!
PostgreSQL怎么创建分区表详解

专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!

C# Winform 自定义进度条ProgressBar
随机推荐
Pressure measuring tool platform problem case base
Androidkotlin comprehensive and detailed class usage grammar learning guide
Apache log4j 2 reported high-risk vulnerability, coding teamed up with Tencent to protect software security
Data interpretation! Ideal L9 sprints to "sell more than 10000 yuan a month" to grab share from BBA
有哪些劵商推荐?在线开户安全么?
How can manufacturing enterprises go to the cloud?
反序列化——php反序列化
Ant won the finqa competition champion and made a breakthrough in AI technology of long text numerical reasoning
Desai wisdom number - histogram (basic histogram): the way to celebrate father's day in 2022
Detailed process of deploying redis cluster and micro service project in docker
2022云顾问技术系列之存储&CDN专场分享会
How to set ulimit value for SYSTEMd service in easycvr?
Docker中部署Redis集群与部署微服务项目的详细过程
2022 cloud consulting technology series storage & CDN special sharing meeting
运维故障经历分享
[technical dry goods] the technical construction route and characteristics of zero trust in ant Office
[observation] Dell technology + Intel aoteng Technology: leading storage innovation with "nanosecond speed"
Chaos engineering, learn about it
PHP timestamp
Save: software analysis, verification and test platform