题面

一句话题意:找一个点使得,使得从这个点出发作为源点,发出的流量最大,输出这个最大的流量

这道题是换根法+二次扫描的模板;

首先若确定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 题解的更多相关文章

  1. poj 3585 Accumulation Degree(二次扫描和换根法)

    Accumulation Degree 大致题意:有一棵流量树,它的每一条边都有一个正流量,树上所有度数为一的节点都是出口,相应的树上每一个节点都有一个权值,它表示从这个节点向其他出口可以输送的最大总 ...

  2. POJ 3585 Accumulation Degree【换根DP】

    传送门:http://poj.org/problem?id=3585 题意:给定一张无根图,给定每条边的容量,随便取一点使得从这个点出发作为源点,发出的流量最大,并且输出这个最大的流量. 思路:最近开 ...

  3. POJ 3585 Accumulation Degree

    二次扫描与换根法 用于解决无根树,对于每一个节点作为根时都要统计 做法: 1.先以任意一个节点为根,做树形DP,保存每个节点的DP值 2.然后自上而下dfs,对于每个节点考虑以他为根的最大值 #inc ...

  4. POJ3585:Accumulation Degree(换根树形dp)

    Accumulation Degree Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3425   Accepted: 85 ...

  5. POJ 2823 Sliding Window 题解

    POJ 2823 Sliding  Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...

  6. poj3585 Accumulation Degree【树形DP】【最大流】

    Accumulation Degree Time Limit: 5000MS   Memory Limit: 65536K Total Submissions:3151   Accepted: 783 ...

  7. 题解 poj3585 Accumulation Degree (树形dp)(二次扫描和换根法)

    写一篇题解,以纪念调了一个小时的经历(就是因为边的数组没有乘2 phhhh QAQ) 题目 题目大意:找一个点使得从这个点出发作为源点,流出的流量最大,输出这个最大的流量. 以这道题来介绍二次扫描和换 ...

  8. POJ 2585 Window Pains 题解

    链接:http://poj.org/problem?id=2585 题意: 某个人有一个屏幕大小为4*4的电脑,他很喜欢打开窗口,他肯定打开9个窗口,每个窗口大小2*2.并且每个窗口肯定在固定的位置上 ...

  9. Poj 1659.Frogs' Neighborhood 题解

    Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和 ...

随机推荐

  1. D. Treasure Hunting ( 思维题 , 贪心)

    传送门 题意: 在一个 n * m 的地图里,有 k 个宝藏,你的起点在 (1, 1), 每次你能 向下向右向左移动(只要在地图里):      现在,有 q 个安全的列, 你只有在这些列上面,你才能 ...

  2. javascript中的原型和原型链(四)

    new运算符工作原理

  3. 51 Nod 1066 Bash游戏

    1066 Bash游戏  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次最少拿1颗,最多拿K颗,拿到 ...

  4. [负数在内存中的存储] 0x80000000 = -2147483648

    https://blog.csdn.net/youyou362/article/details/72667951/ 1. 十进制负数以其补码存储在内存上 例子:-8 在内存中表示为:1111 1111 ...

  5. 分布式-信息方式-ActiveMQ构建应用

                                                     ActivemQ构建应用Broker:相当于一个 ActiveMQ服务器实例命令行启动参数示例如下:1 ...

  6. Static class 与non static class的区别

    内部静态类不需要有指向外部类的引用.但非静态内部类需要持有对外部类的引用.非静态内部类能够访问外部类的静态和非静态成员.静态类不能访问外部类的非静态成员.他只能访问外部类的静态成员.一个非静态内部类不 ...

  7. Python中Counter统计数据输出具体办法

    from collections import Counter # 列表 l_one = [1709020621, 1709020621, 1770603107, 1770603105, 177060 ...

  8. 后盾网lavarel视频项目---lavarel使用模型进行增删改查操作

    后盾网lavarel视频项目---lavarel使用模型进行增删改查操作 一.总结 一句话总结: 使用模型操作常用方法 查一条:$model=Tag::find($id); 删一条:Tag::dest ...

  9. Laravel 在构造方法中使用session

  10. Uva 101 -- the block problem

    Uva 101 the block problem 题目大意: 输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为0~n-1的位置.现对这些木块进行操作,操作分为四种. 1.move a o ...