匈牙利 然后输出match就好了
我会说是因为我的最大流写这题写挂了我才来写匈牙利的吗
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define Min(x,y) ((x)<(y)?(x):(y))
const int N=200+50,M=30000+5,inf=0x3f3f3f3f,P=19650827;
int n,m,ans=0,match[N];
bool link[N][N],vis[N];
template <class t>void rd(t &x){
x=0;int w=0;char ch=0;
while(!isdigit(ch)) w|=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
x=w?-x:x;
}
bool dfs(int x){
for(int i=m+1;i<=n;++i)
if(link[x][i]&&!vis[i]){
vis[i]=1;
if(!match[i]||dfs(match[i])){match[i]=x;return 1;}
}
return 0;
}
int main(){
freopen("in2.txt","r",stdin);
//freopen("xor.out","w",stdout);
rd(m),rd(n);
int x,y;
while(scanf("%d%d",&x,&y)&&(x+y+2)) link[x][y]=1;
for(int i=1;i<=m;++i){
memset(vis,0,sizeof(vis));
if(dfs(i)) ++ans;
}
printf("%d
",ans);
for(int i=m+1;i<=n;++i)
if(match[i]) printf("%d %d
",match[i],i);
return 0;
}
63昏
#include
#include
#include
#include
#include
#include
using namespace std;
#define Min(x,y) ((x)<(y)?(x):(y))
const int N=200+50,M=30000+5,inf=0x3f3f3f3f,P=19650827;
int n,m,tt,s,t,ans,pre[N],ans1[N],ans2[N];
template void rd(t &x){
x=0;int w=0;char ch=0;
while(!isdigit(ch)) w|=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
x=w?-x:x;
}
int head[N],tot=1;
struct edge{int v,flo,nxt;}e[M<<1];
void add(int u,int v,int flo){
e[++tot]=(edge){v,flo,head[u]},head[u]=tot;
e[++tot]=(edge){u,0,head[v]},head[v]=tot;
}
queueq;bool vis[N],used[N];
bool bfs(){
while(!q.empty()) q.pop();
memset(vis,0,sizeof(vis));
q.push(s),vis[s]=1;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u],v;i;i=e[i].nxt)
if(e[i].flo&&!vis[v=e[i].v]){
q.push(v),pre[v]=i,vis[v]=1;
if(v==t) return 1;
}
}
return 0;
}
void upd(){
int x=t;
while(x!=s){
int i=pre[x];
--e[i].flo,++e[i^1].flo;
if(!used[x]){
if(x<=m&&x) ans1[++ans1[0]]=x,used[x]=1;
if(x>m&&x<=n) ans2[++ans2[0]]=x,used[x]=1;
}
x=e[i^1].v;
}
++ans;
}
int main(){
// freopen("in2.txt","r",stdin);
//freopen("xor.out","w",stdout);
rd(m),rd(n);s=n+1,t=s+1;
int x,y;ans=ans1[0]=ans2[0]=0;
while(scanf("%d%d",&x,&y)!=EOF&&(x+y+2)) add(x,y,1);
for(int i=1;i<=m;++i) add(s,i,1);
for(int i=m+1;i<=n;++i) add(i,t,1);
while(bfs())
upd();
printf("%d
",ans);
for(int i=1;i<=ans;++i) printf("%d %d
",ans1[i],ans2[i]);
return 0;
}