#include <iostream> #include <cstdio> #include <vector> using namespace std; char c; bool czy, tutaj; int n, m, a, b, x; int BFS[1005]; int tab[1005][1005]; int result[1005]; bool ktore[1005]; vector <int> V[1005]; vector <int> T[1005]; vector <int>::iterator it; bool probuj (vector <int> v, int ojciec) { if (v.size() == 1) { result[v[0]] = ojciec; return 1; } for (int i = 0; i < v.size(); ++i) { bool dobry = 1; for (int j = 0; j < v.size(); ++j) { if (tab[v[j]][v[i]] == -1) { dobry = 0; break; } } if (dobry) { int xx = v[i]; result[xx] = ojciec; int odw[1005]; for (int j = 0; j < v.size(); ++j) odw[v[j]] = 0; odw[xx] = 1; for (int j = 0; j < v.size(); ++j) if (odw[v[j]] == 0) { BFS[a = b = 0] = v[j]; odw[v[j]] = 1; vector <int> w; w.clear(); w.push_back(v[j]); while (a <= b) { int xxx = BFS[a]; for (vector <int>::iterator iter = T[xxx].begin(); iter != T[xxx].end(); ++iter) { if ((ktore[*iter] == 1) && (odw[*iter] == 0)) { b++; odw[*iter] = 1; BFS[b] = *iter; w.push_back(*iter); } } a++; } for (int k = 0; k < v.size(); ++k) ktore[v[k]] = 0; for (int k = 0; k < w.size(); ++k) ktore[w[k]] = 1; dobry = probuj(w, xx); for (int k = 0; k < v.size(); ++k) ktore[v[k]] = 1; if (dobry == false) break; } } if (dobry) return true; } return false; } int main () { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) tab[i][i] = -1; for (int i = 0; i < m; ++i) { scanf("%d %d %c", &a, &b, &c); if (c == 'N') tab[a][b] = -1; else { V[a].push_back(b); T[a].push_back(b); T[b].push_back(a); } } for (int i = 1; i <= n; ++i) { BFS[a = b = 0] = i; while (a <= b) { x = BFS[a]; for (it = V[x].begin(); it != V[x].end(); ++it) { if (tab[i][*it] == -1) { printf("NIE\n"); return 0; } if (tab[i][*it] == 0) { tab[i][*it] = 1; BFS[++b] = *it; } } a++; } } for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { if (tab[i][j] == 1) { if (tab[j][i] == 1) { printf("NIE\n"); return 0; } tab[j][i] = -1; } } for (int i = 1; i <= n; ++i) tab[i][i] = 1; for (int i = 1; i <= n; ++i) { tutaj = 0; for (int j = 1; j <= n; ++j) { if (tab[j][i] == -1) { tutaj = 1; break; } } if (tutaj == 0) { czy = 1; break; } } if (czy == 0) { printf("NIE\n"); return 0; } for (int i = 1; i <= n; ++i) V[0].push_back(i); for (int i = 1; i <= n; ++i) ktore[i] = 1; if (probuj(V[0], 0) == false) { printf("NIE\n"); return 0; } for (int i = 1; i <= n; ++i) printf("%d\n", result[i]); 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 | #include <iostream> #include <cstdio> #include <vector> using namespace std; char c; bool czy, tutaj; int n, m, a, b, x; int BFS[1005]; int tab[1005][1005]; int result[1005]; bool ktore[1005]; vector <int> V[1005]; vector <int> T[1005]; vector <int>::iterator it; bool probuj (vector <int> v, int ojciec) { if (v.size() == 1) { result[v[0]] = ojciec; return 1; } for (int i = 0; i < v.size(); ++i) { bool dobry = 1; for (int j = 0; j < v.size(); ++j) { if (tab[v[j]][v[i]] == -1) { dobry = 0; break; } } if (dobry) { int xx = v[i]; result[xx] = ojciec; int odw[1005]; for (int j = 0; j < v.size(); ++j) odw[v[j]] = 0; odw[xx] = 1; for (int j = 0; j < v.size(); ++j) if (odw[v[j]] == 0) { BFS[a = b = 0] = v[j]; odw[v[j]] = 1; vector <int> w; w.clear(); w.push_back(v[j]); while (a <= b) { int xxx = BFS[a]; for (vector <int>::iterator iter = T[xxx].begin(); iter != T[xxx].end(); ++iter) { if ((ktore[*iter] == 1) && (odw[*iter] == 0)) { b++; odw[*iter] = 1; BFS[b] = *iter; w.push_back(*iter); } } a++; } for (int k = 0; k < v.size(); ++k) ktore[v[k]] = 0; for (int k = 0; k < w.size(); ++k) ktore[w[k]] = 1; dobry = probuj(w, xx); for (int k = 0; k < v.size(); ++k) ktore[v[k]] = 1; if (dobry == false) break; } } if (dobry) return true; } return false; } int main () { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) tab[i][i] = -1; for (int i = 0; i < m; ++i) { scanf("%d %d %c", &a, &b, &c); if (c == 'N') tab[a][b] = -1; else { V[a].push_back(b); T[a].push_back(b); T[b].push_back(a); } } for (int i = 1; i <= n; ++i) { BFS[a = b = 0] = i; while (a <= b) { x = BFS[a]; for (it = V[x].begin(); it != V[x].end(); ++it) { if (tab[i][*it] == -1) { printf("NIE\n"); return 0; } if (tab[i][*it] == 0) { tab[i][*it] = 1; BFS[++b] = *it; } } a++; } } for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { if (tab[i][j] == 1) { if (tab[j][i] == 1) { printf("NIE\n"); return 0; } tab[j][i] = -1; } } for (int i = 1; i <= n; ++i) tab[i][i] = 1; for (int i = 1; i <= n; ++i) { tutaj = 0; for (int j = 1; j <= n; ++j) { if (tab[j][i] == -1) { tutaj = 1; break; } } if (tutaj == 0) { czy = 1; break; } } if (czy == 0) { printf("NIE\n"); return 0; } for (int i = 1; i <= n; ++i) V[0].push_back(i); for (int i = 1; i <= n; ++i) ktore[i] = 1; if (probuj(V[0], 0) == false) { printf("NIE\n"); return 0; } for (int i = 1; i <= n; ++i) printf("%d\n", result[i]); return 0; } |