POJ3585 Accumulation Degree(二次扫描与换根法)
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)=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.
Input
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.
Output
For each test case, output the result on a single line.
Sample Input
1 5 1 2 11 1 4 13 3 4 5 4 5 10
Sample Output
26
基本是按照蓝书讲的写的,具体看代码注释。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define N 200005 using namespace std; int n; int tot=0,head[N],ver[2*N],edge[2*N],Next[2*N],f[N],d[N],dig[N];//d[i]表示以i为原点,从i出发流向子树的流量最大值 f[i]表示以i为根流向整个水系的最大值 inline void add(int x,int y,int z) { ver[++tot]=y,edge[tot]=z,Next[tot]=head[x],head[x]=tot; } inline void dp(int x,int pre) { d[x]=0; int i; for(i=head[x];i;i=Next[i]) { int y=ver[i],z=edge[i]; if(y==pre)continue; dp(y,x); if(dig[y]==1) d[x]+=z;//x是叶子节点 else d[x]+=min(z,d[y]);//木桶原理 } } inline void dfs(int x,int pre) { int i; for(i=head[x];i;i=Next[i]) { int y=ver[i],z=edge[i]; if(y==pre)continue; //不要在这里dfs 因为d[y]已经求出来了 这和之前的树背包不太一样 这个题是用已知的f[x]去求未知的f[y],是根像叶子结点的更新,而且是先更新再递归 if(dig[x]==1)f[y]=d[y]+z;//以y为原点的话x直接是入海口,所以直接加即可 else f[y]=d[y]+min(z,f[x]-min(z,d[y])); dfs(y,x); } } signed main() { int t;cin>>t; while(t--) { tot=0; memset(f,0,sizeof(f));//-1表示没有正确求出 memset(d,0,sizeof(d)); memset(head,0,sizeof(head)); memset(ver,0,sizeof(ver)); memset(edge,0,sizeof(edge)); memset(Next,0,sizeof(Next)); memset(dig,0,sizeof(dig)); cin>>n; int i; for(i=1;i<=n-1;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); dig[x]++,dig[y]++; } dp(1,0); f[1]=d[1]; dfs(1,0); int ans=0; for(i=1;i<=n;i++)ans=max(ans,f[i]); cout<<ans<<endl; } return 0; }
还是记得开二倍数组大小!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!