Max Flow
Max Flow
题目描述
FJ is pumping milk between K pairs of stalls (1≤K≤100,000). For the ith such pair, you are told two stalls si and ti, 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 K 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 si to ti, then it counts as being pumped through the endpoint stalls si and ti, as well as through every stall along the path between them.
输入
The next N−1 lines each contain two integers x and y (x≠y) describing a pipe between stalls x and y.
The next K lines each contain two integers s and t describing the endpoint stalls of a path through which milk is being pumped.
输出
样例输入
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
样例输出
9
分析:Tarjan+差分思想;
难点在于怎么处理树上的区间加减问题;
把数列差分思想用到树上,区间[i~j]加上k,等价于a[i]+=k,a[j+1]-=k,ans[t]=Σa[p](1=<p<=t);
树上的s,t区间分成两个,设s,t公共祖先为p,一个是s到p的儿子,另一个是t到p,两个区间都差分一下;
最后dfs递归处理类似前缀和的答案;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,fa[maxn],ans[maxn],vis[maxn],faa[maxn],ma;
vi a[maxn],query[maxn];
int find(int x)
{
return faa[x]==x?x:faa[x]=find(faa[x]);
}
void dfs(int now,int pre)
{
for(int x:a[now])
{
if(x!=pre)
{
dfs(x,now);
fa[x]=now;
}
}
}
void dfs1(int now)
{
vis[now]=;
for(int x:query[now])
{
if(vis[x])
{
int p=find(x);
ans[p]--,ans[fa[p]]--;
}
}
for(int x:a[now])
{
if(!vis[x])
{
dfs1(x);
faa[x]=now;
}
}
}
void dfs2(int now,int pre)
{
for(int x:a[now])
{
if(x!=pre)
{
dfs2(x,now);
ans[now]+=ans[x];
}
}
ma=max(ma,ans[now]);
}
int main()
{
int i,j;
scanf("%d%d",&n,&k);
rep(i,,n)faa[i]=i;
rep(i,,n-)
{
scanf("%d%d",&j,&t);
a[j].pb(t),a[t].pb(j);
}
rep(i,,k)
{
scanf("%d%d",&j,&t);
ans[j]++,ans[t]++;
query[j].pb(t);
query[t].pb(j);
}
dfs(,-);
dfs1();
dfs2(,-);
printf("%d\n",ma);
//system("pause");
return ;
}
Max Flow的更多相关文章
- BZOJ 4390: [Usaco2015 dec]Max Flow
4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 113[Submi ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- HackerRank "Training the army" - Max Flow
First problem to learn Max Flow. Ford-Fulkerson is a group of algorithms - Dinic is one of it.It is ...
- min cost max flow算法示例
问题描述 给定g个group,n个id,n<=g.我们将为每个group分配一个id(各个group的id不同).但是每个group分配id需要付出不同的代价cost,需要求解最优的id分配方案 ...
- [Luogu 3128] USACO15DEC Max Flow
[Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...
- [Usaco2015 dec]Max Flow 树上差分
[Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 353 Solved: 236[Submit][Sta ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
- BZOJ4390: [Usaco2015 dec]Max Flow
BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...
- P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of pipes to transport mil ...
随机推荐
- Java Web项目发布及使用自定义域名
详细讲解的网址: http://wenku.baidu.com/link?url=-ACZxKUcfrbhrMRUP3Ov-Q_c-Q9JPrA9D1fzHjHuJsWwZfRsVDVQ2qBtoY7 ...
- 注意题目条件!!! 团问题 HDU 5952
题目大意:团的定义就是,团内的所有点,两两之间各有一条边,团的大小就是点的个数.现给你一个n个点,m条边的图.问,该图中有多少点的个数为s的团. (题目保证每个点的度数不超过20,n<=100, ...
- label不换行的问题
除了numberOfLines属性label有一个preferredMaXLayoutWidth属性.设置试试
- springmvc+mybatis下载项目自带模板
1.首先如果要获取javaweb项目中的文件在哪,用到的代码:request.getSession().getServletContext().getRealPath("/WEB-INF/d ...
- JPA 系列教程1-环境搭建
JPA JPA全称Java Persistence API. JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. Hibernate3.2+.Top ...
- 【转】ethtool 命令详解
命令描述: ethtool 是用于查询及设置网卡参数的命令. 使用概要:ethtool ethx //查询ethx网口基本设置,其中 x 是对应网卡的编号,如eth0.eth1等等etht ...
- 转载 Deep learning:四(logistic regression练习)
前言: 本节来练习下logistic regression相关内容,参考的资料为网页:http://openclassroom.stanford.edu/MainFolder/DocumentPage ...
- javascript语句语义大全(3)
1. for(var i=0;i<10;i++){ } for循环,括号里面是循环条件,翻译过来是,初始设定1=0:没循环一次i会+1,直到i<10 2. var i=0: while(i ...
- UVALive 2517 Moving Object Recognition(模拟)
题目看上去很吓人,很高端,但其实很简单,不要被吓到,照搬题目的公式就可以了. 方法:用BFS求出最大块和重心,找出题目公式需要的未知量,然后套到题目公式里就可以求出答案了. 代码: #include& ...
- 5--OC--构造方法
// Created by Stephen on 16/3/2.// Copyright © 2016年 Stephen. All rights reserved.//// 回顾上一章节Perso ...