Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<vector<int>> G;
- int dfs (int v, vector<int>& used, int color, vector<int>& match) {
- if (used[v] == color) return 0;
- used[v] = color;
- for (auto& u: G[v]) {
- if (match[u] == -1 || dfs (match[u], used, color, match)) match[u] = v;
- }
- }
- int main () {
- int q;
- while (q--) {
- int m, n;
- cin >> m >> n;
- G = vector<vector<int>> (m + n);
- for (int i = 0; i < m; ++i) {
- int x;
- while (1) {
- cin >> x;
- if (x == 0) break;
- --x;
- x += m;
- G[i].push_back (x), G[x].pb (i);
- }
- }
- vector<int> used (n + m), match (n + m, -1);
- int color = 1;
- for (int i = 0; i < m; ++i) {
- dfs (i, used, color, match);
- ++color;
- }
- for (auto& x: match) cout << x << ' ';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement