文章目录

  • 题目描述
  • 样例分析:
  • 题意:
  • 题解:
  • 代码:

本题目传送

题目树学是这个题的简易版,也涉及换根问题,可以先看看这个
树学

时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit
IO Format:%lld

题目描述

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:

  1. Each edge of the tree has an positive capacity.
  2. The nodes with degree of one in the tree are named terminals.
  3. The flow of each edge can’t exceed its capacity.
  4. 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

题意:

看看样例分析应该就明白了
每个节点都有流量,求出最大流量是多少?

题解:

flow【i】表示i点的流量:
一个点的流量是怎么来的?如果j(j是i的子节点)的流量小于i与j边的容量,flow【i】=flow[j],如果大于两点之间的容量,flow[i]=i与j的流量
i与j的流量就是i与j的边权,我们用edge[i][j]表示。
可以得到公式:flow[i]=∑min(flow[j],edge[i][j])
因为i有可能有很多子节点,所以加在一起
考虑完i之后,我们来考虑换根
如图:
我们将根从x换成y
题一中(以x为根)
x的流量来自于y,子树2,子树3
y的流量来自于子树1
图二中(以y为根)
x的流量来自子树2,子树3
y的流量来自子树1,x

我们发现换根后,x的流量就没有了y的部分,其他都还在,此时x的流量就是原本的减去从y流向x的部分,new[x]=flow[ x ] - min ( flow[ y ] , edge[ x ] [ y ] ),这个new表示x新的流量

我们再看y,y的流量多了从x流来的部分,y的流量就是flow[y]+min(new[x],edge[x][y]),,因为换根x的流量发生改变(上一段所讲),那流向y的是现在x的流量,而不是换跟前的flow[x].

换根前后,图二中绿色区域没有发生改变,也就是父节点改变影响不到子节点

还要注意叶子节点,如果x从根变成叶子节点(x的儿子只有y,当y成为根节点之后,x没有了儿子),x的流量不是上面的公式,而是变成了edge[x][y],因为没有子节点的流量流向x,只有x与y的边权值,也就是上面讲的式子使用条件是min(x,y),x和y不能为0。

先求出x为根的流量,然后依次换根求出最大值

代码:

#include <bits/stdc++.h>#define inf 0x7f
typedef long long LL;
using namespace std;
const int maxn = 2e5 + 3;int n;int head[maxn], cnt = 0, d[maxn], deg[maxn], f[maxn];
struct edge{int x, y;int next;int w;
}edge[maxn * 2];void init()
{  cnt = 0;memset(head, -1, sizeof(head));memset(d, 0, sizeof(d));memset(deg, 0, sizeof(deg));
}void addedge(int x, int y, int w)
{edge[cnt].x = x;edge[cnt].y = y;edge[cnt].w = w;edge[cnt].next = head[x];head[x] = cnt++;}void dfs(int root, int fa)
{int ans = 0;for(int i = head[root]; i != -1; i = edge[i].next){int y = edge[i].y;if(y == fa){continue;}if(deg[y] == 1){//如果y只有一个子节点,y的流量只能是root与y的边权值 ans += edge[i].w;}else{dfs(y, root);ans += min(d[y], edge[i].w);}}d[root] = ans return ;
}//先求出节点x的流量 void dp(int x, int fa)
{for(int i = head[x]; i != -1; i = edge[i].next){int y = edge[i].y;if(edge[i].y == fa)continue;if(deg[x] == 1){f[y] = d[y] + edge[i].w;}else{f[y] = d[y] + min(f[x] - min(d[y], edge[i].w), edge[i].w);//核心公式 }dp(y, x);}
}//从x不断换根 int main()
{int t;cin>>t;int x, y, w;while(t--){init();//初始化 scanf("%d", &n);for(int i = 0; i < n - 1; i++){scanf("%d%d%d", &x, &y, &w);addedge(x, y, w);//添边 addedge(y, x, w);//添边 deg[x]++;//deg用于判断这个点有几个子节点 deg[y]++;}int s = 1;dfs(s, 0);//求x的流量 f[s] = d[s];dp(s, 0);//不断换根 int ans = 0;for(int i = 1; i <= n; i++){ans = max(ans, f[i]);}printf("%d\n", ans);}return 0;
}

牛客网【每日一题】4月13号 Accumulation Degree相关推荐

  1. 牛客网 每日一题 7月23日题目精讲—wpy的请求

    来源:牛客网: 文章目录 wpy的请求 题解: 代码: wpy的请求 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K Special Judge ...

  2. 2021-09-02牛客网每日10题--前端

    3 ng-class是做什么用的? 给元素绑定类名 eg:用法①: // An highlighted block <div ng-class="{'A':isA,'B':isB,'C ...

  3. 牛客网SQL刷题笔记(MySQL)

    牛客网SQL刷题笔记(MySQL) 此博客集合LeetCode.牛客网常见的题型及其解法,侵删 目录 牛客网SQL刷题笔记(MySQL) 类型1:查找排名第几的数据 SQL2 查找入职员工时间排名倒数 ...

  4. 牛客网Java刷题知识点之关键字static、static成员变量、static成员方法、static代码块和static内部类...

    不多说,直接上干货! 牛客网Java刷题知识点之关键字static static代表着什么 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个"伪全局"的概 ...

  5. 牛客网Java刷题知识点之构造函数可以调用一般函数,但是一般函数不可以直接调用构造函数...

    不多说,直接上干货! 通过 牛客网Java刷题知识点之构造函数是什么.一般函数和构造函数什么区别呢.构造函数的重载.构造函数的内存图解 我们对构造函数有了一个比较清楚的认识,当我们在创建对象时,我们会 ...

  6. 牛客网Java刷题知识点之Java 集合框架的构成、集合框架中的迭代器Iterator、集合框架中的集合接口Collection(List和Set)、集合框架中的Map集合...

    不多说,直接上干货! 集合框架中包含了大量集合接口.这些接口的实现类和操作它们的算法. 集合容器因为内部的数据结构不同,有多种具体容器. 不断的向上抽取,就形成了集合框架. Map是一次添加一对元素. ...

  7. 牛客网Java刷题知识点之ArrayList 、LinkedList 、Vector 的底层实现和区别

    不多说,直接上干货! 这篇我是从整体出发去写的. 牛客网Java刷题知识点之Java 集合框架的构成.集合框架中的迭代器Iterator.集合框架中的集合接口Collection(List和Set). ...

  8. 牛客网Veirlog刷题答案目录(持续更新)

    牛客网Veirlog刷题答案目录(持续更新) 基础篇 进阶篇 基础篇 1.VL1--四选一多路选择器 2.VL2--异步复位的串联T触发器 3.VL3--奇偶校验 4.VL4--移位运算与乘法 5.V ...

  9. 牛客网刷算法题的输入输出(C++)

    内容简述 该篇文章将对牛客网刷题中关于输入输出的一些问题作一个总结.每年互联网公司的招聘都必不可少会有算法题,因此平时很多人都会去一些刷题网站进行刷题来学习.这里面用的比较多的刷题网站是leetcod ...

最新文章

  1. 极速理解设计模式系列:11.单例模式(Singleton Pattern)
  2. C语言文件操作(二)对指定txt文件中的N个数排序
  3. LRN和Batch Norm
  4. ROS 2 index翻译(五)——关于ROS 2客户端库
  5. 95-140-102-源码-transform-算子Map
  6. json在java中的使用_有效地使用JSON流(在Java中)
  7. java httpclient 重定向_httpclient 中post请求重定向
  8. 面向对象中多个对象之间的关系
  9. 2014520420145212信息安全系统实验三报告
  10. 2019蓝桥杯省赛心得
  11. MTK 手机支持3D
  12. checkbox不全部选中,会跳出提示(这个是我记录的重点)
  13. maven-repository文件
  14. 人工智能数学基础:费马引理、罗尔定理、拉格朗日微分中值定理、柯西中值定理
  15. 一款多核架构GPU IP有多达33种配置!Imagination要守住移动市场攻向云端
  16. win server 2016 无法安转.net framework 3.5 问题
  17. html大作业网页代码 ——2019凡客服装店铺商城(1页) HTML+CSS+JavaScript HTML+CSS大作业_ 服装店铺网页制作作业_购物网页设计...
  18. 微信小程序之实现到商品列表跳转商品详情页
  19. Flink-----Flink CDC 实现数据实时同步
  20. Transformer——patch embedding代码

热门文章

  1. jpa 默认生成sql语句_springboot-jpa自动创建数据库表
  2. ORACLE数据加载加本,使用oracle sqlldr加载数据
  3. python的文件操作os_python文件、文件夹操作OS模块
  4. vba cad 获取宏的路径_VBA批量创建文件目录及链接,建议收藏备用
  5. 百度广告点击软件_结束了,百度 “毒瘤” 广告!
  6. mysql 10分钟一聚合_SQL-根据DateTime查询多个聚合-MySQL
  7. python迅雷_迅雷下载链接解析器。
  8. loadrunner录制事件为0_测试工具LoadRunner常见问题汇总,解决方案整理
  9. 算法设计与分析——递归与分治策略——棋盘覆盖
  10. linux oracle流复制,oracle 流复制