NC51180 Accumulation Degree
题目
题目描述
Trees are an important component of the natural landscape because of their prevention of erosion and the provision of a specific ather-sheltered ecosystem in and under their foliage. Trees have also been found to play an important role in producing oxygen and reducing carbon dioxide in the atmosphere, as well as moderating ground temperatures. They are also significant elements in landscaping and agriculture, both for their aesthetic appeal and their orchard crops (such as apples). Wood from trees is a common building material.
Trees also play an intimate role in many of the world's mythologies. Many scholars are interested in finding peculiar properties about trees, such as the center of a tree, tree counting, tree coloring. A(x) is one of such properties.
A(x) (accumulation degree of node x) is defined as follows:
- Each edge of the tree has an positive capacity.
- The nodes with degree of one in the tree are named terminals.
- The flow of each edge can't exceed its capacity.
- A(x) is the maximal flow that node x can flow to other terminal nodes.
Since it may be hard to understand the definition, an example is showed below:
| A(1)=11+5+8=24 | ||
|---|---|---|
| Details: | 1->2 | 11 |
| 1->4->3 | 5 | |
| 1->4->5 | 8(since 1->4 has capacity of 13) | |
| A(2)=5+6=11 | ||
| Details: | 2->1->4->3 | 5 |
| 2->1->4->5 | 6 | |
| A(3)=5 | ||
| Details: | 3->4->5 | 5 |
| A(4)=11+5+10=26 | ||
| Details: | 4->1->2 | 11 |
| 4->3 | 5 | |
| 4->5 | 10 | |
| A(5)=10 | ||
| Details: | 5->4->1->2 | 10 |
The accumulation degree of a tree is the maximal accumulation degree among its nodes. Here your task is to find the accumulation degree of the given trees.
输入描述
The first line of the input is an integer T which indicates the number of test cases. The first line of each test case is a positive integer n. Each of the following n - 1 lines contains three integers x, y, z separated by spaces, representing there is an edge between node x and node y, and the capacity of the edge is z. Nodes are numbered from 1 to n.
All the elements are nonnegative integers no more than 200000. You may assume that the test data are all tree metrics.
输出描述
For each test case, output the result on a single line.
示例1
输入
1
5
1 2 11
1 4 13
3 4 5
4 5 10
输出
26
题解
知识点:树形dp。
显然树形dp,二次扫描+换根。
第一次先预处理出每个子树的流量最大值,设 \(f[u]\) 为以 \(u\) 为根的子树的流量最大值。转移方程为:
\left \{
\begin{aligned}
&\min (f[v_i],w_{v_i}) & &,v_i不是叶子节点\\
&w_{v_i} & &,v_i是叶子节点
\end{aligned}
\right.
\]
第二次处理出每个点对于整棵树的最大值,设 \(ff[u]\) 为以 \(u\) 为源点的流量最大值。转移方程为:
\left \{
\begin{aligned}
&w_v & &,u是叶子节点\\
&\min(ff[u] - w_v, w_v) & &,v是叶子节点\\
&\min(ff[u] - \min(f[v], w_u), w_u)& &,otherwise
\end{aligned}
\right.
\]
具体原因注释里写着。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<pair<int, ll>> g[200007];
ll f[200007];
void dfs1(int u, int fa) {
for (auto [v, w] : g[u]) {
if (v == fa) continue;
dfs1(v, u);
f[u] += g[v].size() == 1 ? w : min(f[v], w);///叶子节点可以直接贡献,因此直接加
}
}
void dfs2(int u, int fa) {
for (auto [v, w] : g[u]) {
if (v == fa) continue;
if (g[u].size() == 1) f[v] += w;///如果父节点是一个叶节点,可以直接贡献,不需要考虑其到其他子树流量为0
else if (g[v].size() == 1) f[v] += min(f[u] - w, w);///否则如果自己是一个叶节点,自己支路流量应该直接是w,而不能min(f[v],w)
else f[v] += min(f[u] - min(f[v], w), w);///否则大家都不是叶子节点,则总贡献=子树内贡献+min(父节点总贡献-子树实际贡献,边权)
dfs2(v, u);
}
}
bool solve() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) g[i].clear(), f[i] = 0;
for (int i = 1;i < n;i++) {
int u, v, w;
cin >> u >> v >> w;
g[u].push_back({ v,w });
g[v].push_back({ u,w });
}
dfs1(1, 0);
dfs2(1, 0);
ll ans = 0;
for (int i = 1;i <= n;i++) ans = max(ans, f[i]);
cout << ans << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
NC51180 Accumulation Degree的更多相关文章
- poj3585 Accumulation Degree【树形DP】【最大流】
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions:3151 Accepted: 783 ...
- POJ3585:Accumulation Degree(换根树形dp)
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3425 Accepted: 85 ...
- poj 3585 Accumulation Degree(二次扫描和换根法)
Accumulation Degree 大致题意:有一棵流量树,它的每一条边都有一个正流量,树上所有度数为一的节点都是出口,相应的树上每一个节点都有一个权值,它表示从这个节点向其他出口可以输送的最大总 ...
- Accumulation Degree
#include<cstdio> #include<cstring> #define INF 0x7fffffff using namespace std; ; inline ...
- poj3585 Accumulation Degree[树形DP换根]
思路其实非常简单,借用一下最大流求法即可...默认以1为根时,$f[x]$表示以$x$为根的子树最大流.转移的话分两种情况,一种由叶子转移,一种由正常孩子转移,判断一下即可.换根的时候由頂向下递推转移 ...
- POJ 3585 Accumulation Degree
二次扫描与换根法 用于解决无根树,对于每一个节点作为根时都要统计 做法: 1.先以任意一个节点为根,做树形DP,保存每个节点的DP值 2.然后自上而下dfs,对于每个节点考虑以他为根的最大值 #inc ...
- 【POJ3585】Accumulation Degree 二次扫描与换根法
简单来说,这是一道树形结构上的最大流问题. 朴素的解法是可以以每个节点为源点,单独进行一次dp,时间复杂度是\(O(n^2)\) 但是在朴素求解的过程中,相当于每次都求解了一次整棵树的信息,会做了不少 ...
- POJ3585 Accumulation Degree(二次扫描与换根法)
题目:http://poj.org/problem?id=3585 很容易想出暴力.那么就先扫一遍. 然后得到了指定一个根后每个点的子树值. 怎么转化利用一下呢?要是能找出当前点的父亲的 “ 不含当前 ...
- [POJ3585]Accumulation Degree
题面 \(\text{Solution:}\) 有些题目不仅让我们做树型 \(\text{dp}\) ,而且还让我们换每个根分别做一次, 然后这样就愉快的 \(\text{TLE}\) 了,所以我们要 ...
- POJ3585 Accumulation Degree 【树形dp】
题目链接 POJ3585 题解 -二次扫描与换根法- 对于这样一个无根树的树形dp 我们先任选一根进行一次树形dp 然后再扫一遍通过计算得出每个点为根时的答案 #include<iostream ...
随机推荐
- 使用XStream,XMLSerializer 解析及格式转换
博主原创,转载请注明出处 1.引入对应的maven依赖: <!--xstream--> <dependency> <groupId>com.thoughtworks ...
- Cloudquery的学习安装与使用
Cloudquery的学习安装与使用 下载 官方下载地址: https://www.cloudquery.club/download https://pan.baidu.com/s/1a7XOrnMU ...
- [转帖]centos6 free 和 centos 7的free 的差异与对比
目录 一 centos6 free 常用参数和含义 centos6 free 命令示例 free 值讲解 计算公式 二 centos7 free 常用的参数 centos7 free 命令示例 计算公 ...
- [转帖]CTF -bugku-misc(持续更新直到全部刷完)
CTF -bugku-misc(持续更新直到全部刷完) https://www.cnblogs.com/cat47/p/11432475.html 1.签到题 点开可见.(这题就不浪费键盘了) CTF ...
- [转帖]logback:logback和slf4j中的:appender、logger、encoder、layout
(1)appender 1.appender标签是logback配置文件中重要的组件之一.在logback配置文件中使用appender标签进行定义.可 以包含0个或多个appender标签. 2.a ...
- CentOS8 安装Oracle19c RPM的办法
1. 下载相应的rpm包 我这边使用的主要有: -rw-r--r-- 1 root root 19112 Apr 5 15:13 compat-libcap1-1.10-7.el7.x86_64.rp ...
- 神通奥斯卡数据库是否兼容Oracle, 以及参数修改的办法
1. 最近公司要适配神通数据库, 但是因为一些功能异常.参数可能存在风险. 为了减少问题, 想着简单描述一下这些的处理. 开发和客户给的默认参数建议 1. 不选择 兼容oracle模式 2. 字符集选 ...
- DBeaver连接国产信创数据库的步骤
DBeaver连接国产信创数据库的步骤 本次连接使用的数据库类型 1.达梦 2.神通 3.人大金仓 4.瀚高 安装DBeaver 通过官网或者是其他网站下载最新的数据库介质 之后的操作为: 这次不感谢 ...
- ThreadLocal源码解析及实战应用
作者:京东物流 闫鹏勃 1 什么是ThreadLocal? ThreadLocal是一个关于创建线程局部变量的类. 通常情况下,我们创建的变量是可以被任何一个线程访问并修改的.而使用ThreadLoc ...
- TypeScript中泛型<T>详细讲解
1.泛型 在定义函数或者接口或者类的时候 不能预先确定要使用的数据类型 而是在使用函数.接口.或者类的时候才能够确定数据类型 这个时候我们就需要使用的是泛型 2.功能描述 我们需要实现一个方法,方法中 ...