dinic,63pts求助
查看原帖
dinic,63pts求助
259300
hy233楼主2022/3/8 22:22
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int rd()
{
	int x=0;
	bool f=1;
	char ch=getchar();
	for(;ch>'9'||ch<'0';ch=getchar())
		if(ch=='-') f=0;
	for(;ch>='0'&&ch<='9';ch=getchar())
		x=x*10+ch-48;
	return f?x:-x;
}
const int INF =(1<<30);
int from[100005],to[100005],val[100005],nxt[100005];
bool isx[100005];
int hd[1005];
int dep[1005];
int cnt=1;
void add(int u,int v,int w)
{
	nxt[++cnt]=hd[u];
	hd[u]=cnt;
	from[cnt]=u;
	to[cnt]=v;
	val[cnt]=w;
	nxt[++cnt]=hd[v];
	hd[v]=cnt;
	from[cnt]=v;
	to[cnt]=u;
	val[cnt]=w;
}
int n,m;
int s,t;
bool bfs()
{
	memset(dep,0,sizeof(dep));
	queue<int> q;
	q.push(s);
	dep[s]=1;
	while(q.size())
	{
		int u=q.front();
		q.pop();
		for(int i=hd[u];i;i=nxt[i])
		{
			int v=to[i];
			if(!val[i]||dep[v]) continue;
			dep[v]=dep[u]+1;
			q.push(v);
		}
	}
	return dep[t];
}
int dfs(int u,int in)
{
	int out=0;
	if(u==t)
		return in;
	for(int i=hd[u];i&&in;i=nxt[i])
	{
		int v=to[i];
		if(!val[i]||dep[v]!=dep[u]+1) continue;
		int flow=dfs(v,min(in,val[i]));
		in-=flow;
		out+=flow;
		val[i]-=flow;
		val[i^1]+=flow;
	}
	if(in==0) dep[u]=0;
	return out;
}
int main()
{
	m=rd(),n=rd();
	s=m+n+1;
	t=s+1;
	int u=rd(),v=rd();
	while(u!=-1&&v!=-1)
		add(u,v,1),u=rd(),v=rd();
	for(int i=1;i<=m;i++)
		add(s,i,1);
	for(int i=m+1;i<=n;i++)
		add(i,t,1);
	int ans=0;
	while(bfs()) ans+=dfs(s,INF);
	cout<<ans<<endl;
	for(int i=2;i<=cnt;i+=2)
		if(!val[i]&&from[i]!=s&&from[i]!=s&&to[i]!=t&&to[i]!=t)
			cout<<from[i]<<' '<<to[i]<<endl;
	return 0;
}
2022/3/8 22:22
加载中...