题目描述

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

树上差分膜版题
浪费代码长度
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f inline int read(){
int re=;
char ch;
bool flag=;
while((ch=getchar())!='-'&&(ch<''||ch>''));
ch=='-'?flag=:re=ch-'';
while((ch=getchar())>=''&&ch<='') re=(re<<)+(re<<)+ch-'';
return flag?-re:re;
} struct edge{
int to,next;
edge(int to=,int next=):
to(to),next(next){}
}; struct ask{
int ss,tt,lca;
ask(int ss=,int tt=,int lca=):
ss(ss),tt(tt),lca(lca){}
}; const int maxn=; vector<edge> edges;
vector<edge> tree;
vector<edge> ques;
vector<ask> qu;
int n,q,cnt,root=,ans=-inf;
int head[maxn],tmp_head[maxn],had[maxn],fat[maxn];
int par[maxn],sum[maxn];
bool vis[maxn]; inline void add_edge(int from,int to){
edges.push_back(edge(to,head[from]));
head[from]=++cnt;
edges.push_back(edge(from,head[to]));
head[to]=++cnt;
} inline void add_tree(int from,int to){
tree.push_back(edge(to,tmp_head[from]));
tmp_head[from]=++cnt;
} void make_tree(int x,int fa){
fat[x]=fa;
for(int ee=head[x];ee;ee=edges[ee].next)
if(edges[ee].to!=fa){
add_tree(x,edges[ee].to);
make_tree(edges[ee].to,x);
}
} inline void add_ques(int ss,int tt){
ques.push_back(edge(tt,had[ss]));
had[ss]=++cnt;
ques.push_back(edge(ss,had[tt]));
had[tt]=++cnt;
} void init(){
n=read(); q=read();
edges.push_back(edge(,));
cnt=;
for(int i=;i<n;i++){
int from=read(),to=read();
add_edge(from,to);
} cnt=;
tree.push_back(edge(,));
make_tree(root,);
swap(head,tmp_head); cnt=;
ques.push_back(edge(,));
for(int i=;i<q;i++){
int ss=read(),tt=read();
qu.push_back(ask(ss,tt,));
add_ques(ss,tt);
}
} int find(int x){
return par[x]==x?x:par[x]=find(par[x]);
} void tarjan(int x){
for(int ee=head[x];ee;ee=tree[ee].next){
tarjan(tree[ee].to);
par[tree[ee].to]=x;
vis[tree[ee].to]=;
}
for(int ee=had[x];ee;ee=ques[ee].next)
if(vis[ques[ee].to])
qu[(ee-)>>].lca=find(ques[ee].to);
} void dfs_sum(int x){
for(int ee=head[x];ee;ee=tree[ee].next){
dfs_sum(tree[ee].to);
sum[x]+=sum[tree[ee].to];
}
} void solve(){
for(int i=;i<=n;i++) par[i]=i;
vis[root]=;
tarjan(root); for(int i=;i<q;i++){
ask qq=qu[i];
sum[qq.ss]++;
sum[qq.tt]++;
sum[qq.lca]--;
sum[fat[qq.lca]]--;
}
dfs_sum(root); for(int i=;i<=n;i++)
ans=max(ans,sum[i]);
printf("%d\n",ans);
} int main(){
//freopen("temp.in","r",stdin);
init();
solve();
return ;
}

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

[luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]的更多相关文章

  1. bzoj4390: [Usaco2015 dec]Max Flow(LCA+树上差分)

    题目大意:给出一棵树,n(n<=5w)个节点,k(k<=10w)次修改,每次给定s和t,把s到t的路径上的点权+1,问k次操作后最大点权. 对于每次修改,给s和t的点权+1,给lca(s, ...

  2. [Luogu 3128] USACO15DEC Max Flow

    [Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...

  3. luoguP3128 [USACO15DEC]最大流Max Flow 题解(树上差分)

    链接一下题目:luoguP3128 [USACO15DEC]最大流Max Flow(树上差分板子题) 如果没有学过树上差分,抠这里(其实很简单的,真的):树上差分总结 学了树上差分,这道题就极其显然了 ...

  4. [USACO15DEC]最大流Max Flow(树上差分)

    题目描述: Farmer John has installed a new system of N−1N-1N−1 pipes to transport milk between the NNN st ...

  5. luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)

    题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...

  6. LuoguP3128 [USACO15DEC]最大流Max Flow (树上差分)

    跟LOJ10131暗的连锁 相似,只是对于\(lca\)节点把它和父亲减一 #include <cstdio> #include <iostream> #include < ...

  7. [BZOJ3307]:雨天的尾巴(LCA+树上差分+权值线段树)

    题目传送门 题目描述: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式: 第一 ...

  8. P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of  pipes to transport mil ...

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

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

随机推荐

  1. react-native学习(RN)--之Window环境下搭建环境配置

    react-native以后会更火的,自从2015年facebook开源了Android 一.安装java 二.安装Android Studio 三.安装react-native需要的Android ...

  2. 说说ajax上传数据和接收数据

    我是一个脑袋不太灵光的人,所以遇到问题,厚着脸皮去请教大神的时候,害怕被大神鄙视,但是还是被鄙视了.我说自己不要点脸面,那是不可能的,但是,为了能让自己的技术生涯能走的更长远一些,受点白眼,受点嘲笑也 ...

  3. Bash的作业控制

    作业控制是bash Shell提供的一项强大功能,它允许你选择在前台还是后台运行程序,即作业. 1.开启bash的作业控制功能 #set -o monitor或#set -m 2.显示在后台运行的作业 ...

  4. Jmeter-WINDOWS下的配置部署

    前提:已配置安装JDK环境及已部署TOMCAT. 解压apache-jmeter-2.9.zip文件至目录,我的是D:\Program Files目录. 点击我的电脑----属性----高级----环 ...

  5. 主要排序算法的Java实现

    最近温习算法,花点时间写了点代码验证下,在这里做个存档. package com.chrischeng.hacker; import java.util.*; /** * Created by Yex ...

  6. v9更新栏目缓存提示PHP has encountered a Stack overflow解决方法

    原因: 客户在把一些栏目删除或者新增栏目时没更新栏目缓存导致v9_category表里有原来的垃圾信息,多余的表. 解决方法:通过phpmyadmin找到栏目表出错的条目,修改错误信息. 具体步骤: ...

  7. Spring定时任务实例

    一.Quartz介绍 在企业应用中,我们经常会碰到时间任务调度的需求,比如每天凌晨生成前天报表,每小时生成一次汇总数据等等.Quartz是出了名的任务调度框架,它可以与J2SE和J2EE应用程序相结合 ...

  8. Nodejs的模块系统以及require的机制

    一.简介 Nodejs 有一个简单的模块加载系统.在 Nodejs 中,文件和模块是一一对应的(每个文件被视为一个独立的模块),这个文件可能是 JavaScript 代码,JSON 或者编译过的C/C ...

  9. Socket实现

    网络实现架构 4.4BSD通过同时对多种通信协议的支持来提供通用的底层基础服务.4.4BSD支持四种不同的通信协议簇: TCP/IP(互联网协议簇) XNS(Xerox网络系统) OSI协议 Unix ...

  10. 出错场景是升级oracle驱动,将版本从ojdbc14升级到ojdbc6,hibernate执行原生态sql语句会报如下错误

    出错场景是升级oracle驱动,将版本从ojdbc14升级到ojdbc6,hibernate执行原生态sql语句会报如下错误:org.hibernate.MappingException: No Di ...