Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <unordered_map>
- #include <algorithm>
- #include <set>
- using namespace std;
- const int MAXN = 1e4;
- vector<pair<int, int>> v, u;
- vector<int> glr[MAXN], grl[MAXN], g[MAXN];
- vector<int> mtlr(MAXN), mtrl(MAXN), mt(MAXN);
- vector<int> used(MAXN);
- bool dfslr(int v) {
- if (used[v]) return false;
- used[v] = true;
- for (auto to : glr[v]) {
- if (mtlr[to] == -1 || dfslr(mtlr[to])) {
- mtlr[to] = v;
- return true;
- }
- }
- return false;
- }
- bool dfsrl(int v) {
- if (used[v]) return false;
- used[v] = true;
- for (auto to : grl[v]) {
- if (mtrl[to] == -1 || dfsrl(mtrl[to])) {
- mtrl[to] = v;
- return true;
- }
- }
- return false;
- }
- set<int> check;
- bool dfs(int v) {
- if (check.find(v) == check.end())
- return false;
- if (used[v]) return false;
- used[v] = true;
- for (auto to : glr[v]) {
- if (check.find(to) == check.end())
- continue;
- if (mt[to] == -1 || dfs(mt[to])) {
- mt[to] = v;
- return true;
- }
- }
- return false;
- }
- map<int, int> d;
- map<pair<int, int>, int> r;
- int main() {
- cin.tie(0);
- ios_base::sync_with_stdio(false);
- int n, m, e;
- cin >> n >> m >> e;
- v.resize(n);
- u.resize(m);
- for (int i = 0; i < n; ++i) {
- cin >> v[i].first;
- v[i].second = i;
- d[i] = v[i].first;
- }
- for (int i = 0; i < m; ++i) {
- cin >> u[i].first;
- u[i].second = i + n;
- d[i + n] = u[i].first;
- }
- sort(v.rbegin(), v.rend());
- sort(u.rbegin(), u.rend());
- int a, b;
- for (int i = 0; i < e; ++i) {
- cin >> a >> b;
- a--;
- b--;
- r[{a, b + n}] = i;
- glr[a].push_back(n + b);
- grl[n + b].push_back(a);
- }
- mtlr.assign(MAXN, -1);
- mtrl.assign(MAXN, -1);
- mt.assign(MAXN, -1);
- for (int i = 0; i < n; ++i) {
- used.assign(n, false);
- dfslr(v[i].second);
- }
- for (int i = 0; i < m; ++i) {
- used.assign(n + m, false);
- dfsrl(u[i].second);
- }
- for (int i = 0; i < n + m; ++i) {
- if (mtlr[i] != -1) {
- check.insert(mtlr[i]);
- }
- }
- for (int i = 0; i < n + m; ++i) {
- if (mtrl[i] != -1) {
- check.insert(mtrl[i]);
- }
- }
- for (int i = 0; i < n; ++i) {
- used.assign(n, false);
- dfs(v[i].second);
- }
- vector<int> answ;
- int ans = 0;
- for (int i = 0; i < n + m; ++i) {
- if (mt[i] != -1) {
- ans += d[i] + d[mt[i]];
- answ.push_back(r[{mt[i], i}]);
- }
- }
- cout << ans << endl;
- cout << answ.size() << endl;
- for (auto e : answ) {
- cout << e + 1 << " ";
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement