HDU 6060 RXD and dividing
来源:互联网 发布:淘宝联盟权益推广 编辑:程序博客网 时间:2024/06/05 14:55
题意:
给出一个具有n个顶点的树, 将其中的2~n分成k部分, 求1号顶点到分成k块后各个顶点的最大权值和。
思路:
将2~n的结点编号{1~k},每条边的权乘以其下子树的大小与k的较小值。
代码:
# include <bits/stdc++.h>using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int Q = 1e9+7;const int N = 1e6+10;int n, k, tot;int head[N], f[N], si[N], w[N];struct Edge { int fr, to, co, ne;} E[N * 2];void add (int u, int v, int w){ E[tot].fr = u; E[tot].to = v; E[tot].co = w; E[tot].ne = head[u]; head[u] = tot++;}void dfs (int u, int fa){ si[u] = 1; for (int i=head[u]; i!=-1; i=E[i].ne) { int to = E[i].to; if (to == fa) continue; w[to] = E[i].co; dfs(to, u); si[u] += si[to]; }}void solve (){ LL ans = 0; tot = 0; memset (head, -1, sizeof (head)); memset (si, 0, sizeof (si)); for (int i=0; i<n-1; ++i) { int u, v, w; scanf ("%d%d%d", &u, &v, &w); add(u, v, w); add(v, u, w); } dfs (1, -1); for (int i=2; i<=n; ++i) { ans += (LL)w[i] * min(si[i], k); } printf ("%lld\n", ans);}int main (){ while (scanf ("%d%d", &n, &k) != EOF) { solve (); } return 0;}
阅读全文
0 0
- HDU 6060 RXD and dividing
- HDU 6060 RXD and dividing
- [HDU]-6060 RXD and dividing
- HDU 6060 RXD and dividing
- HDU-6060 RXD and dividing
- HDU 6060 RXD and dividing
- hdu 6060 RXD and dividing
- HDU 6060 RXD and dividing
- hdu--6060-RXD and dividing
- HDU 6060 RXD and dividing
- hdu-6060-RXD and dividing
- hdu--6060--RXD and dividing
- HDU 6060 RXD and dividing
- HDU 6060 RXD and dividing
- HDU 6060 RXD and dividing
- HDU 6060 RXD and dividing 思维 + dfs
- Hdu 6060 RXD and dividing【思维】
- HDU 6060 RXD and dividing [想法题]
- newFixedThreadPool
- vue axios 遇到“No 'Access-Control-Allow-Origin' header is present on the requested resource.”的问题
- 移动网络介绍
- Spring-AOP理解
- shell 内嵌 二进制
- HDU 6060 RXD and dividing
- 系统学习深度学习(三十一)--CTC
- SpringSecurity学习(一)
- apache 允许跨域,字体跨域等
- 论文阅读:BiSeg: Simultaneous Instance Segmentation and Semantic Segmentation
- CodeForces 52 B.Right Triangles(水~)
- 通过xml处理sql语句时对小于号与大于号的处理转换
- 必备知识总结
- jenkins + Git 搭建持续集成环境