HDU ACM 2063 过山车(二分匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2063
View Code
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 const int MAX = 1500 + 10; 5 vector <int> v[MAX]; 6 int link[MAX]; 7 int used[MAX]; 8 bool FindPath(int num) 9 { 10 int len = v[num].size(); 11 int i; 12 for(i=0;i<len;i++) 13 { 14 int n = v[num][i]; 15 if(!used[n]) 16 { 17 used[n] = 1; 18 if(link[n] == -1 || FindPath(link[n])) 19 { 20 link[n] = num; 21 return true; 22 } 23 } 24 } 25 return false; 26 } 27 void Solve(int n) 28 { 29 memset(link,-1,sizeof(link)); 30 int i; 31 int num = 0; 32 for(i=1;i<=n;i++) 33 { 34 memset(used,0,sizeof(used)); 35 if(FindPath(i)) 36 { 37 num++; 38 } 39 } 40 cout<<num<<endl; 41 } 42 int main() 43 { 44 int k,f,m; 45 while(cin>>k,k) 46 { 47 cin>>f>>m; 48 int i; 49 for(i=0;i<=f;i++) 50 { 51 v[i].clear(); 52 } 53 for(i=0;i<k;i++) 54 { 55 int a,b; 56 cin>>a>>b; 57 v[a].push_back(b); 58 } 59 Solve(f); 60 } 61 return 0; 62 }