#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define i2 array<int,2>
#define PB push_back
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const int N = 100100;
const ll OO = 1e18;
const int md = int(1e9) + 7;
vector<int> vc[N], who[N], fr[N];
queue<int> q;
int n, m, k, cnt[N], ans = 0;
bool mrk[N], was[N];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> m;
for (int i = 0; i < m; i++){
int x; cin >> x;
mrk[x] = 1;
ans++;
}
cin >> k;
for (int i = 0; i < k; i++){
int L, R; cin >> L >> R;
cnt[i] = L;
for (int j = 0; j < L; j++){
int x; cin >> x;
who[x].PB(i);
fr[i].PB(x);
if (mrk[x]) cnt[i]--;
}
if (cnt[i] == 0) q.push(i);
for (int j = 0; j < R; j++) {
int x; cin >> x;
vc[i].PB(x);
}
}
while (sz(q)){
int id = q.front(); q.pop();
for (int cr : vc[id]){
if (mrk[cr]) continue;
mrk[cr] = 1;
ans++;
for (int nw : who[cr]){
cnt[nw]--;
if (cnt[nw] == 0)
q.push(nw);
}
}
}
cout << ans << '\n';
for (int i = 1; i <= n; i++)
if (mrk[i])
cout << i << " ";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
9216 KB |
Output is correct |
2 |
Correct |
47 ms |
10112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
11768 KB |
Output is correct |
2 |
Correct |
77 ms |
12280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
13304 KB |
Output is correct |
2 |
Correct |
71 ms |
12024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
119 ms |
13944 KB |
Output is correct |
2 |
Correct |
123 ms |
13432 KB |
Output is correct |