4983 2023. 04. 08 14:02:12 TomaSajt Dinók cpp17 Accepted 100/100 94ms 27140 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0), ios::sync_with_stdio(0);

  int n, m;
  cin >> n >> m;

  // dependency graph where edge `i-->j` means `j` depends on `i`
  // `i = 2 * a` is the appearance of the `a` species
  // `i = 2 * a + 1` is the extinction of the `a` species
  vector<vector<int>> g(2 * n);

  // number of dependencies of node `i`
  vector<int> deps(2 * n);

  auto makeEdge = [&](int u, int v) {
    g[u].push_back(v);
    deps[v]++;
  };

  for (int i = 0; i < n; i++) {
    // `i` had to have appeared before going extinct
    makeEdge(2 * i, 2 * i + 1);
  }

  while (m--) {
    int op, a, b;
    cin >> op >> a >> b;
    --a, --b;
    if (op == 1) {
      // `b` appeared before `a` went extinct
      makeEdge(2 * b, 2 * a + 1);
      // `a` appeared before `b` went extinct
      makeEdge(2 * a, 2 * b + 1);
    } else {
      // `a` went extinct before `b` appeared
      makeEdge(2 * a + 1, 2 * b);
    }
  }

  // try to traverse DAG

  queue<int> q;
  for (int i = 0; i < n; i++) {
    if (deps[2 * i] == 0) {
      q.push(2 * i);
    }
  }

  int t = 0;
  vector<int> pos(2 * n);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    pos[u] = ++t;
    for (int v : g[u]) {
      if (--deps[v] == 0) {
        q.push(v);
      }
    }
  }

  // if not all nodes were traversed, it is impossible
  if (t != 2 * n) {
    cout << "NEM";
    return 0;
  }

  cout << "IGEN";
  for (int i = 0; i < n; i++) {
    cout << '\n' << pos[2 * i] << ' ' << pos[2 * i + 1];
  }
}
Subtask Sum Test Verdict Time Memory
subtask1 0/0
1 Accepted 3ms 1824 KiB
2 Accepted 3ms 2016 KiB
3 Accepted 4ms 3184 KiB
subtask2 5/5
4 Accepted 90ms 23456 KiB
5 Accepted 59ms 22696 KiB
6 Accepted 50ms 22776 KiB
subtask3 15/15
7 Accepted 3ms 3128 KiB
8 Accepted 3ms 3296 KiB
9 Accepted 3ms 3388 KiB
10 Accepted 3ms 3472 KiB
11 Accepted 3ms 3476 KiB
12 Accepted 3ms 3480 KiB
subtask4 10/10
13 Accepted 3ms 3500 KiB
14 Accepted 2ms 3588 KiB
15 Accepted 3ms 3688 KiB
16 Accepted 2ms 3692 KiB
17 Accepted 2ms 3684 KiB
subtask5 35/35
18 Accepted 3ms 3928 KiB
19 Accepted 3ms 4136 KiB
20 Accepted 85ms 27080 KiB
21 Accepted 86ms 26912 KiB
22 Accepted 3ms 3916 KiB
23 Accepted 3ms 4240 KiB
24 Accepted 65ms 26940 KiB
subtask6 35/35
25 Accepted 93ms 25184 KiB
26 Accepted 92ms 25376 KiB
27 Accepted 86ms 25568 KiB
28 Accepted 82ms 25512 KiB
29 Accepted 68ms 26536 KiB
30 Accepted 94ms 26112 KiB
31 Accepted 92ms 25732 KiB
32 Accepted 75ms 26516 KiB
33 Accepted 71ms 27140 KiB
34 Accepted 85ms 26056 KiB
35 Accepted 71ms 25712 KiB