题目描述

Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

FJ is pumping milk between  pairs of stalls (). For the th such pair, you are told two stalls  and , endpoints of a path along which milk is being pumped at a unit rate. FJ is concerned that some stalls might end up overwhelmed with all the milk being pumped through them, since a stall can serve as a waypoint along many of the paths along which milk is being pumped. Please help him determine the maximum amount of milk being pumped through any stall. If milk is being pumped along a path from  to , then it counts as being pumped through the endpoint stalls  and

, as well as through every stall along the path between them.

FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N。所有隔间都被管道连通了。

FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。

输入输出格式

输入格式:

The first line of the input contains  and .

The next  lines each contain two integers  and  () describing a pipe

between stalls  and .

The next  lines each contain two integers  and  describing the endpoint

stalls of a path through which milk is being pumped.

输出格式:

An integer specifying the maximum amount of milk pumped through any stall in the

barn.

输入输出样例

输入样例#1:

5 10
3 4
1 5
4 2
5 4
5 4
5 4
3 5
4 3
4 3
1 3
3 5
5 4
1 5
3 4

输出样例#1:

9

树上差分膜版题浪费代码长度
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #include<vector>
  5 using namespace std;
  6 #define inf 0x3f3f3f3f
  7
  8 inline int read(){
  9     int re=0;
 10     char ch;
 11     bool flag=0;
 12     while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
 13     ch=='-'?flag=1:re=ch-'0';
 14     while((ch=getchar())>='0'&&ch<='9')  re=(re<<1)+(re<<3)+ch-'0';
 15     return flag?-re:re;
 16 }
 17
 18 struct edge{
 19     int to,next;
 20     edge(int to=0,int next=0):
 21         to(to),next(next){}
 22 };
 23
 24 struct ask{
 25     int ss,tt,lca;
 26     ask(int ss=0,int tt=0,int lca=0):
 27         ss(ss),tt(tt),lca(lca){}
 28 };
 29
 30 const int maxn=50001;
 31
 32 vector<edge> edges;
 33 vector<edge> tree;
 34 vector<edge> ques;
 35 vector<ask> qu;
 36 int n,q,cnt,root=1,ans=-inf;
 37 int head[maxn],tmp_head[maxn],had[maxn],fat[maxn];
 38 int par[maxn],sum[maxn];
 39 bool vis[maxn];
 40
 41 inline void add_edge(int from,int to){
 42     edges.push_back(edge(to,head[from]));
 43     head[from]=++cnt;
 44     edges.push_back(edge(from,head[to]));
 45     head[to]=++cnt;
 46 }
 47
 48 inline void add_tree(int from,int to){
 49     tree.push_back(edge(to,tmp_head[from]));
 50     tmp_head[from]=++cnt;
 51 }
 52
 53 void make_tree(int x,int fa){
 54     fat[x]=fa;
 55     for(int ee=head[x];ee;ee=edges[ee].next)
 56         if(edges[ee].to!=fa){
 57             add_tree(x,edges[ee].to);
 58             make_tree(edges[ee].to,x);
 59         }
 60 }
 61
 62 inline void add_ques(int ss,int tt){
 63     ques.push_back(edge(tt,had[ss]));
 64     had[ss]=++cnt;
 65     ques.push_back(edge(ss,had[tt]));
 66     had[tt]=++cnt;
 67 }
 68
 69 void init(){
 70     n=read();  q=read();
 71     edges.push_back(edge(0,0));
 72     cnt=0;
 73     for(int i=1;i<n;i++){
 74         int from=read(),to=read();
 75         add_edge(from,to);
 76     }
 77
 78     cnt=0;
 79     tree.push_back(edge(0,0));
 80     make_tree(root,0);
 81     swap(head,tmp_head);
 82
 83     cnt=0;
 84     ques.push_back(edge(0,0));
 85     for(int i=0;i<q;i++){
 86         int ss=read(),tt=read();
 87         qu.push_back(ask(ss,tt,0));
 88         add_ques(ss,tt);
 89     }
 90 }
 91
 92 int find(int x){
 93     return par[x]==x?x:par[x]=find(par[x]);
 94 }
 95
 96 void tarjan(int x){
 97     for(int ee=head[x];ee;ee=tree[ee].next){
 98         tarjan(tree[ee].to);
 99         par[tree[ee].to]=x;
100         vis[tree[ee].to]=1;
101     }
102     for(int ee=had[x];ee;ee=ques[ee].next)
103         if(vis[ques[ee].to])
104             qu[(ee-1)>>1].lca=find(ques[ee].to);
105 }
106
107 void dfs_sum(int x){
108     for(int ee=head[x];ee;ee=tree[ee].next){
109         dfs_sum(tree[ee].to);
110         sum[x]+=sum[tree[ee].to];
111     }
112 }
113
114 void solve(){
115     for(int i=1;i<=n;i++)  par[i]=i;
116     vis[root]=1;
117     tarjan(root);
118
119     for(int i=0;i<q;i++){
120         ask qq=qu[i];
121         sum[qq.ss]++;
122         sum[qq.tt]++;
123         sum[qq.lca]--;
124         sum[fat[qq.lca]]--;
125     }
126     dfs_sum(root);
127
128     for(int i=1;i<=n;i++)
129         ans=max(ans,sum[i]);
130     printf("%d\n",ans);
131 }
132
133 int main(){
134     //freopen("temp.in","r",stdin);
135     init();
136     solve();
137     return 0;
138 }


Goodbye, my almost lover
再见了,我无缘的爱人
Goodbye, my hopeless dream
再见了,我无望的梦想

转载于:https://www.cnblogs.com/ZYBGMZL/p/6899125.html

[luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]相关推荐

  1. P3128 [USACO15DEC]Max Flow P

    P3128 [USACO15DEC]Max Flow P 树上差分之点差分模板题 题目描述: FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N.所有隔间都被管道 ...

  2. P3128-最大流Max Flow【树上差分,LCA】

    正题 题目大意 一棵树 若干条路径,哪个点经过的路径最多,求路径条数. 解题思路 对于每条路径计算一次LCALCALCA,然后树上差分就好了. codecodecode #include<cst ...

  3. 【bzoj 4390】 [Usaco2015 dec]Max Flow(树上差分)

    4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 156  Solved: 100 [Sub ...

  4. [LUOGU] P3128 [USACO15DEC]最大流Max Flow

    题意:一棵树,多次给指定链上的节点加1,问最大节点权值 n个点,n-1条边很容易惯性想成一条链,幸好有样例.. 简单的树剖即可!(划去) 正常思路是树上差分,毕竟它就询问一次.. #include&l ...

  5. P2680-运输计划【LCA,树上差分,二分答案】

    正题 题目链接:https://www.luogu.org/problemnew/show/P2680 题目大意 一棵带权无根树,给出若干条路径.选择一条边使其边权变为0,要求路径的长度的最大值最小. ...

  6. 洛谷P2680 运输计划(倍增LCA + 树上差分 + 二分答案)

    [题目链接] [思路]: 根据题意可以明显看出,当所有任务都完成时的时间是最终的结果,也就是说本题要求,求出最小的最大值. 那这样的话就暗示了将答案二分,进行check. [check方法]: 如果说 ...

  7. 【HDU - 5452】Minimum Cut(树形dp 或 最近公共祖先lca+树上差分,转化tricks,思维)

    题干: Given a simple unweighted graph GG (an undirected graph containing no loops nor multiple edges) ...

  8. HDU - 5452 Minimum Cut(LCA+树上差分)

    题目链接:点击查看 题目大意:给出n个点,n-1条边组成一棵树,然后再给出m-n-1条边,组成一个图,现在要让我们求最少删去几条边才能让整个图不连通,并且要求只能在树上删去最多一条边 题目分析:这个题 ...

  9. [JLOI2014]松鼠的新家 倍增LCA+树上差分

    题目描述 题目 本来想写一道Tarjan的,结果发现这题倍增比较好写 这题主要要搞懂树上差分这东西(NOIP前这东西卡了我好久) 大概要注意的就是对于除了出发点以外的所有点都是重复算了的,所以最后要有 ...

最新文章

  1. HDFS的shell和API操作
  2. CentOS查看主板型号、CPU、显卡、硬盘等信息
  3. java 文件 加解密_Java实现文件的加密解密功能示例
  4. python入门作业编程题-Python编程:从入门到实践——【作业】——第六章(字典)...
  5. python抢红包脚本实例-这个Python脚本牛逼了,秒抢红包,再不怕错过一个亿了!...
  6. chap10 构建Web内容的技术
  7. python读取xlsx文件pandas_用Python的pandas框架操作Excel文件中的数据教程
  8. 为什么大学感觉学编程很难?原因有这三点。
  9. java jdbc jar包_大数据从入门到深入:JavaEE 之 数据库技术 JDBC(1)
  10. 循环队列 - 顺序存储结构
  11. java jpasswordfield_Java JPasswordField
  12. 13、三维图绘制及添加文本
  13. 为什么数字设计中经常使用 片选信号低电平有效,而不是高电平有效?
  14. java excelhandle oschina,基于alibab的easyexcel进行excel表的导出(可自定义handler去设计excel格式)...
  15. 红队笔记之痕迹清理技术要点与实战方法总结
  16. 西北计算机大赛奖金有多少,我校学生2019年中国大学生计算机设计大赛西北赛区决赛中获得佳绩...
  17. 让Boo成为头等语言的新尝试
  18. [Qt]图像处理小软件——给证件照换背景
  19. 如何进行不同容量硬盘对拷
  20. python文件打开的合法模式组合wr_Python Scapy wrpcap-如何将数据包附加到pcap文件?

热门文章

  1. Permission denied: make_sock: could not bind to address 端口问题解决
  2. Oracle ——概述 CBO 优化器
  3. WordPress免费精美主题分享系列之艺术风格篇
  4. A+B and C (64bit)
  5. 7-30 字符串的冒泡排序 (20 分) or 7-27 冒泡法排序 (20 分)
  6. bandizip专业版
  7. 公式中*和· 号的含义区分(GRU公式)
  8. SAP License:雾里看花系列——做管理还要懂SAP吗?
  9. Python入门学习笔记(9)
  10. C++中的多重继承(二)