Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.2 kB
0
Indexable
Never
#include <bits/stdc++.h>
#define int long long
using namespace std;

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;

    while (t--) {
        int m;
        cin >> m;
        vector<vector<int>> v(m);


        for (int i = 0; i < m; ++i) {
            int n;
            cin >> n;
            v[i].resize(n);
            for (int j = 0; j < n; ++j) {
                cin >> v[i][j];
            }
        }

        vector<int> result(m);
        set<int> used;
        bool f = true;


        for (int i = m - 1; i >= 0; --i) {
            bool found = false;
            for (int it : v[i]) {
                if (used.count(it) == 0) {
                    result[i] = it;
                    used.insert(it);
                    found = true;

                }used.insert(it);

            }
            if (!found) {
                f = false;
                break;
            }
        }

        if (f) {
            for (int i = 0; i < result.size(); ++i) {
                if (i != 0) cout << " ";
                cout << result[i];
            }
            cout << endl;
        } else {
            cout << -1 << endl; 
        }
    }

    return 0;
}
Leave a Comment