fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct edge{
  5. int l, r;
  6. vector<int> a, b;
  7. edge(int x, int y): a(x), b(y) {l = x; r = y;};
  8. };
  9.  
  10. int main(){
  11. cin.tie(0)->sync_with_stdio(0);
  12. int n, m, k;
  13. cin >> n >> m;
  14. vector<bool> ans(n, false);
  15. while (m--){
  16. int x;
  17. cin >> x;
  18. ans[x-1] = true;
  19. }
  20.  
  21. cin >> k;
  22. vector<int> prog(k, 0);
  23. vector<vector<int>> need(n);
  24. vector<edge> es;
  25. queue<int> q;
  26. for (int j = 0; j < k; j++){
  27. int l, r;
  28. cin >> l >> r;
  29. edge e(l, r);
  30. for (auto& i: e.a){
  31. cin >> i;
  32. i--;
  33. need[i].push_back(j);
  34. if (ans[i]) prog[j]++;
  35. }
  36. for (auto& i: e.b) cin >> i;
  37. if (prog[j] == l){
  38. q.push(j);
  39. }
  40. es.push_back(e);
  41. }
  42.  
  43. //code
  44. //cout << "-----\n";
  45. while (!q.empty()){
  46. edge e = es[q.front()]; q.pop();
  47. for (auto& i: e.b){
  48. i--;
  49. if (ans[i]) continue;
  50. ans[i] = true;
  51. for (auto& j: need[i]){
  52. prog[j]++;
  53. if (prog[j] == es[j].l) q.push(j);
  54. }
  55. }
  56. }
  57.  
  58. //ans
  59. int cnt = 0;
  60. for (int i = 0; i < n; i++) if (ans[i]) cnt++;
  61. cout << cnt << '\n';
  62. for (int i = 0; i < n; i++) if (ans[i]) cout << i + 1 << ' ';
  63. }
Success #stdin #stdout 0.01s 5516KB
stdin
4 2
1 2
2
2 1
1 2
3
2 1
1 3
4
stdout
4
1 2 3 4