网站建设的技术目标,现代化专业群建设网站,设计方案格式模板,中国建设银行官网站网点文章目录题目描述样例分析#xff1a;题意#xff1a;题解#xff1a;代码#xff1a;本题目传送题目树学是这个题的简易版#xff0c;也涉及换根问题#xff0c;可以先看看这个 树学 时间限制#xff1a;C/C 1秒#xff0c;其他语言2秒 空间限制#xff1a;C/C 32768…
文章目录题目描述样例分析题意题解代码本题目传送题目树学是这个题的简易版也涉及换根问题可以先看看这个 树学 时间限制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:
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)115824 Details: 1-2 11 1-4-3 5 1-4-5 8(since 1-4 has capacity of 13) A(2)5611 Details: 2-1-4-3 5 2-1-4-5 6 A(3)5 Details: 3-4-5 5 A(4)1151026 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点的流量 一个点的流量是怎么来的如果jj是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的流量来自子树1x
我们发现换根后x的流量就没有了y的部分其他都还在此时x的流量就是原本的减去从y流向x的部分new[x]flow[ x ] - min ( flow[ y ] , edge[ x ] [ y ] ),这个new表示x新的流量
我们再看yy的流量多了从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的边权值也就是上面讲的式子使用条件是minxyx和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;cint;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;
}