例题
总结起来就是有机会就上,没有机会创作机会也要上。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 30100;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int k,n,m;
int head[maxn];
int cnt;
bool use[maxn];
int w[maxn]; //该数组的作用是表示每一个男孩所匹配的女该。
struct node
{
int y,p;
}s[maxn];
void add(int a,int b)
{
s[++cnt]=node{b,head[a]};
head[a]=cnt;
}
int find(int a) //最主要的部分
{
if(use[a]) //剪枝用跑过的就不再跑了
return false;
use[a]=true; //设为已经跑过
for(int i=head[a];~i;i=s[i].p){
int b=s[i].y;
if(w[b]==0||find(w[b])){ //该男子未被匹配或者该男孩可以匹配其他有效的女孩
w[b]=a;
return true;
}
}
return false;
}
int main()
{
while(~scanf("%d",&k)){
if(k==0)break;
memset(w,0,sizeof(w));
scanf("%d %d",&m,&n);
cnt=0;
memset(head,-1,sizeof(head));
for(int i=0;i<k;i++){
int a,b;
scanf("%d %d",&a,&b);
add(a,b);
}
int ans=0;
for(int i=1;i<=m;i++){
memset(use,false,sizeof(use));
if(find(i))
ans++;
}
printf("%d\n",ans);
}
return 0;
}