#include <bits/stdc++.h> using namespace std; #define ull unsigned long long const int N = 1005; const int M = 10005; int n, m, ex[M], ey[M], was[N], inDeg[N], root[N]; char et[M]; vector<int> g[N], gn[N], g1[N]; bool dfsCycle(int v) { was[v] = 1; for (auto& to : g[v]) { if (!was[to]) { if (dfsCycle(to)) return true; } if (was[to] == 1) return true; } was[v] = 2; return false; } bool isCycle() { fill(was, was + n, 0); for (int i = 0; i < n; ++i) { if (!was[i] && dfsCycle(i)) { return true; } } return false; } int dfsCheck(int v) { was[v] = true; int result = 1; for (auto& to : g[v]) { if (!was[to]) { result += dfsCheck(to); } } return result; } bool dfsFind(int v, int ver) { if (v == ver) return true; was[v] = true; for (auto& to : g[v]) { if (!was[to]) { if (dfsFind(to, ver)) return true; } } return false; } bool checkResult(vector<int> result) { if ((int)result.size() != n) return false; int root = -1; for (int i = 0; i < (int)result.size(); ++i) { if (result[i] == 0) { if (root != -1) return false; root = i; } if (result[i] < 0 || result[i] > n) return false; } for (int i = 0; i < n; ++i) { g[i].clear(); } fill(was, was + n, 0); for (int i = 0; i < (int)result.size(); ++i) { if (result[i] > 0) { g[result[i] - 1].push_back(i); } } int val = dfsCheck(root); if (val != n) return false; for (int i = 0; i < m; ++i) { fill(was, was + n, 0); bool val = dfsFind(ey[i], ex[i]); if (val && et[i] == 'N') return false; if (!val && et[i] == 'T') return false; } return true; } int vis[N], timer; void dfs(int v, int timer) { vis[v] = timer; for (auto& to : gn[v]) { if (!was[to] && vis[to] != timer) dfs(to, timer); } } bool rec(int v, int par, vector<int>& result) { bool ok = true; was[v] = true; result[v] = par + 1; vector<int> ver; dfs(v, ++timer); for (auto& to : g[v]) { --inDeg[to]; } if (par != -1) { for (int i = 0; i < n; ++i) { if (!was[i] && !inDeg[i] && vis[i] == timer) ver.push_back(i); } } else { ver.clear(); for (int i = 0; i < n; ++i) { if (!inDeg[i] && !was[i]) ver.push_back(i); } } int val = timer; for (auto& i : ver) { if (!was[i] && !inDeg[i]) { if (vis[i] <= val) { dfs(i, ++timer); } int cur = vis[i]; bool good = true; for (auto& to : g1[i]) { if (vis[to] == cur) { good = false; break; } } if (good) ok &= rec(i, v, result); } } for (auto& i : ver) if (!was[i]) { ok = false; break; } return ok; } void solve() { cin >> n >> m; for (int i = 0; i < n; ++i) { root[i] = 0; } for (int i = 0; i < m; ++i) { cin >> ex[i] >> ey[i] >> et[i]; --ex[i]; --ey[i]; if (et[i] == 'T') { gn[ex[i]].push_back(ey[i]); gn[ey[i]].push_back(ex[i]); g[ey[i]].push_back(ex[i]); ++inDeg[ex[i]]; } else { g1[ey[i]].push_back(ex[i]); ++root[ey[i]]; } } if (isCycle()) { cout << "NIE\n"; return; } vector<int> result(n); fill(was, was + n, 0); bool found = false; for (int v = 0; v < n; ++v) { if (!inDeg[v] && !root[v]) { found = rec(v, -1, result); break; } } if (!found) { cout << "NIE\n"; return; } //cout << "TAK\n"; //assert(checkResult(result)); for (auto& cur : result) cout << cur << '\n'; } int main() { ios::sync_with_stdio(false); int z = 1; while (z--) solve(); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | #include <bits/stdc++.h> using namespace std; #define ull unsigned long long const int N = 1005; const int M = 10005; int n, m, ex[M], ey[M], was[N], inDeg[N], root[N]; char et[M]; vector<int> g[N], gn[N], g1[N]; bool dfsCycle(int v) { was[v] = 1; for (auto& to : g[v]) { if (!was[to]) { if (dfsCycle(to)) return true; } if (was[to] == 1) return true; } was[v] = 2; return false; } bool isCycle() { fill(was, was + n, 0); for (int i = 0; i < n; ++i) { if (!was[i] && dfsCycle(i)) { return true; } } return false; } int dfsCheck(int v) { was[v] = true; int result = 1; for (auto& to : g[v]) { if (!was[to]) { result += dfsCheck(to); } } return result; } bool dfsFind(int v, int ver) { if (v == ver) return true; was[v] = true; for (auto& to : g[v]) { if (!was[to]) { if (dfsFind(to, ver)) return true; } } return false; } bool checkResult(vector<int> result) { if ((int)result.size() != n) return false; int root = -1; for (int i = 0; i < (int)result.size(); ++i) { if (result[i] == 0) { if (root != -1) return false; root = i; } if (result[i] < 0 || result[i] > n) return false; } for (int i = 0; i < n; ++i) { g[i].clear(); } fill(was, was + n, 0); for (int i = 0; i < (int)result.size(); ++i) { if (result[i] > 0) { g[result[i] - 1].push_back(i); } } int val = dfsCheck(root); if (val != n) return false; for (int i = 0; i < m; ++i) { fill(was, was + n, 0); bool val = dfsFind(ey[i], ex[i]); if (val && et[i] == 'N') return false; if (!val && et[i] == 'T') return false; } return true; } int vis[N], timer; void dfs(int v, int timer) { vis[v] = timer; for (auto& to : gn[v]) { if (!was[to] && vis[to] != timer) dfs(to, timer); } } bool rec(int v, int par, vector<int>& result) { bool ok = true; was[v] = true; result[v] = par + 1; vector<int> ver; dfs(v, ++timer); for (auto& to : g[v]) { --inDeg[to]; } if (par != -1) { for (int i = 0; i < n; ++i) { if (!was[i] && !inDeg[i] && vis[i] == timer) ver.push_back(i); } } else { ver.clear(); for (int i = 0; i < n; ++i) { if (!inDeg[i] && !was[i]) ver.push_back(i); } } int val = timer; for (auto& i : ver) { if (!was[i] && !inDeg[i]) { if (vis[i] <= val) { dfs(i, ++timer); } int cur = vis[i]; bool good = true; for (auto& to : g1[i]) { if (vis[to] == cur) { good = false; break; } } if (good) ok &= rec(i, v, result); } } for (auto& i : ver) if (!was[i]) { ok = false; break; } return ok; } void solve() { cin >> n >> m; for (int i = 0; i < n; ++i) { root[i] = 0; } for (int i = 0; i < m; ++i) { cin >> ex[i] >> ey[i] >> et[i]; --ex[i]; --ey[i]; if (et[i] == 'T') { gn[ex[i]].push_back(ey[i]); gn[ey[i]].push_back(ex[i]); g[ey[i]].push_back(ex[i]); ++inDeg[ex[i]]; } else { g1[ey[i]].push_back(ex[i]); ++root[ey[i]]; } } if (isCycle()) { cout << "NIE\n"; return; } vector<int> result(n); fill(was, was + n, 0); bool found = false; for (int v = 0; v < n; ++v) { if (!inDeg[v] && !root[v]) { found = rec(v, -1, result); break; } } if (!found) { cout << "NIE\n"; return; } //cout << "TAK\n"; //assert(checkResult(result)); for (auto& cur : result) cout << cur << '\n'; } int main() { ios::sync_with_stdio(false); int z = 1; while (z--) solve(); return 0; } |