当前位置:网站首页>图论(树的直径)
图论(树的直径)
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';
}边栏推荐
- C#/VB. Net word to text
- 微信视频号如何用 PC 电脑做直播?
- How to write and read ASM file system data
- AndroidKotlin全面详细类使用语法学习指南
- The sandbox and bayz have reached cooperation to jointly drive the development of metauniverse in Brazil
- SQL语句中EXISTS的详细用法大全
- The Sandbox 与 BAYZ 达成合作,共同带动巴西的元宇宙发展
- 迪赛智慧数——柱状图(基本柱状图):2022年父亲节过节的方式
- Androidkotlin comprehensive and detailed class usage grammar learning guide
- 新股民怎样炒股票开户?在线开户安全么?
猜你喜欢

Giants end up "setting up stalls" and big stalls fall into "bitter battle"
AndroidKotlin全面详细类使用语法学习指南

Telecommuting: how to become a master of time management| Community essay solicitation
![[design] 1359- how umi3 implements plug-in architecture](/img/f1/c4cb7715aff7f10d099e4f11b32c01.png)
[design] 1359- how umi3 implements plug-in architecture

CTF—Go题目复现
Docker中部署Redis集群与部署微服务项目的详细过程
Mysql中的触发器定义及语法介绍

Cs5213 HDMI to VGA with audio signal output scheme

Bilibili × Blue bridge cloud course | online programming practice competition is new!

FANUC机器人SRVO-050碰撞检测报警原因分析及处理对策(亲测可用)
随机推荐
Flutter中的GetX状态管理用起来真的那么香吗?
生鲜前置仓的面子和里子
[js] 去除小数点后面多余的零
Troubleshooting of undefined problems in the channel list of easynvr channel management
Application of clock synchronization system in banking system
CTF—Go题目复现
SQL Server Common SQL
Practice of issuing vouchers for Tiktok payment of 100000 TPS traffic
Is Everbright futures safe? What do I need to open an account?
How does the fortress connection key server associate with the server host?
Oracle关闭回收站
远程办公之:如何成为时间管理大师?| 社区征文
在OpenCloudOS使用snap安装.NET 6
Postman可以集成到CI,CD流水线中做自动化接口测试吗?
Chaos engineering, learn about it
Trigger definition and syntax introduction in MySQL
C#/VB. Net word to text
Zynq Ultrascale+ RF Data Coverter IP配置 - ADC
浩哥的博客之路
Aicon2021 | AI technology helps content security and promotes the healthy development of Internet Environment