[牛客每日一题](DFS, 推理) NC13886 Shortest Path

NC13886 Shortest Path
在这里插入图片描述

示例1
输入

2
4
1 2 5
2 3 8
3 4 6
6
1 3 5
3 2 3
4 5 4
4 3 9
4 6 10

输出

11
31

说明

In the first example, you can divide them into (1,2), and (3,4), then the minimum sum of length is 5+6=11
In the second example, you can divide them into (1,3),(2,4),(5,6), hen the minimum sum of length is 5+(3+9)+(10+4)=31
在这里插入图片描述

1. 推理

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

图a

aaa

图b

bbb
在这里插入图片描述

通过观察,每个边在最小距离和中只有选or不选, 不能重复选择(假设方案有重复,则必有更优的方案)

2. DFS

为了尽量避免选重边,尽可能在节点所在子树内部里寻找答案(比如上面的图a,b中, 尽量选择图a这样辈分差得不多, 条理更清晰的)

  • 若一棵子树中的节点有偶数个(含当前根节点),那么两两配对即可,不用添加新的边权。
  • 若一棵子树有奇数个节点(含当前根节点),那么当前子树除根节点外都可以内部消化掉, 根节点只能回老家找它的兄弟子树or父亲连边。

实现方法为DFS, 不妨以①为根节点, 向邻接点(除父节点)递归, 每次都要记录当前子树的节点个数, 并在递归回来的时候更新

#include<cstdio>
#include<cstring>
using namespace std;

const int N = 1e4+10, M = 2e4+10;
int h[M], w[M], e[M], ne[M], idx;
long long res;
int treesize[N];
void add(int x, int y, int z)//增加单向边, x--(z)-->y
{
    e[idx]=y, ne[idx]=h[x], w[idx]=z, h[x]=idx++; 
}

void dfs(int u, int fa, int len)
{
    treesize[u] = 1;
    for(int i = h[u]; ~i; i=ne[i])//遍历临接点
    {
        int j = e[i];
        if(j == fa) continue;
        dfs(j, u, w[i]); //向下递归子树
        treesize[u] += treesize[j];
    }
    if(treesize[u]%2) res += len; //以u为根的整棵树的点数(含u)
}
int main()
{
    int t, n;

    scanf("%d", &t);
    while(t --)
    {
        scanf("%d", &n);
        res = 0, idx = 0;
        memset(h, -1, sizeof(h));
        memset(treesize, 0, sizeof(treesize));
        int x, y, z;
        for(int i = 0; i < n-1; i ++){
            scanf("%d%d%d", &x, &y, &z);
            add(x, y, z), add(y, x, z);
        }
        dfs(1, 0, 0);
        printf("%lld\n", res);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泥烟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值