Source Code
#include <bits/stdc++.h>

#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

typedef long long ll;
typedef long double ld;
const int N = 1e6 + 15, lg = 20;
int mod = 1e9 + 7;
int inf = 1e9;
bool good[N];
int co[N];

int main() {
    FIO
    int n, m;
    cin >> n >> m;
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int x;
        cin >> x;
        good[x] = 1;
    }
    vector<vector<int>> par(n + 1);
    for (int i = 1; i <= n; i++) {
        int sz;
        cin >> sz;
        par[i].resize(sz);
        bool goodp = 1;
        for (auto &it:par[i]) {
            cin >> it;
            goodp &= good[it];
        }
        if (goodp) {
            for (auto &it:par[i]) {
                co[it]++;
            }
        }
    }
    vector<int> ans;
    for (int i = 1; i <= n; i++) {
        bool goodp = 0, goodp2 = 1;
        for (auto &it:par[i]) {
            goodp2 &= good[it];
            goodp |= (co[it] == 1);
        }
        if (goodp && goodp2)
            ans.push_back(i);
    }
    cout << ans.size() << endl;
    for (auto it:ans)
        cout << it << " ";
    return 0;
}
Copy
Projects ELbadawe
GNU G++17
487 ms
39.1 MB
Accepted