bzoj2229: [Zjoi2011]最小割(最小割树)

时间:2023-11-26 12:48:32

传送门

这题是用最小割树做的(不明白最小割树是什么的可以去看看这一题->这里

有了最小割树就很简单了……点数那么少……每次跑出一个最大流就暴力搞一遍就好了

 //minamoto
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,:;}
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
char sr[<<],z[];int C=-,Z;
inline void Ot(){fwrite(sr,,C+,stdout),C=-;}
inline void print(int x){
if(C><<)Ot();if(x<)sr[++C]=,x=-x;
while(z[++Z]=x%+,x/=);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=,M=;
int head[N],Next[M],ver[M],edge[M],ee[M],tot=;
queue<int> q;
inline void add(int u,int v,int e){
ver[++tot]=v,Next[tot]=head[u],head[u]=tot,edge[tot]=e,ee[tot]=e;
ver[++tot]=u,Next[tot]=head[v],head[v]=tot,edge[tot]=e,ee[tot]=e;
}
int cur[N],dep[N],n,m,S,T;
bool bfs(){
memset(dep,-,sizeof(dep));
for(int i=;i<=n;++i) cur[i]=head[i];
while(!q.empty()) q.pop();
q.push(S),dep[S]=;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=Next[i])
if(dep[ver[i]]<&&edge[i]){
q.push(ver[i]),dep[ver[i]]=dep[u]+;
if(ver[i]==T) return true;
}
}
return false;
}
int dfs(int u,int limit){
if(u==T||!limit) return limit;
int flow=,f;
for(int i=cur[u];i;cur[u]=i=Next[i]){
int v=ver[i];
if(dep[v]==dep[u]+&&(f=dfs(v,min(limit,edge[i])))){
flow+=f,limit-=f;
edge[i]-=f,edge[i^]+=f;
if(!limit) break;
}
}
if(!flow) dep[u]=-;
return flow;
}
int dinic(){
int flow=;
while(bfs()) flow+=dfs(S,inf);
return flow;
}
int id[N],tmp[N],ans[N][N];
void solve(int L,int R){
if(L==R) return;
for(int i=;i<=tot;i+=) edge[i]=edge[i^]=ee[i];
S=id[L],T=id[R];
int flow=dinic();
for(int i=;i<=n;++i)
if(~dep[i])
for(int j=;j<=n;++j)
if(dep[j]<)
cmin(ans[i][j],flow),cmin(ans[j][i],flow);
int l=L,r=R;
for(int i=L;i<=R;++i)
if(~dep[id[i]]) tmp[l++]=id[i];
else tmp[r--]=id[i];
memcpy(id+L,tmp+L,sizeof(int)*(R-L+));
solve(L,l-),solve(r+,R);
}
inline void init(){
memset(head,,sizeof(head)),tot=,memset(ans,0x3f,sizeof(ans));
}
int main(){
//freopen("testdata.in","r",stdin);
int T=read();
while(T--){
init();
n=read(),m=read();
for(int i=;i<=m;++i){
int u=read(),v=read(),e=read();add(u,v,e);
}
for(int i=;i<=n;++i) id[i]=i;
solve(,n);
int q=read();
while(q--){
int k=read(),res=;
for(int i=;i<=n;++i)
for(int j=i+;j<=n;++j)
if(ans[i][j]<=k) ++res;
print(res);
}
sr[++C]='\n';
}
Ot();
return ;
}