Advertisement
Lexolordan

Untitled

Nov 16th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <unordered_map>
  5. #include <algorithm>
  6. #include <set>
  7.  
  8. using namespace std;
  9.  
  10. const int MAXN = 1e4;
  11.  
  12. vector<pair<int, int>> v, u;
  13.  
  14. vector<int> glr[MAXN], grl[MAXN], g[MAXN];
  15. vector<int> mtlr(MAXN), mtrl(MAXN), mt(MAXN);
  16.  
  17. vector<int> used(MAXN);
  18.  
  19. bool dfslr(int v) {
  20.     if (used[v]) return false;
  21.     used[v] = true;
  22.     for (auto to : glr[v]) {
  23.         if (mtlr[to] == -1 || dfslr(mtlr[to])) {
  24.             mtlr[to] = v;
  25.             return true;
  26.         }
  27.     }
  28.     return false;
  29. }
  30.  
  31. bool dfsrl(int v) {
  32.     if (used[v]) return false;
  33.     used[v] = true;
  34.     for (auto to : grl[v]) {
  35.         if (mtrl[to] == -1 || dfsrl(mtrl[to])) {
  36.             mtrl[to] = v;
  37.             return true;
  38.         }
  39.     }
  40.     return false;
  41. }
  42.  
  43. set<int> check;
  44.  
  45. bool dfs(int v) {
  46.     if (check.find(v) == check.end())
  47.         return false;
  48.  
  49.     if (used[v]) return false;
  50.     used[v] = true;
  51.  
  52.     for (auto to : glr[v]) {
  53.         if (check.find(to) == check.end())
  54.             continue;
  55.         if (mt[to] == -1 || dfs(mt[to])) {
  56.             mt[to] = v;
  57.             return true;
  58.         }
  59.     }
  60.     return false;
  61. }
  62.  
  63. map<int, int> d;
  64. map<pair<int, int>, int> r;
  65.  
  66.  
  67. int main() {
  68.     cin.tie(0);
  69.     ios_base::sync_with_stdio(false);
  70.  
  71.     int n, m, e;
  72.     cin >> n >> m >> e;
  73.  
  74.     v.resize(n);
  75.     u.resize(m);
  76.  
  77.     for (int i = 0; i < n; ++i) {
  78.         cin >> v[i].first;
  79.         v[i].second = i;
  80.         d[i] = v[i].first;
  81.     }
  82.  
  83.     for (int i = 0; i < m; ++i) {
  84.         cin >> u[i].first;
  85.         u[i].second = i + n;
  86.         d[i + n] = u[i].first;
  87.     }
  88.  
  89.     sort(v.rbegin(), v.rend());
  90.     sort(u.rbegin(), u.rend());
  91.  
  92.     int a, b;
  93.     for (int i = 0; i < e; ++i) {
  94.         cin >> a >> b;
  95.         a--;
  96.         b--;
  97.         r[{a, b + n}] = i;
  98.         glr[a].push_back(n + b);
  99.         grl[n + b].push_back(a);
  100.     }
  101.  
  102.     mtlr.assign(MAXN, -1);
  103.     mtrl.assign(MAXN, -1);
  104.     mt.assign(MAXN, -1);
  105.  
  106.     for (int i = 0; i < n; ++i) {
  107.         used.assign(n, false);
  108.         dfslr(v[i].second);
  109.     }
  110.  
  111.     for (int i = 0; i < m; ++i) {
  112.         used.assign(n + m, false);
  113.         dfsrl(u[i].second);
  114.     }
  115.  
  116.     for (int i = 0; i < n + m; ++i) {
  117.         if (mtlr[i] != -1) {
  118.             check.insert(mtlr[i]);
  119.         }
  120.     }
  121.  
  122.     for (int i = 0; i < n + m; ++i) {
  123.         if (mtrl[i] != -1) {
  124.             check.insert(mtrl[i]);
  125.         }
  126.     }
  127.  
  128.     for (int i = 0; i < n; ++i) {
  129.         used.assign(n, false);
  130.         dfs(v[i].second);
  131.     }
  132.  
  133.     vector<int> answ;
  134.  
  135.     int ans = 0;
  136.     for (int i = 0; i < n + m; ++i) {
  137.         if (mt[i] != -1) {
  138.             ans += d[i] + d[mt[i]];
  139.             answ.push_back(r[{mt[i], i}]);
  140.         }
  141.     }
  142.  
  143.     cout << ans << endl;
  144.     cout << answ.size() << endl;
  145.     for (auto e : answ) {
  146.         cout << e + 1 << " ";
  147.     }
  148.     cout << endl;
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement