POJ 3041 Asteroids 题解
简单的二分图最大匹配
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 #include<cstdio> 5 #include<cstring> 6 7 #define MAXN 510 8 9 int match[MAXN]; 10 int map[MAXN][MAXN]; 11 int vis[MAXN]; 12 int K,M,N; 13 14 int find(int x) 15 { 16 for(int i=1;i<=N;i++) 17 { 18 if(map[x][i]&&!vis[i]) 19 { 20 vis[i] = 1; 21 if(!match[i] || find(match[i])) 22 { 23 match[i] = x; 24 return 1; 25 } 26 } 27 } 28 return 0; 29 } 30 31 int main() 32 { 33 while(scanf("%d%d",&N,&K)!=EOF) 34 { 35 memset(map,0,sizeof(map)); 36 memset(match,0,sizeof(match)); 37 int a,b; 38 for(int i=0;i<K;i++) 39 { 40 scanf("%d%d",&a,&b); 41 map[a][b]=1; 42 } 43 44 int ans=0; 45 for(int i=1;i<=N;i++) 46 { 47 memset(vis,0,sizeof(vis)); 48 ans+=find(i); 49 } 50 printf("%d\n",ans); 51 } 52 return 0; 53 }