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

来源:互联网 发布:台湾娱乐圈的现状知乎 编辑:程序博客网 时间:2024/06/14 05:40

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

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

后来看了看题解,也没说啥叫斯塔纳树,但是就是直接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;}


原创粉丝点击