HDU 6060:RXD and dividing(DFS)
题意
给出n个点,要把除1以外的点分成k个集合,然后对于每个集合要和1这个点一起求一个最小生成树,然后问这k个最小生成树的最大总和是多少。
思路
因为每个集合都包含1这个点,因此对于每个点都至少有一条到1的路径。可以从1开始DFS,对于每个点u,它和父亲的边的贡献最多可以是min(sz[x], k),因为可以把x的儿子结点分在不同的k个集合里面,这些儿子结点都必须经过x和父亲的边才能到达1。那么对于每条边都这样做一遍。一个DFS可以求出答案。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
typedef long long LL;
struct Edge {
int v, nxt, w;
} edge[N*2];
int n, k, tot, head[N], sz[N];
LL ans;
void Add(int u, int v, int w) {
edge[tot] = (Edge) { v, head[u], w }; head[u] = tot++;
edge[tot] = (Edge) { u, head[v], w }; head[v] = tot++;
}
void DFS(int u, int fa) {
sz[u] = 1;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].w;
if(v == fa) continue;
DFS(v, u);
sz[u] += sz[v];
LL tol = sz[v] < k ? sz[v] : k;
ans += tol * w;
}
}
int main() {
while(~scanf("%d%d", &n, &k)) {
memset(head, -1, sizeof(head));
tot = ans = 0;
for(int i = 1; i < n; i++) {
int u, v, c; scanf("%d%d%d", &u, &v, &c);
Add(u, v, c);
}
DFS(1, -1);
printf("%lld\n", ans);
} return 0;
}
HDU 6060:RXD and dividing(DFS)的更多相关文章
- HDU 5113:Black And White(DFS)
题目链接 题意 给出一个n*m的图,现在有k种颜色让你对这个图每个格子染色,每种颜色最多可以使用col[i]次,问是否存在一种染色方案使得相邻格子的颜色不同. 思路 以为是构造题,结果是爆搜.对于每一 ...
- HDU 6060 RXD and dividing(dfs 思维)
RXD and dividing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- POJ3009:Curling 2.0(dfs)
http://poj.org/problem?id=3009 Description On Planet MM-21, after their Olympic games this year, cur ...
- HDU 6060 RXD and dividing(LCA)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6060 [题目大意] 给一个n个节点的树,要求将2-n号节点分成k部分, 然后将每一部分加上节点1, ...
- HDU 5795:A Simple Nim(博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Problem Description Two players take t ...
- 洛谷P1019:单词接龙(DFS)
题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的"龙"(每个单词都最多在"龙" ...
- HDU 6188:Duizi and Shunzi(贪心)(广西邀请赛)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意 有n个数字,每个数字小于等于n,两个相同的数字价值为1,三个连续的数字价值为1 .问这n个 ...
- hdu 1010 Tempter of the Bone(dfs)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- POJ1062:昂贵的聘礼(dfs)
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 58108 Accepted: 17536 题目链接:http ...
随机推荐
- python_matplotlib cannot import name _thread on mac
最后的2行错误信息是 from six.moves import _thread ImportError: cannot import name _thread 1 2 发现是six出现了问题,用pi ...
- C#控制台关闭之前做一些操作
using System; using System.Runtime.InteropServices; class Program { static void Main(string[] args) ...
- 用MVVM模式开发中遇到的零散问题总结(5)——将动态加载的可视元素保存为图片的控件,Binding刷新的时机
原文:用MVVM模式开发中遇到的零散问题总结(5)--将动态加载的可视元素保存为图片的控件,Binding刷新的时机 在项目开发中经常会遇到这样一种情况,就是需要将用户填写的信息排版到一张表单中,供打 ...
- jquery权限选择
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- EPPlus导出两千万行记录到xlsx的c#代码
private void button5_Click(object sender, EventArgs e) { Stopwatch watch = new Stopwatch(); Backgrou ...
- C#获取字符串宽度像素
通过Graphics对象的MeasureString方法可以获取字符串的大小,如下: Graphics graphics = CreateGraphics(); SizeF sizeF = graph ...
- WP8.1使用HttpClient类
Uri uri = new Uri("http://www.cnsos.net/weburl/index.htm", UriKind.Absolute); HttpClient m ...
- 关于git远程分支操作
对于用户来说,git给人提交到本地的机会.我们可以在自己的机器上创建不同的branch,来测试和存放不同的代码. 对于代码管理员而言,git有许多优良的特性.管理着不同的分支,同一套源代码可以出不一样 ...
- Understand the Qt containers(有对应表)
Container classes are one of the cornerstones of object-oriented programming, invaluable tools that ...
- iis mime 类型
application/sqlite3 .db application/octet-stream .MP4 application/vnd.android.package-archive .apk