[网络流24题]飞行员配对方案问题
题意:二分图模板
Code(匈牙利算法)
#include<bits/stdc++.h>
#define N 205
using namespace std;
int n,m,ans;
int link[N][N];
int match[N],book[N];
template <class T>
void read(T &x)
{
char c;int sign=1;
while((c=getchar())>'9'||c<'0') if(c=='-') sign=-1; x=c-48;
while((c=getchar())>='0'&&c<='9') x=(x<<1)+(x<<3)+c-48; x*=sign;
}
bool dfs(int u)
{
for(int i=1;i<=m;++i)
{
if(link[u][i]&&!book[i])
{
book[i]=1;
if(!match[i]||dfs(match[i]))
{
match[i]=u;
return true;
}
}
}
return false;
}
int main()
{
read(m);read(n);
int x,y;
while(1)
{
read(x);read(y);
if(x==-1||y==-1) break;
link[x][y]=link[y][x]=1;
}
for(int i=m+1;i<=n+m;++i)
{
memset(book,0,sizeof(book));
if(dfs(i)) ++ans;
}
if(!ans)
{
printf("No Solution!");
return 0;
}
printf("%d\n",ans);
for(int i=1;i<=m;++i)
{
if(match[i]) printf("%d %d\n",i,match[i]);
}
return 0;
}