#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];
}
}
Részfeladat | Összpont | Teszt | Verdikt | Idő | Memória | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Elfogadva | 3ms | 1824 KiB | ||||
2 | Elfogadva | 3ms | 2016 KiB | ||||
3 | Elfogadva | 4ms | 3184 KiB | ||||
subtask2 | 5/5 | ||||||
4 | Elfogadva | 90ms | 23456 KiB | ||||
5 | Elfogadva | 59ms | 22696 KiB | ||||
6 | Elfogadva | 50ms | 22776 KiB | ||||
subtask3 | 15/15 | ||||||
7 | Elfogadva | 3ms | 3128 KiB | ||||
8 | Elfogadva | 3ms | 3296 KiB | ||||
9 | Elfogadva | 3ms | 3388 KiB | ||||
10 | Elfogadva | 3ms | 3472 KiB | ||||
11 | Elfogadva | 3ms | 3476 KiB | ||||
12 | Elfogadva | 3ms | 3480 KiB | ||||
subtask4 | 10/10 | ||||||
13 | Elfogadva | 3ms | 3500 KiB | ||||
14 | Elfogadva | 2ms | 3588 KiB | ||||
15 | Elfogadva | 3ms | 3688 KiB | ||||
16 | Elfogadva | 2ms | 3692 KiB | ||||
17 | Elfogadva | 2ms | 3684 KiB | ||||
subtask5 | 35/35 | ||||||
18 | Elfogadva | 3ms | 3928 KiB | ||||
19 | Elfogadva | 3ms | 4136 KiB | ||||
20 | Elfogadva | 85ms | 27080 KiB | ||||
21 | Elfogadva | 86ms | 26912 KiB | ||||
22 | Elfogadva | 3ms | 3916 KiB | ||||
23 | Elfogadva | 3ms | 4240 KiB | ||||
24 | Elfogadva | 65ms | 26940 KiB | ||||
subtask6 | 35/35 | ||||||
25 | Elfogadva | 93ms | 25184 KiB | ||||
26 | Elfogadva | 92ms | 25376 KiB | ||||
27 | Elfogadva | 86ms | 25568 KiB | ||||
28 | Elfogadva | 82ms | 25512 KiB | ||||
29 | Elfogadva | 68ms | 26536 KiB | ||||
30 | Elfogadva | 94ms | 26112 KiB | ||||
31 | Elfogadva | 92ms | 25732 KiB | ||||
32 | Elfogadva | 75ms | 26516 KiB | ||||
33 | Elfogadva | 71ms | 27140 KiB | ||||
34 | Elfogadva | 85ms | 26056 KiB | ||||
35 | Elfogadva | 71ms | 25712 KiB |