This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(m);
for (int i = 0; i < m; i++) {
cin >> a[i];
a[i]--;
}
int k;
cin >> k;
vector<vector<int>> adj1(n);
vector<vector<int>> adj2(n);
vector<int> deg(n);
for (int i = 0; i < k; i++) {
int l, r;
cin >> l >> r;
deg[i] = l;
while (l--) {
int x;
cin >> x;
x--;
adj1[x].emplace_back(i);
}
while (r--) {
int x;
cin >> x;
x--;
adj2[i].emplace_back(x);
}
}
vector<bool> has(n);
queue<int> que;
for (int i = 0; i < m; i++) {
if (!has[a[i]]) {
has[a[i]] = 1;
for (int x : adj1[a[i]]) {
deg[x] -= 1;
if (deg[x] == 0) {
que.emplace(x);
}
}
}
}
while (!que.empty()) {
int u = que.front();
que.pop();
for (int v : adj2[u]) {
if (!has[v]) {
has[v] = 1;
for (int x : adj1[v]) {
deg[x] -= 1;
if (deg[x] == 0) {
que.emplace(x);
}
}
}
}
}
vector<int> res;
for (int i = 0; i < n; i++) {
if (has[i]) {
res.emplace_back(i);
}
}
cout << (int)res.size() << '\n';
for (int x : res) {
cout << x + 1 << ' ';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |