题目大意
给出一些题目,每题有两个锦囊,用其中一个一定能解决一道题,必须按顺序解决题目,求最多可以解决多少道题目。
解题思路
显然用匈牙利做二分图匹配,注意按顺序做,一旦有一个失配就停止并输出答案。
code
#include<set>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
using namespace std ;
int const maxn=1001 ,maxm=1001 ;
int n,m,to[maxm+10 ][10 ],pai[maxn+10 ];
bool visit[maxn+10 ];
bool match(int now){
visit[now]=1 ;
fo(i,1 ,2 ){
int next=to[now][i];
if ((!pai[next])||((!visit[pai[next]])&&(to[pai[next]][0 ])&&(match(pai[next])))){
pai[next]=now;
return 1 ;
}
}
return 0 ;
}
int main(){
freopen("d.in" ,"r" ,stdin);
freopen("d.out" ,"w" ,stdout);
scanf ("%d%d" ,&n,&m);
int ans=0 ;
fo(i,1 ,m){
scanf ("%d%d" ,&to[i][1 ],&to[i][2 ]);to[i][0 ]=1 ;
if (match(i))ans++;
else break ;
memset (visit,0 ,sizeof (visit));
}
printf ("%d" ,ans);
return 0 ;
}