题目链接:点击这里
题目大意:
将
n
n
n 名飞行员分成
1
1
1 到
m
m
m ,
m
+
1
m+1
m+1 到
n
n
n 两组,给出两组之间的一些相互配合的关系,求每个飞行员至多只能和一名另一组飞行员想匹配的情况下的最大分组数量
题目分析:
典型的二分图匹配模型,我们将
1
1
1 到
m
m
m 与超级源点
s
s
s 相连,
m
+
1
m+1
m+1 到
n
n
n 与超级汇点
t
t
t 相连,在这张图上跑网络流即可(顺便记录当前点的后继即为所求方案)
具体细节见代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
#define int ll
using namespace std;
int read()
{
int res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 205;
const int maxm = 5005;
const int mod = 998244353;
const double pi = acos(-1);
const double eps = 1e-8;
struct Edge{
int nxt,to,val;
}edge[maxm<<1];
int n,m,s,t,cnt = 1,head[maxn],cur[maxn],dep[maxn],son[maxn];
void addedge(int from,int to,int val)
{
edge[++cnt].nxt = head[from];
edge[cnt].to = to;
edge[cnt].val = val;
head[from] = cnt;
}
bool bfs(int s,int t)
{
memcpy(cur,head,sizeof(head));
memset(dep,0,sizeof(dep));
dep[s] = 1;
queue<int>qu;
qu.push(s);
while(!qu.empty())
{
int h = qu.front(); qu.pop();
for(int i = head[h];i;i = edge[i].nxt)
{
int to = edge[i].to,val = edge[i].val;
if(val>0 && !dep[to]) dep[to] = dep[h]+1,qu.push(to);
}
}
return dep[t];
}
int dfs(int now = s,int flow = inf) //s->t
{
if(now == t) return flow;
int rem = flow;
for(int i = cur[now];i && rem;i = edge[i].nxt)
{
cur[now] = i;
int to = edge[i].to,val = edge[i].val;
if(val>0 && dep[to]==dep[now]+1)
{
int tmp = dfs(to,min(val,rem));
if(tmp > 0) son[now] = to;
rem -= tmp;
edge[i].val -= tmp;
edge[i^1].val += tmp;
}
}
return flow-rem;
}
int dinic(int s,int t)
{
int res = 0;
while(bfs(s,t)) res += dfs();
return res;
}
signed main()
{
m = read(),n = read();
s = m+n+1,t = m+n+2;
for(int i = 1;i <= m;i++) addedge(s,i,1),addedge(i,s,0);
for(int i = m+1;i <= n;i++) addedge(i,t,1),addedge(t,i,0);
int from,to;
while(scanf("%lld%lld",&from,&to) && (from != -1 || to != -1))
{
addedge(from,to,1);
addedge(to,from,0);
}
printf("%d\n",dinic(s,t));
for(int i = 1;i <= m;i++) if(son[i]) printf("%d %d\n",i,son[i]);
return 0;
}