45pts Dinic 求条
查看原帖
45pts Dinic 求条
826774
Little_Fox_Fairy楼主2024/9/15 16:52

用的是 vector 版本的 dinic,错的都是 RE。

#include<bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
namespace fast_IO {
#define IOSIZE 100000
	char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
	template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
	template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
	inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
	inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
	inline void print(char x) { putchar(x); }
	inline void print(char *x) { while (*x) putchar(*x++); }
	inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
	inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
	inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
	inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
	inline void print(bool b) { putchar(b+48); }
	template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
	template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
	struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
	template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
	template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
const int N=2e4+10;
const int inf=0x3f3f3f3f;
struct node {int v,w,idx;};
int n,m;
int S,T;
int dep[N],cur[N];
vector<node> e[N];
priority_queue<pii,vector<pii>,greater<pii> > s;
inline bool bfs()
{
	memset(dep,-1,sizeof dep);
	queue<int> q;
	q.push(S);
	dep[S]=0;
	while (!q.empty())
	{
		int u=q.front();q.pop();
		for (int i=0;i<e[u].size();i++)
		{
			int v=e[u][i].v;
			if (dep[v]==-1 and e[u][i].w)
			{
				dep[v]=dep[u]+1;
				q.push(v);
			}
		}
	}
	memset(cur,0,sizeof cur);
	return (dep[T]!=-1);
}
inline int dfs(int u,int limit)
{
	if (u==T) return limit;
	for (int i=cur[u];i<e[u].size();i++)
	{
		cur[u]=i;
		int v=e[u][i].v;
		if (dep[v]==dep[u]+1 and e[u][i].w)
		{
			int row=dfs(v,min(limit,e[u][i].w));
			if (row)
			{
				e[u][i].w-=row;
				e[v][e[u][i].idx].w+=row;
				s.push({u,v});
				return row;
			}
			else dep[v]=-1;
		}
	}
	return 0;
}
inline int Dinic()
{
	int ans=0,row;
	while (bfs()) while (row=dfs(S,inf)) ans+=row;
	return ans;
}
signed main()
{
	cin>>m>>n;S=n+1,T=n+2;
	while (1)
	{
		int u,v;cin>>u>>v;
		if (u==-1 and v==-1) break;
		int uid=e[u].size();
		int vid=e[v].size();
		e[u].emplace_back(node{v,inf,uid});
		e[v].emplace_back(node{u,0,vid});
	}
	for (int i=1;i<=m;i++)
	{
		int uid=e[S].size();
		int vid=e[i].size();
		e[S].emplace_back(node{i,1,uid});
		e[i].emplace_back(node{S,0,vid});
	}
	for (int i=m+1;i<=n;i++)
	{
		int uid=e[i].size();
		int vid=e[T].size();
		e[i].emplace_back(node{T,1,uid});
		e[T].emplace_back(node{i,0,vid});
	}
	cout<<Dinic()<<endl;
	while (s.size()) 
	{
		auto k=s.top();s.pop();
		if (k.first>m) break;
		cout<<k.first<<" "<<k.second<<endl;
	}
	return (0-0);
}
2024/9/15 16:52
加载中...