49832023-04-08 14:02:12TomaSajtDinókcpp17Accepted 100/10094ms27140 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];
  }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
2Accepted3ms2016 KiB
3Accepted4ms3184 KiB
subtask25/5
4Accepted90ms23456 KiB
5Accepted59ms22696 KiB
6Accepted50ms22776 KiB
subtask315/15
7Accepted3ms3128 KiB
8Accepted3ms3296 KiB
9Accepted3ms3388 KiB
10Accepted3ms3472 KiB
11Accepted3ms3476 KiB
12Accepted3ms3480 KiB
subtask410/10
13Accepted3ms3500 KiB
14Accepted2ms3588 KiB
15Accepted3ms3688 KiB
16Accepted2ms3692 KiB
17Accepted2ms3684 KiB
subtask535/35
18Accepted3ms3928 KiB
19Accepted3ms4136 KiB
20Accepted85ms27080 KiB
21Accepted86ms26912 KiB
22Accepted3ms3916 KiB
23Accepted3ms4240 KiB
24Accepted65ms26940 KiB
subtask635/35
25Accepted93ms25184 KiB
26Accepted92ms25376 KiB
27Accepted86ms25568 KiB
28Accepted82ms25512 KiB
29Accepted68ms26536 KiB
30Accepted94ms26112 KiB
31Accepted92ms25732 KiB
32Accepted75ms26516 KiB
33Accepted71ms27140 KiB
34Accepted85ms26056 KiB
35Accepted71ms25712 KiB