BZOJ 4390 Max Flow
同运输计划。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxv 50050
#define maxe 100500
using namespace std;
struct edge
{
int v,nxt;
}e[maxe];
int n,k,x,y,val[maxv],anc[maxv][],ans=,dis[maxv],g[maxv],nume=;
void addedge(int u,int v)
{
e[++nume].v=v;
e[nume].nxt=g[u];
g[u]=nume;
}
void dfs1(int x)
{
for (int i=g[x];i;i=e[i].nxt)
{
int v=e[i].v;
if (v!=anc[x][])
{
anc[v][]=x;dis[v]=dis[x]+;
dfs1(v);
}
}
}
void get_table()
{
for (int e=;e<=;e++)
for (int i=;i<=n;i++)
anc[i][e]=anc[anc[i][e-]][e-];
}
int lca(int x,int y)
{
if (dis[x]<dis[y]) swap(x,y);
for (int e=;e>=;e--)
{
if ((dis[anc[x][e]]>=dis[y]) && (anc[x][e]))
x=anc[x][e];
}
if (x==y) return x;
for (int e=;e>=;e--)
{
if (anc[x][e]!=anc[y][e])
{
x=anc[x][e];
y=anc[y][e];
}
}
return anc[x][];
}
void dfs2(int x)
{
for (int i=g[x];i;i=e[i].nxt)
{
int v=e[i].v;
if (v!=anc[x][])
{
dfs2(v);
val[x]+=val[v];
}
}
ans=max(ans,val[x]);
}
int main()
{
scanf("%d%d",&n,&k);
for (int i=;i<=n-;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);addedge(y,x);
}
dfs1();get_table();
for (int i=;i<=k;i++)
{
scanf("%d%d",&x,&y);
int t=lca(x,y);
val[x]++;val[y]++;val[t]--;val[anc[t][]]--;
}
dfs2();
printf("%d\n",ans);
return ;
}
BZOJ 4390 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 ...
- Max Flow
Max Flow 题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N st ...
- 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 ...
随机推荐
- [redis] 分布式 Redis 的 CRUD 实现
1.applicationContext-redis.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- JavaWeb基础:Servlet Request
Request获取请求头和请求数据 Request实现请求转发和MVC Request实现页面包含和Refresh
- C类型
类型 32位 64位 char 1 1 short 2 2 int 4 4 long 4 8 指针 4 8 float 4 4 double 8 8 long 8 8 常用的基本 ...
- HDU-------(2795)Billboard(线段树区间更新)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- ABAP-smartform-一页3列的条码打印
1.自建barcode: se73->System bar code->change->create->new input the name of the bar code y ...
- DataProcessing
clear load X4058 [m,n]=size(X528); Mean=zeros(1,n); Dev=zeros(1,n); for i=1:n Xi=X528(1:end-1,i); Xi ...
- GridView72变 控件
快速预览:GridView 无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单 DropDownList结合GridView和CheckBox结 ...
- LInux软件的卸载和安装(转)
在linux环境中,尤其是cenos中安装过一些软件,一般是二进制安装与源码安装,现小结一下linux中的安装与卸载. 一.通常Linux应用软件的安装包有三种: 1) tar包,如software- ...
- mark 一下
Android资源管理框架(Asset Manager)简要介绍和学习计划 http://www.cnblogs.com/hjtdlx/p/4332060.html
- HtmlHelper—DropDownList:SelectList、SelectListItem
前言 在项目中经常使用到DropDownList来显示数据库中的数据,典型的例子为为某书籍选择所属类型. 使用SelectList来实现: 实现一: Controller 代码 SelectList ...