hdu 2063 过山车
http://acm.hdu.edu.cn/showproblem.php?pid=2063
View Code
1 #include <cstdio> 2 #include <vector> 3 #include <cstring> 4 5 using namespace std; 6 7 const int maxn = 501; 8 vector<int> p[maxn]; 9 int mat[maxn]; 10 bool vis[maxn]; 11 12 bool dfs(int v){ 13 for (int i = 0; i < p[v].size(); i++){ 14 int t = p[v][i]; 15 16 if (!vis[t]){ 17 vis[t] = true; 18 if (mat[t] == -1 || dfs(mat[t])){ 19 mat[t] = v; 20 return true; 21 } 22 } 23 } 24 return false; 25 } 26 27 int match(int m, int n){ 28 int cnt = 0; 29 30 memset(mat, -1, sizeof(mat)); 31 for (int i = 1; i <= m; i++){ 32 memset(vis, false, sizeof(vis)); 33 if (dfs(i)) cnt++; 34 } 35 36 return cnt; 37 } 38 39 bool deal(){ 40 int k, m, n; 41 int a, b; 42 43 scanf("%d", &k); 44 if (!k) return false; 45 scanf("%d%d", &m, &n); 46 for (int i = 1; i <= m; i++) p[i].clear(); 47 for (int i = 0; i < k; i++){ 48 scanf("%d%d", &a, &b); 49 p[a].push_back(b); 50 } 51 printf("%d\n", match(m, n)); 52 53 return true; 54 } 55 56 int main(){ 57 #ifndef ONLINE_JUDGE 58 freopen("in", "r", stdin); 59 #endif 60 while (deal()); 61 62 return 0; 63 }
(代码更新了,之前不懂原理,搞了个相对复杂的,现在修改成较为简单的一个)
切一下水题,试一下简单的二分匹配,1y了~ 向大牛学习,学习各种知识,各种技巧!
keep walking...我一定要不负众望,向高峰迈进!
——written by Lyon