易错点:
- 需要用pre[]数组记录方案.
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN=1000010,MAXM=1000010;
int n,s,t;
struct Edge{
int from,to,w,nxt;
}e[MAXM];
int head[MAXN],edgeCnt=1;
void addEdge(int u,int v,int w){
e[++edgeCnt].from=u;
e[edgeCnt].to=v;
e[edgeCnt].w=w;
e[edgeCnt].nxt=head[u];
head[u]=edgeCnt;
}
int d[MAXN];
bool bfs(){
memset(d,0,sizeof(d));
queue<int> q;
d[s]=1;q.push(s);
while(!q.empty()){
int nowV=q.front();q.pop();
for(int i=head[nowV];i;i=e[i].nxt){
int nowNode=e[i].to;
if(!d[nowNode]&&e[i].w){
d[nowNode]=d[nowV]+1;
if(nowNode==t)return 1;
q.push(nowNode);
}
}
}
return d[t]!=0;
}
int pre[MAXN];
int Dinic(int x,int flow){
if(x==t)return flow;
int rest=flow;
for(int i=head[x];i&&rest;i=e[i].nxt){
int nowV=e[i].to;
if(d[nowV]==d[x]+1&&e[i].w){
int k=Dinic(nowV,min(rest,e[i].w));
if(!k)d[nowV]=0;
if(k)pre[nowV]=x;
e[i].w-=k,e[i^1].w+=k;
rest-=k;
}
}
return flow-rest;
}
const int INF=2e9;
int main(){
int m;
scanf("%d%d",&m,&n);
s=n+1,t=n+2;
int u,v;
while(1){
scanf("%d%d",&u,&v);
if(u==-1){
break;
}
addEdge(u,v,INF);
addEdge(v,u,0);
}
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 ans=0;
while(bfs()){
ans+=Dinic(s,2e9);
}
if(!ans){
cout<<"No Solution!"<<endl;
return 0;
}
cout<<ans<<endl;
for(int i=m+1;i<=n;i++){
if(pre[i])
printf("%d %d\n",i,pre[i]);
}
return 0;
}