Advertisement
nullzero

Social Holidaying: P'Amm

Aug 17th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  E
  4. //
  5. //  Created by Wasin Kalintha on 7/31/12.
  6. //  Copyright 2012 Chulalongkorn University. All rights reserved.
  7. //
  8.  
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <set>
  12.  
  13. using namespace std;
  14.  
  15. const int N(500);
  16. const int UNDEF(-1);
  17.  
  18. set<int> s;
  19.  
  20. int _pair[N], a[N];
  21. bool used[N];
  22. int R, B;
  23.  
  24. bool dfs(int now){
  25.     used[now] = true;
  26.     for(int j = 0; j < R; j++){
  27.         if(used[j]) continue;
  28.         if(_pair[j] == UNDEF and s.find(a[now] + a[j]) != s.end()){
  29.             _pair[now] = j;
  30.             _pair[j] = now;
  31.             return true;
  32.         }
  33.     }
  34.     for(int j = 0; j < R; j++){
  35.         if(used[j]) continue;
  36.         if(s.find(a[now] + a[j]) != s.end()){
  37.             _pair[now] = j;
  38.             int k = _pair[j];
  39.             _pair[j] = now;
  40.             _pair[k] = UNDEF;
  41.             used[j] = true;
  42.             if(dfs(k)) return true;
  43.         }
  44.     }
  45.     return false;
  46. }
  47.  
  48. void solve(){
  49.     s.clear();
  50.     scanf("%d %d", &R, &B);
  51.     for(int i = 0; i < R; i++){
  52.         _pair[i] = UNDEF;
  53.         scanf("%d", &a[i]);
  54.     }
  55.     for(int i = 0; i < B; i++){
  56.         int k;
  57.         scanf("%d", &k);
  58.         s.insert(k);
  59.     }
  60.     int ans = 0;
  61.     while(true){
  62.         bool hasAug = false;
  63.         for(int i = 0; i < R; i++){
  64.             if(_pair[i] == UNDEF){
  65.                 memset(used, 0, sizeof(used));
  66.                 if(dfs(i)){
  67.                     hasAug = true;
  68.                     ans++;
  69.                     break;
  70.                 }
  71.             }
  72.         }
  73.         if(not hasAug) break;
  74.     }
  75.     printf("%d\n", ans);
  76. }
  77.  
  78. int main(){
  79.     int n;
  80.     scanf("%d", &n);
  81.     while(n--) solve();
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement