牛客网【每日一题】4月13号 Accumulation Degree

文章目录

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

本题目传送

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

时间限制: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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/320386.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【二分】Distinct

Distinct 题目大意&#xff1a; 有n个军队&#xff08;有自己在x轴上的坐标&#xff09;&#xff0c;每个军队有一定的人&#xff0c;要一个坐标只有一个人&#xff0c;移动路程最大的士兵最少移动多长 原题&#xff1a; 题目描述 Daniel 正在玩一个战棋游戏。 现在 Danie…

P2839-[国家集训队]middle【主席树,二分】

正题 题目链接:https://www.luogu.com.cn/problem/P2839 题目大意 nnn个数字&#xff0c;mmm次询问给出(a,b,c,d)(a,b,c,d)(a,b,c,d)表示左端点在[a,b][a,b][a,b]中&#xff0c;右端点在[c,d][c,d][c,d]的子区间中中位数最大的值。 解题思路 显然我们需要二分一下答案midmidm…

微软把UWP定位成业务线应用程序开发平台

微软把UWP定位成传统业务线&#xff08;LOB&#xff09;应用程序开发平台&#xff0c;以使用Windows Template Studio实现快速应用程序开发为重点。但是&#xff0c;为了把LOB开发人员吸引到UWP平台&#xff0c;他们在做的事情不止这些。最初发布时&#xff0c;通用Windows平台…

牛客网【每日一题】 合集

文章目录2020年3月25日 NC50439 tokitsukaze and Soldier 牛客练习赛50-C2020年3月26日 NC13230 合并回文子串 美团2017年CodeM大赛-初赛A轮2020年3月27日 NC15553 数学考试 2018年长沙理工大学程序设计竞赛2020年3月30日 NC50528 滑动窗口 《信息学奥赛一本通》Part5.52020年3…

NBA总冠军

NBA总冠军NBA总冠军NBA总冠军 题目描述 又要考试了&#xff0c;LJW决定放松一下&#xff0c;就打开电视&#xff0c;看见篮球赛&#xff0c;他立即想到了每年的NBA总冠军队伍。由于复习紧张&#xff0c;他只记起了一部分&#xff0c;记忆的内容是正确的&#xff0c;可能不是按…

P6773-[NOI2020]命运【线段树合并,树形dp】

正题 题目链接:https://www.luogu.com.cn/problem/P6773 题目大意 nnn个点的一棵树&#xff0c;边权可以是000或111。mmm个条件(x,y)(x,y)(x,y)表示要求x,yx,yx,y之间要有边权值为111&#xff08;保证xxx是yyy的祖先&#xff09;&#xff0c;求方案数。 解题思路 考虑容斥&am…

读 《CSharp Coding Guidelines》有感

C# 编程指南前不久在 Github 上看见了一位大牛创建一个仓库&#xff1a;CSharpCodingGuidelines&#xff0c;打开之后看了一下 readme.md 相关描述&#xff0c;感觉应该很不错&#xff0c;于是就 clone 到本地拜读一下&#xff0c;这里列一些自己的笔记&#xff0c;方便日后回顾…

牛客网 【每日一题】4月10日 二分图染色(弱化版)

精讲 组合、容斥 文章目录题目&#xff1a;题意&&题解&#xff1a;&#xff1a;代码&#xff1a;题目传送题目&#xff1a; 时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C 524288K&#xff0c;其他语言1048576K 64bit IO Format: %lld …

数字游戏(水博客ing / csp-J T1 / luogu 5660)

数字游戏 luogu 5660 题目大意&#xff1a; 给你一个长度为8的01串&#xff0c;分别表示你某一场比赛是否AKAKAK了&#xff0c;问你AKAKAK了几场比赛 输入样例#1 00010100输出样例#1 2输入样例#2 11111111输出样例#2 8样例解释#1 该 01 字符串中有 2 个字符 1。 样例…

P2231-[HNOI2002]跳蚤【容斥】

正题 题目链接:https://www.luogu.com.cn/problem/P2231 题目大意 求一个由[1,m][1,m][1,m]的整数组成的长度为nnn的序列使得他们的gcdgcdgcd和mmm互质。 解题思路 考虑容斥减去不合法的答案。那就是要求序列的gcdgcdgcd和mmm不互质的个数&#xff0c;那么我们依旧需要容斥计…

微软Windows Community Toolkit一览

为了满足业务线开发人员的需求&#xff0c;微软推出了Windows Community Toolkit。这个快速变化的库充当了新的UWP控件和功能的测试基础。在创建UWP之初&#xff0c;其重点目标是智能手机和平板电脑。这意味着大部分开发预算都花费在控件上&#xff0c;确保这些控件能够在有限的…

牛客网【每日一题】4月14日题目精讲 Xorto

文章目录题目描述题解&#xff1a;代码&#xff1a;扩展传送时间限制&#xff1a;C/C 2秒&#xff0c;其他语言4秒 空间限制&#xff1a;C/C 32768K&#xff0c;其他语言65536K 64bit IO Format:%lld 题目描述 给定一个长度为n的整数数组&#xff0c;问有多少对互不重叠的非空区…

YbtOJ#20081-[NOIP2020模拟赛B组Day8]树上排列【组合数,树形dp】

正题 题面链接:https://www.ybtoj.com.cn/contest/62/problem/3 题目大意 nnn个点的一棵树&#xff0c;每个边的边会表示一个大小关系&#xff08;如px>pyp_x>p_ypx​>py​或px<pyp_x<p_ypx​<py​&#xff09;。求有多少个排列满足所有条件。 解题思路 考…

如何简单的在 ASP.NET Core 中集成 JWT 认证?

前情提要&#xff1a;ASP.NET Core 使用 JWT 搭建分布式无状态身份验证系统文章超长预警&#xff08;1万字以上&#xff09;&#xff0c;不想看全部实现过程的同学可以直接跳转到末尾查看成果或者一键安装相关的 nuget 包自上一篇介绍如何在 ASP.NET Core 中集成 JWT 的博文发布…

【二分】【暴力】蛋糕(gmoj 3918)

蛋糕 gmoj 3918 题目大意&#xff1a; 有一个蛋糕&#xff0c;分成n∗mn*mn∗m个单位&#xff0c;现在横竖各切三刀&#xff0c;使其分成16个矩阵&#xff0c;使价值最小的矩阵价值最大 输出样例 5 5 95998 21945 23451 99798 74083输入样例 3数据范围 40%的数据&#x…

Music Problem

文章目录题目描述题意&#xff1a;题解&#xff1a;传送时间限制&#xff1a;C/C 2秒&#xff0c;其他语言4秒 空间限制&#xff1a;C/C 131072K&#xff0c;其他语言262144K 64bit IO Format: %lld 题目描述 Listening to the music is relax, but for obsessive(强迫症), it …

【DP】翻硬币(jzoj 3921)

翻硬币 jzoj 3921 题目大意&#xff1a; 给你一个长度为nnn的当前01串和目标01串&#xff0c;现在你要做mmm此操作&#xff0c;每次操作你要使kkk个不同的位取反&#xff0c;现在问你有多少种方法可以使当前01串变为目标01串 输入样例&#xff1a; 3 2 1 100 001输出样例&…

可扩展架构设计的三个维度

业界对于可扩展的系统架构设计有一个朴素的理念,就是&#xff1a;通过加机器就可以解决容量和可用性问题这一理念在“云计算”概念疯狂流行的今天&#xff0c;得到了广泛的认可&#xff01;对于一个规模迅速增长的系统而言&#xff0c;容量和性能问题当然是首当其冲的。但是随着…

YbtOJ#20082-[NOIP2020模拟赛B组Day8]导出子图【dp】

正题 题面链接:https://www.ybtoj.com.cn/contest/62/problem/4 题目大意 nnn个区间&#xff0c;如果第xxx个区间和第yyy个区间有交集那么xxx到yyy直接就有一条边。 求这张图上的所有导出子图中有多少棵树。 解题思路 条件可以转换为这些区间联通并且没有一个位置被333个区间…

.NET Core开发日志——简述路由

有过ASP.NET或其它现代Web框架开发经历的开发者对路由这一名字应该不陌生。如果要用一句话解释什么是路由&#xff0c;可以这样形容&#xff1a;通过对URL的解析&#xff0c;指定相应的处理程序。回忆下在Web Forms应用程序中使用路由的方式&#xff1a;然后是MVC应用程序&…