洛谷P2756 飞行员配对方案问题 网络流_二分图
Code:
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=203;
const int INF=100000+233;
int s,t;
int mapp[maxn];
struct Edge{
int from,to,cap;
Edge(int u,int v,int c):from(u),to(v),cap(c){}
};
vector<Edge>edges;
struct Dicnic{
vector<int>G[maxn];
queue<int>Q;
int d[maxn],vis[maxn],p[maxn],cur[maxn];
void addedge(int u,int v,int c){
edges.push_back(Edge(u,v,c));
edges.push_back(Edge(v,u,0));
int m=edges.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}
int BFS(){
memset(vis,0,sizeof(vis));
Q.push(s);vis[s]=1,d[s]=0;
while(!Q.empty()){
int u=Q.front();Q.pop();
int sz=G[u].size();
for(int i=0;i<sz;++i)
{
Edge e=edges[G[u][i]];
if(e.cap>0&&!vis[e.to]){
d[e.to]=d[u]+1,vis[e.to]=1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t)return a;
int sz=G[x].size();
int flow=0;
for(int i=cur[x];i<sz;++i){
Edge e=edges[G[x][i]];
if(d[e.to]==d[x]+1&&e.cap>0){
int f=dfs(e.to,min(a,e.cap));
if(f>0){
int u=G[x][i];
a-=f,flow+=f;
edges[u].cap-=f;
edges[u^1].cap+=f;
if(a==0)break;
}
}
}
return flow;
}
int maxflow(){
memset(cur,0,sizeof(cur));
int ans=0;
while(BFS())ans+=dfs(s,INF);
return ans;
}
};
int main()
{
int n,m;
Dicnic op;
scanf("%d%d",&m,&n);
s=0,t=m+n+1;
for(int i=1;i<=m;++i)op.addedge(s,i,1);
for(int i=m+1;i<=n+m;++i)op.addedge(i,t,1);
while(233)
{
int a,b;scanf("%d%d",&a,&b);
if(a<0||b<0)break;
op.addedge(a,b,1);
}
int ans=op.maxflow();
if(ans==0){
printf("No Solution!\n");
return 0;
}
printf("%d\n",ans);
int sz=edges.size();
for(int i=n+n+m+m;i<sz;i+=2){
if(edges[i].cap==0)mapp[edges[i].from]=edges[i].to;
}
for(int i=1;i<=m;++i)
if(mapp[i])printf("%d %d\n",i,mapp[i]);
return 0;
}