2017年多校赛第三场 1005 RXD and dividing(DFS)

当时写这道题目的时候实在是懵了。研究了好久什么叫做斯塔纳树。就觉得很混乱。

我现在对斯塔纳树的理解就是,选取若干个点,然后生成一棵数即可。两点之间的边肯定是走最短路的。但是题目说的最小斯塔纳树又是啥呢。。。

后来看了看题解,也没说啥叫斯塔纳树,但是就是直接DFS求贡献度即可。

代码如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
const int maxn = 1e6 + 5;
int n, k;
int head[maxn], to[maxn << 1], nx[maxn << 1], ppp;
long long cost[maxn << 1];
long long ans;
inline void add_edge(int u, int v, long long c) {
	to[ppp] = v, nx[ppp] = head[u], cost[ppp] = c, head[u] = ppp++;
	to[ppp] = u, nx[ppp] = head[v], cost[ppp] = c, head[v] = ppp++;
}

int dfs(int last, int u, int id) {
	int cnt = 1;
	for(int i = head[u]; ~i; i = nx[i]) {
		if(to[i] == last)
			continue;
		cnt += dfs(u, to[i], i);
	}
	ans += min(cnt, k) * cost[id];
	return cnt;
}

int main() {
	while(scanf("%d%d", &n, &k) != EOF) {
		ppp = 0;
		memset(head, -1, sizeof(head));
		ans = 0;
		int a, b;
		long long c;
		for(int i = 1; i < n; i++) {
			scanf("%d%d%I64d", &a, &b, &c);
			add_edge(a, b, c);
		}
		for(int i = head[1]; ~i; i = nx[i]) {
			dfs(1, to[i], i);
		}
		printf("%I64d\n", ans);
	} 
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值