#include <iostream> #include <vector> #include <cstdlib> #include <cstring> using namespace std; const int N = 1001; int n, m, a, b, par[N]; char c; vector<int> visited, no[N], yes[N], revyes[N]; void dfs(int a, bool *vis, bool *present) { vis[a] = true; visited.push_back(a); for (int b: yes[a]) if (!vis[b] && present[b]) dfs(b, vis, present); for (int b: revyes[a]) if (!vis[b] && present[b]) dfs(b, vis, present); } int gen(vector<int> v) { bool bad[n+1], present[n+1]; memset(bad+1, 0, n*sizeof(bool)); memset(present+1, 0, n*sizeof(bool)); for (int x: v) present[x] = true; for (int x: v) for (int y: no[x]) bad[y] = true; for (int x: v) for (int y: revyes[x]) bad[y] = true; bool vis[n+1]; memset(vis+1, 0, n*sizeof(bool)); for (int root: v) { if (bad[root]) continue; vis[root] = true; for (int x: v) { if (vis[x]) continue; visited.clear(); dfs(x, vis, present); par[gen(visited)] = root; } return root; } cout << "NIE\n"; exit(0); } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; while (m--) { cin >> a >> b >> c; if (c == 'T') { yes[a].push_back(b); revyes[b].push_back(a); } else no[a].push_back(b); } vector<int> all(n); for (int i=0; i<n; i++) all[i] = i+1; gen(all); for (int i=1; i<=n; i++) cout << par[i] << "\n"; // cout << "TAK\n"; 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 | #include <iostream> #include <vector> #include <cstdlib> #include <cstring> using namespace std; const int N = 1001; int n, m, a, b, par[N]; char c; vector<int> visited, no[N], yes[N], revyes[N]; void dfs(int a, bool *vis, bool *present) { vis[a] = true; visited.push_back(a); for (int b: yes[a]) if (!vis[b] && present[b]) dfs(b, vis, present); for (int b: revyes[a]) if (!vis[b] && present[b]) dfs(b, vis, present); } int gen(vector<int> v) { bool bad[n+1], present[n+1]; memset(bad+1, 0, n*sizeof(bool)); memset(present+1, 0, n*sizeof(bool)); for (int x: v) present[x] = true; for (int x: v) for (int y: no[x]) bad[y] = true; for (int x: v) for (int y: revyes[x]) bad[y] = true; bool vis[n+1]; memset(vis+1, 0, n*sizeof(bool)); for (int root: v) { if (bad[root]) continue; vis[root] = true; for (int x: v) { if (vis[x]) continue; visited.clear(); dfs(x, vis, present); par[gen(visited)] = root; } return root; } cout << "NIE\n"; exit(0); } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; while (m--) { cin >> a >> b >> c; if (c == 'T') { yes[a].push_back(b); revyes[b].push_back(a); } else no[a].push_back(b); } vector<int> all(n); for (int i=0; i<n; i++) all[i] = i+1; gen(all); for (int i=1; i<=n; i++) cout << par[i] << "\n"; // cout << "TAK\n"; return 0; } |