#include <cstdio> #include <algorithm> #include <vector> #include <cstdlib> using namespace std; const int debug = 0; struct person { vector<int> up /* bosses */, down, other /* up or down */, notup /* N */; int s; // original up.size() int k; bool vis; int father, depth; }; void fail() { printf("NIE\n"); exit(0); } person p[1000]; int n; char dep[1000][1000]; // dep[i][j] = 1 (j is a boss), -1 (i is a boss), -2 (j is not a boss), 0 (unknown), 2 (one of them is a boss) #define print do { \ printf("---------- PRINT %d -----------------\n", __LINE__); \ for (int ii = 0; ii < n; ++ii) { \ for (int jj = 0; jj < n; ++jj) \ printf("%d\t", dep[ii][jj]); \ printf("\n"); \ } \ printf("*****************************************\n"); \ } while(0) vector<pair<int, int> > new_boss; // (a, b) - a is a boss void add_boss(int a, int b, bool add) { if (debug >= 1) printf("xxx add boss %d %d (%d) xxx\n", a, b, add); dep[a][b] = -1; if (dep[b][a] < 0) fail(); dep[b][a] = 1; p[a].down.push_back(b); p[b].up.push_back(a); if (add) new_boss.push_back(make_pair(a, b)); } void fix(int a, int b) { // one of them is the boss if (debug >= 3) printf("fix: %d %d\n", a, b); if (dep[a][b] == 1 || dep[a][b] == -1) return; if (dep[a][b] == -2) { // a is a boss add_boss(a, b, 1); } if (dep[b][a] == -2) { // b is a boss add_boss(b, a, 1); } if (dep[a][b] == 0 && dep[b][a] == 0) { dep[a][b] = dep[b][a] = 2; p[a].other.push_back(b); p[b].other.push_back(a); } } // dep[i][j] = 1 (j is a boss), -1 (i is a boss), -2 (j is not a boss), 0 (unknown), 2 (one of them is a boss) bool comp(int a, int b) { if (dep[a][b] == -1) // a is the boss return 1; else if (dep[a][b] != 1) return p[a].other.size() + p[a].down.size() > p[b].other.size() + p[b].down.size(); else return 0; } bool can_be_boss(int b) { bool ok = true; for (int i = 0; i < n; ++i) if (dep[i][b] < 0) { ok = false; break; } return ok; } int main() { int k; scanf("%d %d ", &n, &k); while (k--) { int x, y; char c; scanf("%d %d %c", &x, &y, &c); --x; --y; if (debug >= 3) printf("%d %d %c\n", x, y, c); if (c == 'T') { p[x].up.push_back(y); p[y].down.push_back(x); dep[x][y] = 1; dep[y][x] = -1; } else { p[x].notup.push_back(y); dep[x][y] = -2; } } vector<int> queue; int counter = 0; int id[n]; for (int i = 0; i < n; ++i) { p[i].k = p[i].s = p[i].up.size(); if (p[i].k == 0) queue.push_back(i); } if (debug >= 3) print; //topo order: from the sink while (!queue.empty()) { // update up and down; time = O(n * m) int x = queue.back(); if (debug >= 3) printf("x = %d\n", x); ++counter; id[n-counter] = x; queue.pop_back(); for (vector<int>::iterator i = p[x].down.begin(); i != p[x].down.end(); ++i) { if (!(--p[*i].k)) { queue.push_back(*i); } } int s = p[x].up.size(); for (int i = 0; i < s; ++i) { // total iterations: O(m) int y = p[x].up[i]; for (vector<int>::iterator j = p[y].up.begin(); j != p[y].up.end(); ++j) { // dep[i][j] = 1 (j is a boss), -1 (i is a boss), -2 (j is not a boss), 0 (unknown), 2 (one of them is a boss) if (dep[x][*j] < 0 || dep[*j][x] == 1) fail(); else if (dep[x][*j] != 1) { dep[x][*j] = 1; dep[*j][x] = -1; p[x].up.push_back(*j); p[*j].down.push_back(x); } } } } if (counter != n) fail(); if (debug >= 3) print; //topo order: from the source for (int i = 0; i < n; ++i) { // update notup; time = O(n * m) int x = id[i]; for (int j = 0; j < p[x].s; ++j) { int y = p[x].up[j]; for (vector<int>::iterator vit = p[x].notup.begin(); vit != p[x].notup.end(); ++vit) { // dep[i][j] = 1 (j is a boss), -1 (i is a boss), -2 (j is not a boss), 0 (unknown), 2 (one of them is a boss) if (dep[y][*vit] == 1) fail(); else if (dep[y][*vit] == 0) { dep[y][*vit] = -2; p[y].notup.push_back(*vit); } } } } if (debug >= 3) print; bool new_noup = false; do { new_noup = false; while (!new_boss.empty()) { // TODO int x = new_boss.back().first; // x is a new boss of y int y = new_boss.back().second; new_boss.pop_back(); if (debug >= 3) printf("wybrano %d %d <<<<<<<<<<<\n", x, y); int z = x; for (int i = -1; i < (int) p[x].up.size(); ++i) { if (i >= 0) z = p[x].up[i]; int z2 = y; for (int j = -1; j < (int) p[y].down.size(); ++j) { if (j >= 0) z2 = p[y].down[j]; if (debug >= 3) printf(">>>>>> z2 = %d, z = %d\n", z2, z); if (dep[z2][z] != 1) add_boss(z, z2, 0); } } for (unsigned i = 0; i < p[y].notup.size(); ++i) { z = p[y].notup[i]; int z2 = x; for (int j = -1; j < (int) p[x].up.size(); ++j) { if (j >= 0) z2 = p[x].up[j]; if (dep[z2][z] == 2) { add_boss(z2, z, 0); } else if (dep[z2][z] == 1) fail(); else if (dep[z2][z] == 0) { dep[z2][z] = -2; new_noup = true; } } } } for (int i = 0; i < n; ++i) p[i].vis = false; //topo order: from the source for (int i = 0; i < n; ++i) { // fix(a, b) int x = id[i]; if (p[x].vis) continue; if (debug >= 3) printf(" @@@ inside @@@ x = %d\n", x); p[x].vis = true; int size = p[x].up.size(); for (int a = 0; a < size; ++a) { p[p[x].up[a]].vis = true; for (int b = a+1; b < size; ++b) { fix(p[x].up[a], p[x].up[b]); } } } if (debug >= 3) print; if (new_boss.empty()) { for (int i = 0; i < n; ++i) { if (!new_boss.empty()) break; for (int j = 0; j < n; ++j) { if (!new_boss.empty()) break; if (dep[i][j] == 2) { if (debug >= 1) printf("random boss:\n"); if (can_be_boss(i)) add_boss(i, j, 1); else add_boss(j, i, 1); } } } } } while (!new_boss.empty()); //find boss of all the bosses int b; for (b = 0; b < n; ++b) { if (can_be_boss(b)) break; } if (b >= n) fail(); if (debug >= 3) printf("b = %d\n", b + 1); for (int i = 0; i < n; ++i) { if (dep[i][b] != 1) { dep[i][b] = 1; dep[b][i] = -1; p[i].up.push_back(b); p[b].down.push_back(i); } } for (int i = 0; i < n; ++i) { // clean p.other at the end unsigned j = 0; while (j < p[i].other.size()) { int y = p[i].other[j]; if (dep[i][y] != 2) { p[i].other[j] = p[i].other[p[i].other.size()-1]; p[i].other.pop_back(); } else ++j; } } if (debug >= 3) print; sort(id, id+n, comp); if (b != id[0]) { printf("ERROR!\n"); fail(); } if (debug >= 3) { printf("id:\n"); for (int i = 0; i < n; ++i) { printf("%d ", id[i] + 1); } printf("\n"); } p[b].father = -1; p[b].depth = 0; for (int i = 1; i < n; ++i) { int x = id[i]; p[x].father = b; for (unsigned j = 0; j < p[x].up.size(); ++j) { int y = p[x].up[j]; if (p[y].depth + 1 > p[x].depth) { p[x].depth = p[y].depth + 1; p[x].father = y; } } if (debug >= 3) printf("%d: father: %d\n", x+1, p[x].father+1); } //last check for (int i = 0; i < n; ++i) { int j = i; while (j != b) { j = p[j].father; if (dep[i][j] < 0) { fail(); } dep[i][j] = 0; } for (j = 0; j < n; ++j) if (dep[i][j] == 1) { fail(); } } // printf("TAK\n"); if (debug == 0) { for (int i = 0; i < n; ++i) printf("%d\n", p[i].father + 1); } 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | #include <cstdio> #include <algorithm> #include <vector> #include <cstdlib> using namespace std; const int debug = 0; struct person { vector<int> up /* bosses */, down, other /* up or down */, notup /* N */; int s; // original up.size() int k; bool vis; int father, depth; }; void fail() { printf("NIE\n"); exit(0); } person p[1000]; int n; char dep[1000][1000]; // dep[i][j] = 1 (j is a boss), -1 (i is a boss), -2 (j is not a boss), 0 (unknown), 2 (one of them is a boss) #define print do { \ printf("---------- PRINT %d -----------------\n", __LINE__); \ for (int ii = 0; ii < n; ++ii) { \ for (int jj = 0; jj < n; ++jj) \ printf("%d\t", dep[ii][jj]); \ printf("\n"); \ } \ printf("*****************************************\n"); \ } while(0) vector<pair<int, int> > new_boss; // (a, b) - a is a boss void add_boss(int a, int b, bool add) { if (debug >= 1) printf("xxx add boss %d %d (%d) xxx\n", a, b, add); dep[a][b] = -1; if (dep[b][a] < 0) fail(); dep[b][a] = 1; p[a].down.push_back(b); p[b].up.push_back(a); if (add) new_boss.push_back(make_pair(a, b)); } void fix(int a, int b) { // one of them is the boss if (debug >= 3) printf("fix: %d %d\n", a, b); if (dep[a][b] == 1 || dep[a][b] == -1) return; if (dep[a][b] == -2) { // a is a boss add_boss(a, b, 1); } if (dep[b][a] == -2) { // b is a boss add_boss(b, a, 1); } if (dep[a][b] == 0 && dep[b][a] == 0) { dep[a][b] = dep[b][a] = 2; p[a].other.push_back(b); p[b].other.push_back(a); } } // dep[i][j] = 1 (j is a boss), -1 (i is a boss), -2 (j is not a boss), 0 (unknown), 2 (one of them is a boss) bool comp(int a, int b) { if (dep[a][b] == -1) // a is the boss return 1; else if (dep[a][b] != 1) return p[a].other.size() + p[a].down.size() > p[b].other.size() + p[b].down.size(); else return 0; } bool can_be_boss(int b) { bool ok = true; for (int i = 0; i < n; ++i) if (dep[i][b] < 0) { ok = false; break; } return ok; } int main() { int k; scanf("%d %d ", &n, &k); while (k--) { int x, y; char c; scanf("%d %d %c", &x, &y, &c); --x; --y; if (debug >= 3) printf("%d %d %c\n", x, y, c); if (c == 'T') { p[x].up.push_back(y); p[y].down.push_back(x); dep[x][y] = 1; dep[y][x] = -1; } else { p[x].notup.push_back(y); dep[x][y] = -2; } } vector<int> queue; int counter = 0; int id[n]; for (int i = 0; i < n; ++i) { p[i].k = p[i].s = p[i].up.size(); if (p[i].k == 0) queue.push_back(i); } if (debug >= 3) print; //topo order: from the sink while (!queue.empty()) { // update up and down; time = O(n * m) int x = queue.back(); if (debug >= 3) printf("x = %d\n", x); ++counter; id[n-counter] = x; queue.pop_back(); for (vector<int>::iterator i = p[x].down.begin(); i != p[x].down.end(); ++i) { if (!(--p[*i].k)) { queue.push_back(*i); } } int s = p[x].up.size(); for (int i = 0; i < s; ++i) { // total iterations: O(m) int y = p[x].up[i]; for (vector<int>::iterator j = p[y].up.begin(); j != p[y].up.end(); ++j) { // dep[i][j] = 1 (j is a boss), -1 (i is a boss), -2 (j is not a boss), 0 (unknown), 2 (one of them is a boss) if (dep[x][*j] < 0 || dep[*j][x] == 1) fail(); else if (dep[x][*j] != 1) { dep[x][*j] = 1; dep[*j][x] = -1; p[x].up.push_back(*j); p[*j].down.push_back(x); } } } } if (counter != n) fail(); if (debug >= 3) print; //topo order: from the source for (int i = 0; i < n; ++i) { // update notup; time = O(n * m) int x = id[i]; for (int j = 0; j < p[x].s; ++j) { int y = p[x].up[j]; for (vector<int>::iterator vit = p[x].notup.begin(); vit != p[x].notup.end(); ++vit) { // dep[i][j] = 1 (j is a boss), -1 (i is a boss), -2 (j is not a boss), 0 (unknown), 2 (one of them is a boss) if (dep[y][*vit] == 1) fail(); else if (dep[y][*vit] == 0) { dep[y][*vit] = -2; p[y].notup.push_back(*vit); } } } } if (debug >= 3) print; bool new_noup = false; do { new_noup = false; while (!new_boss.empty()) { // TODO int x = new_boss.back().first; // x is a new boss of y int y = new_boss.back().second; new_boss.pop_back(); if (debug >= 3) printf("wybrano %d %d <<<<<<<<<<<\n", x, y); int z = x; for (int i = -1; i < (int) p[x].up.size(); ++i) { if (i >= 0) z = p[x].up[i]; int z2 = y; for (int j = -1; j < (int) p[y].down.size(); ++j) { if (j >= 0) z2 = p[y].down[j]; if (debug >= 3) printf(">>>>>> z2 = %d, z = %d\n", z2, z); if (dep[z2][z] != 1) add_boss(z, z2, 0); } } for (unsigned i = 0; i < p[y].notup.size(); ++i) { z = p[y].notup[i]; int z2 = x; for (int j = -1; j < (int) p[x].up.size(); ++j) { if (j >= 0) z2 = p[x].up[j]; if (dep[z2][z] == 2) { add_boss(z2, z, 0); } else if (dep[z2][z] == 1) fail(); else if (dep[z2][z] == 0) { dep[z2][z] = -2; new_noup = true; } } } } for (int i = 0; i < n; ++i) p[i].vis = false; //topo order: from the source for (int i = 0; i < n; ++i) { // fix(a, b) int x = id[i]; if (p[x].vis) continue; if (debug >= 3) printf(" @@@ inside @@@ x = %d\n", x); p[x].vis = true; int size = p[x].up.size(); for (int a = 0; a < size; ++a) { p[p[x].up[a]].vis = true; for (int b = a+1; b < size; ++b) { fix(p[x].up[a], p[x].up[b]); } } } if (debug >= 3) print; if (new_boss.empty()) { for (int i = 0; i < n; ++i) { if (!new_boss.empty()) break; for (int j = 0; j < n; ++j) { if (!new_boss.empty()) break; if (dep[i][j] == 2) { if (debug >= 1) printf("random boss:\n"); if (can_be_boss(i)) add_boss(i, j, 1); else add_boss(j, i, 1); } } } } } while (!new_boss.empty()); //find boss of all the bosses int b; for (b = 0; b < n; ++b) { if (can_be_boss(b)) break; } if (b >= n) fail(); if (debug >= 3) printf("b = %d\n", b + 1); for (int i = 0; i < n; ++i) { if (dep[i][b] != 1) { dep[i][b] = 1; dep[b][i] = -1; p[i].up.push_back(b); p[b].down.push_back(i); } } for (int i = 0; i < n; ++i) { // clean p.other at the end unsigned j = 0; while (j < p[i].other.size()) { int y = p[i].other[j]; if (dep[i][y] != 2) { p[i].other[j] = p[i].other[p[i].other.size()-1]; p[i].other.pop_back(); } else ++j; } } if (debug >= 3) print; sort(id, id+n, comp); if (b != id[0]) { printf("ERROR!\n"); fail(); } if (debug >= 3) { printf("id:\n"); for (int i = 0; i < n; ++i) { printf("%d ", id[i] + 1); } printf("\n"); } p[b].father = -1; p[b].depth = 0; for (int i = 1; i < n; ++i) { int x = id[i]; p[x].father = b; for (unsigned j = 0; j < p[x].up.size(); ++j) { int y = p[x].up[j]; if (p[y].depth + 1 > p[x].depth) { p[x].depth = p[y].depth + 1; p[x].father = y; } } if (debug >= 3) printf("%d: father: %d\n", x+1, p[x].father+1); } //last check for (int i = 0; i < n; ++i) { int j = i; while (j != b) { j = p[j].father; if (dep[i][j] < 0) { fail(); } dep[i][j] = 0; } for (j = 0; j < n; ++j) if (dep[i][j] == 1) { fail(); } } // printf("TAK\n"); if (debug == 0) { for (int i = 0; i < n; ++i) printf("%d\n", p[i].father + 1); } return 0; } |