Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- //template<typename T>
- //void print_vector(vector<T> vec) { for (auto &t : vec) cout << t << " "; cout << endl; }
- vector<vector<int>> to;
- vector<bool> used;
- vector<int> matching;
- bool dfs(int x) {
- if (used[x]) {
- return false;
- }
- used[x] = true;
- for (auto &t : to[x]) {
- if (matching[t] == -1 || dfs(matching[t])) {
- matching[t] = x;
- return true;
- }
- }
- return false;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- ifstream fin("matching.in");
- ofstream fout("matching.out");
- int n;
- fin >> n;
- vector<int> w(n);
- vector<int> indexes(n);
- for (int i = 0; i < n; ++i) {
- fin >> w[i];
- }
- to.resize(n, vector<int>());
- for (int i = 0; i < n; ++i) {
- int tc;
- fin >> tc;
- for (int j = 0; j < tc; ++j) {
- int t;
- fin >> t;
- t--;
- to[i].push_back(t);
- }
- }
- used.resize(n, false);
- matching.resize(n, -1);
- for (int i = 0; i < n; ++i) {
- indexes[i] = i;
- }
- sort(indexes.begin(), indexes.end(), [&](int a, int b) { return w[a] > w[b]; });
- for (int i = 0; i < n; ++i) {
- fill(used.begin(), used.end(), false);
- dfs(indexes[i]);
- }
- vector<int> answ_back(n, -1);
- for (int i = 0; i < n; ++i) {
- if (matching[i] != -1)
- answ_back[matching[i]] = i;
- }
- for (int i = 0; i < n; ++i) {
- fout << answ_back[i] + 1 << " ";
- }
- fout << endl;
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement