1 //匈牙利算法模版题 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 using namespace std; 6 const int maxn = 505; 7 vector<int> v[maxn]; 8 int vis[maxn],match[maxn]; 9 bool dfs(int t) 10 { 11 for(int i = 0; i < v[t].size(); ++i) 12 { 13 int x = v[t][i]; 14 if(!vis[x]) 15 { 16 vis[x] = 1; 17 if(match[x] == -1 || dfs(match[x])) 18 {match[x] = t; return true; } 19 } 20 } 21 return false; 22 } 23 int hungary(int n) 24 { 25 int ans = 0; 26 for(int i = 1; i <= n; ++i) 27 { 28 memset(vis,0,sizeof vis); 29 if(dfs(i)) ++ans; 30 } 31 return ans; 32 } 33 int main() 34 { 35 int k,n,m,x,y; 36 while(~scanf("%d",&k) && k) 37 { 38 scanf("%d%d",&n,&m); 39 for(int i = 1; i <= n; ++i) 40 v[i].clear(); 41 memset(match,-1,sizeof match); 42 while(k--) 43 { 44 scanf("%d%d",&x,&y); 45 v[x].push_back(y); 46 } 47 printf("%d\n",hungary(n)); 48 } 49 return 0; 50 }
时间: 2024-11-07 22:02:21