~树的直径~

本文介绍了如何在给定无向加权树的情况下,使用二次扫描(两次BFS或DFS)来求解树的直径问题。作者提供了C++代码示例,并提到了另一种方法——树形动态规划。最后还提及了一个拓展题目,涉及到了线段树的应用。
摘要由CSDN通过智能技术生成


树的直径:2024.4.13 树的直径 - Virtual Judge

------------------------------------------------~~~~~一道板子题~~~~~------------------------------------------------

树的直径:树的直径就是树中所有最短路经距离的最大值
共两种求法:1.树形dp,我之前写过一篇—— 《树上问题——树形dp-CSDN博客》
                      2.二次扫描(2次bfs或dfs)
今天主要讲第2种

我们先看看这个题:

Given a tree T with non-negative weight, find the diameter of the tree.

The diameter of a tree is the maximum distance between two nodes in a tree.

Input
n
s1 t1 w1
s2 t2 w2
:
sn-1 tn-1 wn-1
The first line consists of an integer n which represents the number of nodes in the tree. Every node has a unique ID from 0 to n-1 respectively.

In the following n-1 lines, edges of the tree are given. si and ti represent end-points of the i-th edge (undirected) and wi represents the weight (distance) of the i-th edge.

Output
Print the diameter of the tree in a line.

Constraints
1 ≤ n ≤ 100,000
0 ≤ wi ≤ 1,000
Sample Input 1
4
0 1 2
1 2 1
1 3 3
Sample Output 1
5
Sample Input 2
4
0 1 1
1 2 2
2 3 4
Sample Output 2
7
其实就是很单纯的想让你求树的直径(无根树),我们下面来解剖以下代码:

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int N=1e5+100;
struct edge{
    int v,w;//可用结构体,也可用make_pair
}; 
vector<edge>son[N];
int d[N],n,maxd,x,A,B;//maxd是最大距离
void dfs(int u,int fa){//深搜
    int k=son[u].size();
    for(int i=0;i<k;i++){
        int v=son[u][i].v,w=son[u][i].w;
        if(v==fa)continue;
        d[v]=d[u]+w;//d数组记录距离,w是边权
        if(d[v]>maxd)maxd=d[x=v];
        dfs(v,u);
    }

int main(){
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        int u,w,v;
        scanf("%d%d%d",&u,&v,&w);
        u++,v++;
        son[u].push_back((edge){v,w});
        son[v].push_back((edge){u,w});//无根树读入
    }
    dfs(1,0);
    d[x]=0;
    dfs(x,0);//二次扫描
    printf("%d\n",maxd);
    return 0;
}
再来一道!
2024.4.13 树的直径 - Virtual Judge
​来,大家想一想,这题能用树的直径吗?🧐🧐🧐


不但能用,还得用两次!
用两次树的直径,这题就AK了
#include<iostream>//对于代码我不敢做任何评价( ̄▽ ̄)"
#include<cstdio>
#include<vector>
using namespace std;
const int N=1e5+100;
struct edge{
    int v,w;
}; 
vector<edge>son[N];
int d[N],n,maxd,x,tot,s;
void dfs(int u,int fa){
    int k=son[u].size();
    for(int i=0;i<k;i++){
        int v=son[u][i].v,w=son[u][i].w;
        if(v==fa)continue;
        d[v]=d[u]+w;
        if(d[v]>maxd)maxd=d[x=v];
        dfs(v,u);
    }

int main(){
    scanf("%d%d",&n,&s);
    for(int i=1;i<n;i++){
        int u,w,v;
        scanf("%d%d%d",&u,&v,&w);
        tot+=w;
        son[u].push_back((edge){v,w});
        son[v].push_back((edge){u,w});
    }
    maxd=0;
    d[1]=0;
    dfs(1,0);
    d[x]=0;
    maxd=0;
    dfs(x,0);
    tot*=2;
    printf("%d\n",tot-maxd);
    
    return 0;
}
关于树的直径,我们就讲这些
拓展:[TJOI2017] 城市 - 洛谷我朋友竟然用线段树AK了,真·神犇大家加油!)

奇环(Odd-Eulerian Tree)是一种特殊的有向图,它的特征是有奇数个顶点度为偶数,其余顶点的度为奇数。奇环直径是指从其中一个奇度顶点到另一个奇度顶点的最大简单路径长度。 要计算奇环直径,我们通常需要遍历整个图并维护两个变量:当前最短距离(shortestDistance),以及已知最长的距离(longestDistance)。我们可以使用深度优先搜索(DFS)结合回溯的方法来进行查找。 以下是一个简单的C++代码示例,假设你已经有了一个表示奇环邻接关系的边集`edges`,其中每个元素是一个`std::pair<int, int>`,代表从节点`u`指向节点`v`的一条边: ```cpp #include <vector> #include <queue> int diameterOfOddEulerianTree(const std::vector<std::pair<int, int>>& edges) { std::vector<int> degrees(edges.size(), 0); for (const auto& edge : edges) { degrees[edge.first]++; degrees[edge.second]++; } // 奇数度顶点集合 std::vector<int> oddDegreeVertices; for (int i = 0; i < degrees.size(); ++i) { if (degrees[i] % 2 == 1) { oddDegreeVertices.push_back(i); } } if (oddDegreeVertices.empty()) { // 如果没有奇度顶点,说明不是奇环 return -1; } int shortestDistance = 0; int longestDistance = INT_MIN; // 初始化最长距离为任意两个奇度顶点之间的距离 longestDistance = dfs(oddDegreeVertices[0], 0, edges); // 从最后一个奇度顶点开始,再尝试寻找更长的路径 dfs(oddDegreeVertices.back(), 0, edges, &longestDistance); return longestDistance; } // DFS辅助函数,返回起点到终点的最长路径 int dfs(int vertex, int currentDistance, const std::vector<std::pair<int, int>>& edges, int* longestDistance) { *longestDistance = std::max(*longestDistance, currentDistance); std::queue<std::pair<int, int>> q; q.push({vertex, currentDistance}); while (!q.empty()) { int u = q.front().first; int dist = q.front().second; q.pop(); for (const auto& edge : edges[u]) { int v = edge.first; if (dist + 1 < degrees[v]) { // 避免重复计数 dfs(v, dist + 1, edges, longestDistance); } } } return *longestDistance; } ``` 在这个代码中,`diameterOfOddEulerianTree`函数首先计算每个节点的度,然后找到所有的奇度顶点。接下来,它执行两次DFS,分别从第一个和最后一个奇度顶点开始,更新最长距离。请注意,这个实现假定输入是有效的,即图中的所有边都是相连的,并且没有自环。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值