二分图
#include<bits/stdc++.h> using namespace std; int x,y,S,T,MAXN=99999999,level[500],lin[500],ans=0,n,m,len=0,q[500]; struct one { int x,y,next,flow,v,reverse; bool f; }; one e[50000]; void insert(int xx,int yy,int vv) { e[++len].next=lin[xx]; lin[xx]=len; e[len].x=xx; e[len].y=yy;e[len].v=vv; e[len].reverse=len+1; e[++len].next=lin[yy]; lin[yy]=len; e[len].y=xx;e[len].v=0; e[len].reverse=len-1; } bool makelevel() { memset(level,-1,sizeof(level)); q[1]=0;level[0]=0; int head=0,tail=1; while(head++<tail) { int tn=q[head]; for(int i=lin[tn];i;i=e[i].next) { if(e[i].v>0&&level[e[i].y]<0) { q[++tail]=e[i].y; level[e[i].y]=level[tn]+1; } } } return level[T]>=0; } int MAXflow(int aa,int flow) { if(aa==T)return flow; int maxflow=0,d=0; for(int i=lin[aa];maxflow<flow&&i;i=e[i].next) { if(e[i].v&&level[e[i].y]==level[aa]+1) { if(d=MAXflow(e[i].y,min(flow-maxflow,e[i].v))) { e[i].v-=d; e[e[i].reverse].v+=d; maxflow+=d; } } } if(maxflow<=0)level[aa]=-1; return maxflow; } void dinic() { while(makelevel()) ans+=MAXflow(S,MAXN); } int main() { scanf("%d%d",&m,&n); S=0;T=n+m+1; for(int i=1;i<=m;i++)insert(S,i,1); for(int i=1;i<=n;i++)insert(i+m,T,1); while(1) { scanf("%d%d",&x,&y); if(x==-1&&y==-1)break; insert(x,y+m,1); e[len-1].f=true; } dinic(); printf("%d ",ans); for(int i=1;i<=len;i++) { if(e[i].f&&e[i].v==0)printf("%d %d ",e[i].x,e[i].y-m); } return 0; }