专栏文章
匈牙利算法优化
个人记录参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @minbc5wq
- 此快照首次捕获于
- 2025/12/01 23:37 3 个月前
- 此快照最后确认于
- 2025/12/01 23:37 3 个月前
CPP
#include <bits/stdc++.h>
using namespace std;
const int N=505,M=5e4+8;
struct G{int x,t;} a[M];
int n,m,e,w[N],h[N],cnt=0,k[N];
bool dfs(int x){
w[x]=1;
for (int i=h[x],j=0;i;j=i,i=a[i].t){
if (k[a[i].x]==0||(w[k[a[i].x]]==0&&dfs(k[a[i].x]))){
k[a[i].x]=x;w[x]=0;return 1;
}else if (w[k[a[i].x]]==2){
if (j==0) h[x]=a[i].t;
else a[j].t=a[i].t;
}
}
w[x]=2;
return 0;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >>n>>m>>e;
for (int i=1,x,y;i<=e;++i){
cin >>x>>y;
a[++cnt]={y,h[x]},h[x]=cnt;
}
int ans=0;
for (int i=1;i<=n;++i){
if (dfs(i)) ++ans;
}
cout <<ans;
return 0;
}
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...