POJ 3041 Asteroids Posted on 2018-09-30 | In ACM | 阅读 匈牙利算法模板 C++ Code 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#define M 500using namespace std;bool grid[M][M];bool vis[M];//本轮是否已经尝试通过y来增广并且失败int n, k;int cnt;int link[M];bool dfs(int x){ for(int i = 1; i <= n; i++){ if(grid[x][i] && !vis[i]){ vis[i] = true; if(link[i] == -1 || dfs(link[i]) == true){ link[i] = x; return true; } } } return false;}void search(){ for(int i = 1; i <= n; i++){ memset(vis, false, sizeof(vis)); if(dfs(i)) cnt++; }}int main(){ int x, y; while(~scanf("%d%d", &n, &k)){ memset(link, -1, sizeof(link)); memset(grid, false, sizeof(grid)); for(int i = 0; i < k; i++){ scanf("%d%d", &x, &y); grid[x][y] = true; } cnt = 0; search(); printf("%d\n", cnt); } return 0;}