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 ...
随机推荐
- JavaWeb基础: Tomcat
Tomcat目录层次结构 Tomcat本质上是一个Java应用,要启动Tomcat服务需要安装JRE并配置JRE的Path,才能通过Tomcat_HOME/bin/startup脚本正常启动Tomca ...
- SAP资产变动明细
*&---------------------------------------------------------------------* *& Report ZFIR027 * ...
- 【堆栈应用一】一个数divided=几个最小质因数的乘积
/******************************************堆栈:一个数divided几个质因数(质因数的乘积为N)***************************** ...
- ocument的createDocumentFragment()方法
在<javascript高级程序设计>一书的6.3.5:创建和操作节点一节中,介绍了几种动态创建html节点的方法,其中有以下几种常见方法: · crateAttribute(name): ...
- shell脚本流程搭建
程序中删除文件夹即用rm -rf ${Dir} rm -f ${Dir}/* rm -f ${Dir}* 的时候要注意先检查路径变量是否有定义不为空. if [ ! -z ${LogDir} ]; t ...
- 如何解决linQ“序列不包含任何元素”的问题?
描述:该问题出现在校对BT种子数据的时候遇到的bug,原因是使用linq查找元素的时候 B是A的一个子集, B在A中一定存在,这种情况下就不会抛出异常情况,反之B的一部分不属于A就会异常应为B中的一个 ...
- 在现有 SharePoint 服务器上安装 PowerPivot for SharePoint
步骤1: 检查 SQL Server 2008 R2 Analysis Services 实例的“程序”文件夹.如果您找到了现有安装或之前安装的证据,则执行剩余步骤.否则,直接执行步骤 2:安装 Po ...
- treap 1296 营业额统计
有一个点答案错误,求大神指教 #include<cstdio>#include<iostream>#include<cstdlib>#include<ctim ...
- POJ 3225 Help with Intervals
U:把区间[l,r]覆盖成1I:把[0,l-1][r+1,∞]覆盖成0D:把区间[l,r]覆盖成0C:把[0,l-1][r+1,∞]覆盖成0 , 且[l,r]区间0/1互换(即异或)S:[l,r]区间 ...
- 【NOIP2015】提高day2解题报告
题目: P1981跳石头 描述 一年一度的“跳石头”比赛又要开始了!这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 N ...