50082023-04-09 01:42:11TomaSajtBürokrácia (40)cpp17Accepted 40/4021ms7080 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0), ios::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> in_deg(n + 1);
  vector<int> target(n + 1, -1);
  vector<bool> removed(n + 1);
  for (int i = 1; i <= n; i++) {
    char c;
    cin >> c;
    if (c == 'R')
      continue;
    int t;
    cin >> t;
    target[i] = t;
    in_deg[t]++;
  }
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (in_deg[i] == 0)
      q.push(i);
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (target[u] == -1 || removed[target[u]])
      continue;
    removed[target[u]] = true;

    int tt = target[target[u]];
    if (tt == -1)
      continue;

    in_deg[tt]--;
    if (in_deg[tt] == 0) {
      q.push(tt);
    }
  }
  cout << count(removed.begin() + 1, removed.end(), false) << endl;
  for (int i = 1; i <= n; i++) {
    if (!removed[i])
      cout << i << ' ';
  }
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1972 KiB
2Accepted1/12ms2188 KiB
3Accepted1/13ms2340 KiB
4Accepted1/13ms2548 KiB
5Accepted1/114ms5188 KiB
6Accepted1/119ms4396 KiB
7Accepted1/118ms4612 KiB
8Accepted2/219ms5056 KiB
9Accepted2/220ms5204 KiB
10Accepted2/221ms5528 KiB
11Accepted2/221ms5836 KiB
12Accepted2/214ms6108 KiB
13Accepted2/214ms6108 KiB
14Accepted2/220ms6020 KiB
15Accepted2/218ms6364 KiB
16Accepted2/214ms6520 KiB
17Accepted2/214ms6716 KiB
18Accepted2/214ms6660 KiB
19Accepted2/214ms6956 KiB
20Accepted2/214ms6972 KiB
21Accepted2/213ms7080 KiB
22Accepted2/218ms6076 KiB
23Accepted4/421ms6572 KiB