AcWing 1207. 大臣的旅费【第四届蓝桥杯省赛C++A组,第四届蓝桥杯省赛JAVAA组】【树的直径】【BFS】【DFS】【树形DP】


一、题目链接

AcWing 1207. 大臣的旅费
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


二、题目分析

(一)算法标签

树的直径 BFS DFS 树形DP

(二)解题思路

求出数的直径s(最远距离)之后,再求 10 × s + s ( s + 1 ) 2 10 \times s + \frac {s(s+1)}{2} 10×s+2s(s+1) 即为答案

树的直径的求法:从某一个点x,求出到x距离最远的点u,再求出到u距离最远的点v,此时v到u的距离即为树的直径
详细证明参考下文:
https://blog.csdn.net/u011426016/article/details/89164896

详细了解搜索类题目(DFS、BFS),请移步DFS BFS 搜索题目归纳


三、AC代码

解法一(dfs):

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 1e5 + 10;

struct Edge {
    int id, w;
};

vector<Edge> h[N];
int dist[N];

// 从u开始出发, 深搜求最每个点到u的距离
void dfs(int u, int father, int distance)
{
    dist[u] = distance;
    
    for (auto node : h[u])
    {
        if (node.id != father)
            dfs(node.id, u, distance + node.w);
    }
}

int main()
{
    int n;
    int a, b, c;
    cin >> n;
    for (int i = 0; i < n - 1; i ++ )
    {
        scanf("%d%d%d", &a, &b, &c);
        h[a].push_back({b, c});
        h[b].push_back({a, c});
    }
    
    // 从1开始,求出每个点到1的距离
    dfs(1, -1, 0);
    // 找到距离1最远的点u
    int u = 1;
    for (int i = 1; i <= n; i ++ )
        if (dist[i] > dist[u])
            u = i;
    
    // 从u开始,求出每个点到u的距离
    dfs(u, -1, 0);
    // 找到距离u最远的点v, dist[v] 即为树的直径
    int v;
    for (int i = 1; i <= n; i ++ )
    if (dist[i] > dist[v])
        v = i;
    
    int s = dist[v];
    
    printf("%lld", 10 * s + s * (s + 1ll) / 2);
    return 0;
}

解法二(bfs):

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

const int N = 1e5 + 10;

struct Edge {
    int id, w;
};

vector<Edge> h[N];
int dist[N];
bool st[N];

void bfs(int u)
{
    memset(st, false, sizeof st);
    st[u] = true;
    
    dist[u] = 0;
    
    queue<int> q;
    q.push(u);
    
    while (!q.empty())
    {
        auto t = q.front();
        q.pop();
        
        for (auto node : h[t])
        {
            if (!st[node.id] && node.id != u)
            {
                dist[node.id] = dist[t] + node.w;
                st[node.id] = true;
                q.push(node.id);
            }
        }
    }
}
int main()
{
    int n, a, b, c;
    cin >> n;
    for (int i = 0; i < n - 1; i ++ )
    {
        scanf("%d%d%d", &a, &b, &c);
        h[a].push_back({b, c});
        h[b].push_back({a, c});
    }
    
    bfs(1);
    
    int u = 1;
    for (int i = 1; i <= n; i ++ )
        if (dist[i] > dist[u])
            u = i;
    
    bfs(u);
    
    int v = 1;
    for (int i = 1; i <= n; i ++ )
        if (dist[i] > dist[v])
            v = i;
    
    int s = dist[v];

    printf("%lld", 10 * s + (s + 1ll) * s / 2);
    return 0;
}

四、其它题解

AcWing 1207. 大臣的旅费

vector 和 数组模拟邻接表存储图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值