POJ 3585 Accumulation Degree 题解
一句话题意:找一个点使得,使得从这个点出发作为源点,发出的流量最大,输出这个最大的流量
这道题是换根法+二次扫描的模板;
首先若确定1为原点,那么可以写出dp方程:当v的度是1时, g[u]+=g[v];否则g[u]+=min(g[v],star[i].w);
但以上仅仅是有原点的做法,那么如果没有原点就只能是N^2了吗?当然不仅可能;
我们从上到下开始dfs,设f[i]表示以i为根所能得到的最大值,那么显然:f[1]=g[1];
然后分两种情况:如果u的度是1,那么f[v]=g[u]+star[i].w;
否则,f[v]=g[v]+min(star[i].w,f[u]-min(g[v],star[i].w));
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n;
struct littlestar{
int to;
int nxt;
int w;
}star[];
int head[],cnt;
void add(int u,int v,int w)
{
star[++cnt].to=v;
star[cnt].w=w;
star[cnt].nxt=head[u];
head[u]=cnt;
}
int g[],f[];
int du[];
void dp(int u,int fa)
{
g[u]=;
for(int i=head[u];i;i=star[i].nxt){
int v=star[i].to;
if(v==fa) continue;
dp(v,u);
if(du[v]==){
g[u]+=star[i].w;
}
else{
g[u]+=min(g[v],star[i].w);
}
}
}
void dfs(int u,int fa)
{
for(int i=head[u];i;i=star[i].nxt){
int v=star[i].to;
if(v==fa) continue;
if(du[u]==){
f[v]=g[v]+star[i].w;
}
else{
f[v]=g[v]+min(f[u]-min(star[i].w,g[v]),star[i].w);
}
dfs(v,u);
}
}
int main()
{
int t;
cin>>t;
while(t--){
memset(head,,sizeof(head));
cnt=;
memset(g,,sizeof(g));
memset(f,,sizeof(f));
memset(du,,sizeof(du));
int n;
cin>>n;
for(int i=;i<=n-;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
du[u]++;
du[v]++;
add(u,v,w);
add(v,u,w);
}
dp(,);
f[]=g[];
dfs(,);
int ans=;
for(int i=;i<=n;i++) ans=max(ans,f[i]);
cout<<ans<<endl;
}
}
POJ 3585 Accumulation Degree 题解的更多相关文章
- poj 3585 Accumulation Degree(二次扫描和换根法)
Accumulation Degree 大致题意:有一棵流量树,它的每一条边都有一个正流量,树上所有度数为一的节点都是出口,相应的树上每一个节点都有一个权值,它表示从这个节点向其他出口可以输送的最大总 ...
- POJ 3585 Accumulation Degree【换根DP】
传送门:http://poj.org/problem?id=3585 题意:给定一张无根图,给定每条边的容量,随便取一点使得从这个点出发作为源点,发出的流量最大,并且输出这个最大的流量. 思路:最近开 ...
- POJ 3585 Accumulation Degree
二次扫描与换根法 用于解决无根树,对于每一个节点作为根时都要统计 做法: 1.先以任意一个节点为根,做树形DP,保存每个节点的DP值 2.然后自上而下dfs,对于每个节点考虑以他为根的最大值 #inc ...
- POJ3585:Accumulation Degree(换根树形dp)
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3425 Accepted: 85 ...
- POJ 2823 Sliding Window 题解
POJ 2823 Sliding Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...
- poj3585 Accumulation Degree【树形DP】【最大流】
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions:3151 Accepted: 783 ...
- 题解 poj3585 Accumulation Degree (树形dp)(二次扫描和换根法)
写一篇题解,以纪念调了一个小时的经历(就是因为边的数组没有乘2 phhhh QAQ) 题目 题目大意:找一个点使得从这个点出发作为源点,流出的流量最大,输出这个最大的流量. 以这道题来介绍二次扫描和换 ...
- POJ 2585 Window Pains 题解
链接:http://poj.org/problem?id=2585 题意: 某个人有一个屏幕大小为4*4的电脑,他很喜欢打开窗口,他肯定打开9个窗口,每个窗口大小2*2.并且每个窗口肯定在固定的位置上 ...
- Poj 1659.Frogs' Neighborhood 题解
Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和 ...
随机推荐
- sh_06_元组基本使用
sh_06_元组基本使用 info_tuple = ("zhangsan", 18, 1.75, "zhangsan") # 1. 取值和取索引 print(i ...
- ABP .net Core MQTT+signalr通讯
abp版本: 4.3.0.0 .net core 版本 2.2 1.Mqtt 1.1 添加程序集:M2MqttDotnetCore(差点以为没有.net core 的) 2.2 实现代码:抄了个单例模 ...
- hive单用户多点模式配置
简介 单用户多点模式也称远程服务模式,用户非java客户端访问元数据库,在服务端启动MetaStoreServer,客户端利用Thrift协议通过MetaStoreServer访问元数据库. mysq ...
- sqli-labs(46)
0X01首先我们先来看一下源码 发现查询语句变成了 order by 参数也变成了 sort 看看是什么样的 ()首先看看本关sql语句 $sql = "SELECT * FROM use ...
- RedisTemplate与zset
Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有 ...
- React 开发中面临的九个重要抉择
抉择系列:在技术开发的过程中我们会面临着各种各样的抉择,我们在不同情境下该如何选择恰当的技术,这是本系列文章想要解决的问题. 在 React 开发的过程中我们常常会遇到一些抉择,下面我将选取其中一些个 ...
- winform Timer控件的使用
private void button1_Click(object sender, EventArgs e){ Timer timer1 = new Timer(); timer1.Interval ...
- synchronized与Lock区别简单总结
类别 synchronized Lock 存在层次 Java的关键字,在jvm层面上 是一个类 锁的释放 1.以获取锁的线程执行完同步代码,释放锁 2.线程执行发生异常,jvm会让线程释放锁 在fin ...
- [论文理解] Attentional Pooling for Action Recognition
Attentional Pooling for Action Recognition 简介 这是一篇NIPS的文章,文章亮点是对池化进行矩阵表示,使用二阶池的矩阵表示,并将权重矩阵进行低秩分解,从而使 ...
- leetcode-easy-math-326. Power of Three
mycode class Solution(object): def isPowerOfThree(self, n): """ :type n: int :rtype: ...