Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. //#define _MY_FILE_
  2.  
  3. #ifdef _MY_FILE_
  4. #include <fstream>
  5. //#define _MY_DEBUG_
  6. #else
  7. #include <iostream>
  8. #endif
  9.  
  10. #include <string>
  11. #include <vector>
  12. #include <cstdio>
  13. #include <cstring>
  14. #include <algorithm>
  15. #include <cassert>
  16. #include <queue>
  17. #include <memory>
  18. #include <iomanip>
  19. #include <set>
  20. #include <map>
  21. #include <cmath>
  22.  
  23. using namespace std;
  24.  
  25. #ifdef _MY_FILE_
  26. string res_path  = "/home/pavel/Qt5.8.0/Projects/11-lab-flow/";
  27. ifstream cin(res_path + "in.txt");
  28. ofstream cout(res_path + "out.txt");
  29. #endif
  30.  
  31. const int max_n = 10000;
  32.  
  33. int nl, nr, m;
  34. bool usedl[max_n];
  35. //bool usedr[max_n];
  36. int match[max_n];
  37. bool is_matchl[max_n];
  38. vector<int> gl[max_n];
  39. //vector<int> gr[max_n];
  40.  
  41. inline void add_edge(int a, int b);
  42. inline void kuhn(); // res --> match
  43. bool inc_dfs(int vl);
  44.  
  45.  
  46. void dfs_marker(int vl); // --> res
  47. vector<int> resl;
  48. bool used_markerl[max_n];
  49. //bool used_markerr[max_n];
  50.  
  51. int main() {
  52.     ios_base::sync_with_stdio(false);
  53.     cin.tie(NULL);
  54. #ifdef _MY_DEBUG_
  55.     cout << "DEBUG" << endl;
  56. #endif
  57.  
  58.     while (cin >> nr >> nl) {
  59.         cin >> m;
  60.  
  61.         for (int i = 0; i < nl; i++) gl[i].clear();
  62. //        for (int i = 0; i < nr; i++) gr[i].clear();
  63.  
  64.         for (int i = 0, a, b; i < m; ++i) {
  65.             cin >> a >> b;
  66.             add_edge(b - 1, a - 1);
  67.         }
  68.  
  69.         memset(match, -1, sizeof(match));
  70.         memset(is_matchl, 0, sizeof(is_matchl));
  71.         kuhn(); // --> match
  72.         memset(used_markerl, 0, sizeof(used_markerl));
  73. //        memset(used_markerr, 0, sizeof(used_markerr));
  74.         resl.clear();
  75.         for (int vl = 0; vl < nl; vl++) {
  76.             if (!is_matchl[vl]) {
  77.                 dfs_marker(vl);
  78.                 break;
  79.             }
  80.         }
  81. #ifdef _MY_DEBUG_
  82.       cout << "match:\n";
  83.       for (int i = 0; i < nr; ++i) {
  84.           cout << i << " -> " << match[i] << "\n";
  85.       }
  86.       cout << endl;
  87. #endif
  88.  
  89. #ifdef _MY_DEBUG_
  90.       cout << "is_match:\n";
  91.       for (int i = 0; i < nl; ++i) {
  92.           cout << i << " match: " << is_matchl[i] << "\n";
  93.       }
  94.       cout << endl;
  95. #endif
  96.         sort(resl.begin(), resl.end());
  97.         cout << resl.size() << endl;
  98.         for (auto i: resl) {
  99.             cout << i  + 1 << " ";
  100.         }
  101.         cout << "\n\n";
  102.     }
  103.  
  104.     return 0;
  105. }
  106.  
  107. void add_edge(int a, int b) {
  108.     gl[a].push_back(b);
  109. //    gr[b].push_back(a);
  110.     return;
  111. }
  112.  
  113. void kuhn() {
  114.     memset(usedl, 0, sizeof(usedl));
  115. //    memset(usedr, 0, sizeof(usedr));
  116.  
  117.     bool go = true;
  118.     while(go) {
  119.         go = 0;
  120.         memset(usedl, 0, sizeof(usedl));
  121.         for (int vl = 0; vl < nl; ++vl) {
  122.             if (!is_matchl[vl] && inc_dfs(vl)) {
  123.                 go = true;
  124.             }
  125.         }
  126.     }
  127.     return;
  128. }
  129.  
  130. bool inc_dfs(int vl) {
  131.     if (usedl[vl]) {
  132.         return false;
  133.     } else {
  134.         usedl[vl] = true;
  135.         for (auto vr: gl[vl]) {
  136.             if (match[vr] == -1) {
  137.                 match[vr] = vl;
  138.                 is_matchl[vl] = true;
  139.                 return true;
  140.             }
  141.         }
  142.         for (auto vr: gl[vl]) {
  143.             if (inc_dfs(match[vr])) {
  144.                 match[vr] = vl;
  145.                 is_matchl[vl] = true;
  146.                 return true;
  147.             }
  148.         }
  149.         return false;
  150.     }
  151. }
  152.  
  153. void dfs_marker(int vl) {
  154.     if(used_markerl[vl]) {
  155.         return;
  156.     } else {
  157.         used_markerl[vl] = true;
  158.         resl.push_back(vl);
  159.         for (auto vr : gl[vl]) {
  160.             if (match[vr] != -1) {
  161.                dfs_marker(match[vr]);
  162.             }
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement