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

本文共 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

你可能感兴趣的文章
Hadoop的安装及一些基本概念解释
查看>>
大容量分区命令parted
查看>>
从输入 URL 到页面加载完成的过程中都发生了什么事情?
查看>>
实例讲解JQuery中this和$(this)区别
查看>>
centos 7 静态ip地址模板
查看>>
影响系统性能的20个瓶颈
查看>>
shell的详细介绍和编程(上)
查看>>
软件开发性能优化经验总结
查看>>
面试题编程题05-python 有一个无序数组,如何获取第K 大的数,说下思路,实现后的时间复杂度?...
查看>>
kendo grid序号显示
查看>>
Spring 教程(二) 体系结构
查看>>
Indexes
查看>>
2.Web中使用iReport 整合----------创建html格式的
查看>>
异常备忘:java.lang.UnsupportedClassVersionError: Bad version number in .class file
查看>>
最全三大框架整合(使用映射)——applicationContext.xml里面的配置
查看>>
初步理解Java的三大特性——封装、继承和多态
查看>>
知识点积累(一)
查看>>
iphone-common-codes-ccteam源代码 CCFile.m
查看>>
python:浅析python 中__name__ = '__main__' 的作用
查看>>
修改tomcat端口后不能IP访问问题
查看>>