博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[洛谷P3931]SAC E#1 - 一道难题 Tree
阅读量:4594 次
发布时间:2019-06-09

本文共 976 字,大约阅读时间需要 3 分钟。

题目大意:给你一棵带权有根树,可以切断一些边,问使得根和叶子节点不连通的最小代价。

题解:做了一天的网络流,这道题显然可以用最小割来做,但是也可以用树形$DP$,基本同,这道题一次询问,只需要那个$O(n)$的$DP$就行了。

卡点:

 

C++ Code:

#include 
#include
#define maxn 100010const long long inf = 0x3f3f3f3f3f3f3f3f;int head[maxn], cnt;struct Edge { int to, nxt, w;} e[maxn << 1];inline void addedge(int a, int b, int c) { e[++cnt] = (Edge) { b, head[a], c }; head[a] = cnt; e[++cnt] = (Edge) { a, head[b], c }; head[b] = cnt;}int n, rt, ind[maxn];long long dfs(int u, int fa = 0) { long long res = 0; for (int i = head[u], v; i; i = e[i].nxt) { v = e[i].to; if (v != fa) res += std::min(dfs(v, u), static_cast
(e[i].w)); } if (u != rt && ind[u] == 1) return inf; return res;}int main() { scanf("%d%d", &n, &rt); for (int i = 1, a, b, c; i < n; ++i) { scanf("%d%d%d", &a, &b, &c); addedge(a, b, c); ++ind[a], ++ind[b]; } printf("%lld\n", dfs(rt)); return 0;}

  

转载于:https://www.cnblogs.com/Memory-of-winter/p/10342942.html

你可能感兴趣的文章
Simplified English v.s. Vocabulary Bank for ESL
查看>>
wordpress 搭建安装教程 2 安装Apache
查看>>
【清华集训2014】Sum
查看>>
SQL注入漏洞测试工具比较
查看>>
采用smarty开发的小论坛的学习总结
查看>>
Flexible 弹性盒子模型之CSS flex-direction
查看>>
linux的nohup命令的用法
查看>>
Xilinx ISE中使用Synplify综合报错的原因之二
查看>>
Delegate辅助绘制
查看>>
"__BuglySetUserId", referenced from:---bugly 错误
查看>>
variable_scope
查看>>
Java多线程(二)线程的生命周期、优先级和控制
查看>>
cmd 一键获取 所有连接过的wifi 密码
查看>>
TOJ 2452 Ultra-QuickSort
查看>>
ZOJ 2067 White Rectangles
查看>>
windows基础应用(word)
查看>>
Linux索引节点(Inode)用满导致空间不足
查看>>
【Android】Notification
查看>>
(转)数据库 schema含义
查看>>
E2E测试神器nightwatch.js使用
查看>>