tjut 4313
来源:互联网 发布:mac os更新系统失败 编辑:程序博客网 时间:2024/05/16 14:07
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> using namespace std; const int maxn = 100010; int t, n, k; int mach[maxn], father[maxn]; struct Edge { int u, v, c; }edge; vector<Edge> vv; bool cmp(Edge a, Edge b) { return a.c > b.c; } void initSet() { for (int i = 0; i < maxn; ++i) { father[i] = i; mach[i] = 0; } } int find(int x) { if (x != father[x]) { father[x] = find(father[x]); } return father[x]; } void merge(int a, int b) { a = find(a); b = find(b); if (a == b) return ; if (a < b) { father[b] = a; mach[a] += mach[b]; } else { father[a] = b; mach[b] += mach[a]; } } int main() { scanf("%d", &t); while (t--) { scanf("%d%d", &n, &k); vv.clear(); initSet(); int u, v, c; __int64 ans = 0; for (int i = 0; i < n - 1; ++i) { scanf("%d%d%d", &u, &v, &c); edge.u = u; edge.v = v; edge.c = c; vv.push_back(edge); ans += c; } for (int i = 0; i < k; ++i) { scanf("%d", &u); mach[u] = 1; } sort(vv.begin(), vv.end(), cmp); for (int i = 0; i < n - 1; ++i) { u = find(vv[i].u); v = find(vv[i].v); if (mach[u] + mach[v] <= 1) { merge(u, v); ans -= vv[i].c; } } printf("%I64d\n", ans); } return 0; }
0 0
- tjut 4313
- tjut 5289
- tjut 5288
- tjut 5294
- tjut 2586
- tjut 5296
- tjut 5297
- tjut 5299
- tjut 5384
- tjut 5387
- tjut 5386
- tjut 5381
- tjut 5400
- tjut 5399
- tjut 5396
- tjut 5398
- tjut 5412
- tjut 5410
- js/JavaScript获取IP地址的方法小结
- web.xml基本配置
- Label--Label自适应
- Android framework系统默认设置修改
- 深入解析 ObjC 中方法的结构
- tjut 4313
- java异常处理的throw和throws的区别
- puppet 配置 2 模块
- C++ 命名空间
- 在移动端上用GIS开发利用html5获取经纬度并在google地图中查看
- jvm原理--简单的程序加载分析
- XML文件的读写
- 无法访问centos的tomcat
- MyEclipse导入Maven项目遇到的几个问题