【题目大意】

给出一棵带权树,有两类点,一类黑点,一类白点。

求切断黑点和白点间路径的最小代价。

$n \leq 10^5$

【题解】

直接最小割能过。。但是树形dp明显更好写

设$f_{x,0/1/2}$表示$x$这个点的子树中,0表示没有带颜色的点连到这个子树的根$x$,1表示黑点连到$x$,2表示白点连到$x$。

直接转移即可。具体看代码,挺好推得。。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull;
typedef long double ld; const int N = 1e5 + , M = 2e5 + ;
const ll inf = 1e17; int n, d[N];
int head[N], nxt[M], to[M], w[M], tot = ;
inline void add(int u, int v, int _w) {
++tot; nxt[tot] = head[u]; head[u] = tot; to[tot] = v; w[tot] = _w;
}
inline void adde(int u, int v, int _w) {
add(u, v, _w), add(v, u, _w);
} ll f[N][];
// 没有有颜色点连到根上,只有黑点连到根上,只有白点连到根上
inline void dp(int x, int fa = ) {
f[x][] = inf, f[x][] = f[x][] = ;
if(d[x] == ) f[x][] = ;
if(d[x] == ) f[x][] = inf;
if(d[x] == ) f[x][] = inf;
for (int i=head[x]; i; i=nxt[i]) {
if(to[i] == fa) continue;
int y = to[i];
dp(y, x);
f[x][] += min(f[y][], min(f[y][], f[y][]) + w[i]);
if(f[x][] > inf) f[x][] = inf;
f[x][] += min(min(f[y][], f[y][]), f[y][] + w[i]);
if(f[x][] > inf) f[x][] = inf;
f[x][] += min(min(f[y][], f[y][]), f[y][] + w[i]);
if(f[x][] > inf) f[x][] = inf;
}
// printf("x = %d, f[x][0] = %I64d, f[x][1] = %I64d, f[x][2] = %I64d\n", x, f[x][0], f[x][1], f[x][2]);
} int main() {
freopen("tree.in", "r", stdin);
freopen("tree.out", "w", stdout);
cin >> n;
for (int i=, u, v, _w; i<n; ++i) {
scanf("%d%d%d", &u, &v, &_w);
adde(u, v, _w);
}
int T; cin >> T;
for (int i=, x; i<=T; ++i) {
scanf("%d", &x);
d[x] = ;
}
cin >> T;
for (int i=, x; i<=T; ++i) {
scanf("%d", &x);
d[x] = ;
}
dp();
cout << min(f[][], min(f[][], f[][]));
return ;
}
/*
6
1 2 5
2 4 4
2 5 1
1 3 2
3 6 7
1
4
2
5 6
*/

最新文章

  1. ORACLE的SQL JOIN方式小结
  2. java多线程-线程通信
  3. C++混合编程之idlcpp教程Python篇(3)
  4. svn搭建,很简单
  5. javascript设计模式学习之十四——中介者模式
  6. LINQ基础 之 LINQ TO SQL (二)
  7. Programmer&#39;s Jokes
  8. hdu 2845(dp基础题)
  9. 学习C++ Primer 的个人理解(零)
  10. 安装Maven、nexus
  11. WebApi接口开发
  12. oracle error info
  13. ovirt node的安装简介
  14. HTML学习(四)样式
  15. python爬虫之Beautiful Soup的基本使用
  16. $Django 中间件 csrf
  17. Spring框架源码阅读之Springs-beans(一)容器的基本实现概述(待续)
  18. [HDU5685]Problem A
  19. SharePoint 2013自定义Providers在基于表单的身份验证(Forms-Based-Authentication)中的应用
  20. 支付宝:电脑网站沙箱测试(Java)

热门文章

  1. LintCode-73.前序遍历和中序遍历树构造二叉树
  2. 【OSG】运行OSG示例出现的奶牛不完整问题
  3. .net 内置对象之Session对象和Session的过期时间
  4. win10 64位系统中安装多个jdk版本的切换问题
  5. 【Docker 命令】- images命令
  6. Python自定义包在linux服务器导入错误的解决办法
  7. VC学习笔记:对话框
  8. 使用mac电脑,对Github客户端的简单操作1----开源项目
  9. CS6的安装与破解
  10. sql批量更新关系型数据库