Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define _MY_FILE_
- #ifdef _MY_FILE_
- #include <fstream>
- //#define _MY_DEBUG_
- #else
- #include <iostream>
- #endif
- #include <string>
- #include <vector>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <cassert>
- #include <queue>
- #include <memory>
- #include <iomanip>
- #include <set>
- #include <map>
- #include <cmath>
- using namespace std;
- #ifdef _MY_FILE_
- string res_path = "/home/pavel/Qt5.8.0/Projects/11-lab-flow/";
- ifstream cin(res_path + "in.txt");
- ofstream cout(res_path + "out.txt");
- #endif
- const int max_n = 10000;
- int nl, nr, m;
- bool usedl[max_n];
- //bool usedr[max_n];
- int match[max_n];
- bool is_matchl[max_n];
- vector<int> gl[max_n];
- //vector<int> gr[max_n];
- inline void add_edge(int a, int b);
- inline void kuhn(); // res --> match
- bool inc_dfs(int vl);
- void dfs_marker(int vl); // --> res
- vector<int> resl;
- bool used_markerl[max_n];
- //bool used_markerr[max_n];
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- #ifdef _MY_DEBUG_
- cout << "DEBUG" << endl;
- #endif
- while (cin >> nr >> nl) {
- cin >> m;
- for (int i = 0; i < nl; i++) gl[i].clear();
- // for (int i = 0; i < nr; i++) gr[i].clear();
- for (int i = 0, a, b; i < m; ++i) {
- cin >> a >> b;
- add_edge(b - 1, a - 1);
- }
- memset(match, -1, sizeof(match));
- memset(is_matchl, 0, sizeof(is_matchl));
- kuhn(); // --> match
- memset(used_markerl, 0, sizeof(used_markerl));
- // memset(used_markerr, 0, sizeof(used_markerr));
- resl.clear();
- for (int vl = 0; vl < nl; vl++) {
- if (!is_matchl[vl]) {
- dfs_marker(vl);
- break;
- }
- }
- #ifdef _MY_DEBUG_
- cout << "match:\n";
- for (int i = 0; i < nr; ++i) {
- cout << i << " -> " << match[i] << "\n";
- }
- cout << endl;
- #endif
- #ifdef _MY_DEBUG_
- cout << "is_match:\n";
- for (int i = 0; i < nl; ++i) {
- cout << i << " match: " << is_matchl[i] << "\n";
- }
- cout << endl;
- #endif
- sort(resl.begin(), resl.end());
- cout << resl.size() << endl;
- for (auto i: resl) {
- cout << i + 1 << " ";
- }
- cout << "\n\n";
- }
- return 0;
- }
- void add_edge(int a, int b) {
- gl[a].push_back(b);
- // gr[b].push_back(a);
- return;
- }
- void kuhn() {
- memset(usedl, 0, sizeof(usedl));
- // memset(usedr, 0, sizeof(usedr));
- bool go = true;
- while(go) {
- go = 0;
- memset(usedl, 0, sizeof(usedl));
- for (int vl = 0; vl < nl; ++vl) {
- if (!is_matchl[vl] && inc_dfs(vl)) {
- go = true;
- }
- }
- }
- return;
- }
- bool inc_dfs(int vl) {
- if (usedl[vl]) {
- return false;
- } else {
- usedl[vl] = true;
- for (auto vr: gl[vl]) {
- if (match[vr] == -1) {
- match[vr] = vl;
- is_matchl[vl] = true;
- return true;
- }
- }
- for (auto vr: gl[vl]) {
- if (inc_dfs(match[vr])) {
- match[vr] = vl;
- is_matchl[vl] = true;
- return true;
- }
- }
- return false;
- }
- }
- void dfs_marker(int vl) {
- if(used_markerl[vl]) {
- return;
- } else {
- used_markerl[vl] = true;
- resl.push_back(vl);
- for (auto vr : gl[vl]) {
- if (match[vr] != -1) {
- dfs_marker(match[vr]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement