BZOJ 4390 Max Flow

时间:2022-07-04 23:32:02

同运输计划。

#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 ;
}