正题
题目链接:https://www.luogu.com.cn/problem/P3329
题目大意
nnn个点mmm条边的无向图,每次询问一个xxx表示最小割不超过xxx的点对数量。
解题思路
我们对于两个点sss到ttt完成网络流后的残量网络上,与sss联通的点属于点集SSS,与ttt联通的属于点集TTT。那么S−>TS->TS−>T的任意点对最小割都为s−>ts->ts−>t的最小割。
我们根据这个性质来建立最小割树,每次让sss与ttt连接一条边权为www的边然后递归下去分开处理SSS和TTT。
这样满足两点之间的最小割就是他们最小割树路径上的最小边权。
然后排序加并查集处理询问即可。
codecodecode
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define ll long long
using namespace std;
const ll N=210,inf=1e18;
struct edge{ll x,y,w;
}e[N],q[N];
ll n,m,T,tot,fa[N],siz[N],ans[N],p[N],dep[N];
namespace net{struct node{ll to,next,w,ret;}a[3100*4];ll tot,s,t,ls[N];queue<int> q;void init(){memset(ls,0,sizeof(ls));tot=1;return;}void addl(ll x,ll y,ll w){a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;a[tot].w=a[tot].ret=w;a[++tot].to=x;a[tot].next=ls[y];ls[y]=tot;a[tot].w=a[tot].ret=w;return;}bool bfs(){memset(dep,0,sizeof(dep));dep[s]=1;while(!q.empty())q.pop();q.push(s);while(!q.empty()){ll x=q.front();q.pop();for(ll i=ls[x];i;i=a[i].next){ll y=a[i].to;if(dep[y]||!a[i].w)continue;dep[y]=dep[x]+1;if(y==t)return 1;q.push(y);}}return 0;}ll dinic(ll x,ll flow){if(x==t)return flow;ll rest=0,k;for(ll i=ls[x];i;i=a[i].next){ll y=a[i].to;if(dep[x]+1!=dep[y]||!a[i].w)continue;rest+=(k=dinic(a[i].to,min(flow-rest,a[i].w)));a[i].w-=k;a[i^1].w+=k;if(rest==flow)return flow;}if(!rest)dep[x]=0;return rest;}ll solve(ll S,ll T){s=S;t=T;ll ans=0;while(bfs())ans+=dinic(s,inf);return ans;}void rcy(){for(ll i=2;i<=tot;i++)a[i].w=a[i].ret;}
}
void addl(ll x,ll y,ll w)
{e[++tot].x=x;e[tot].y=y;e[tot].w=w;return;}
bool cmp(ll x,ll y)
{return dep[x]<dep[y];}
bool cMp(edge x,edge y)
{return x.w>y.w;}
void solve(ll l,ll r){if(l==r)return;net::rcy();ll w=net::solve(p[l],p[r]);addl(p[l],p[r],w);sort(p+l,p+r+1,cmp);ll mid;for(ll i=l;i<=r;i++)if(dep[p[i]]){mid=i;break;}solve(l,mid-1);solve(mid,r);return;
}
ll find(ll x)
{return (fa[x]==x)?x:(fa[x]=find(fa[x]));}
int main()
{scanf("%lld",&T);while(T--){scanf("%lld%lld",&n,&m);net::init();tot=0;for(ll i=1;i<=m;i++){ll x,y,w;scanf("%lld%lld%lld",&x,&y,&w);net::addl(x,y,w);}for(ll i=1;i<=n;i++)p[i]=fa[i]=i,siz[i]=1;solve(1,n);sort(e+1,e+1+tot,cMp);scanf("%lld",&m);for(ll i=1;i<=m;i++){scanf("%lld",&q[i].w);q[i].x=i;}sort(q+1,q+1+m,cMp);ll l=1,sum=0;for(ll i=1;i<=m;i++){while(l<=tot&&e[l].w>q[i].w){ll x=e[l].x,y=e[l].y;x=find(x);y=find(y);fa[y]=x;sum+=siz[x]*siz[y];siz[x]+=siz[y];l++;}ans[q[i].x]=n*(n-1)/2-sum;}for(ll i=1;i<=m;i++)printf("%lld\n",ans[i]);putchar('\n');}
}