#include <vector> #include <cstdio> #include <set> using namespace std; const int MAX = 1010; int back_T[MAX], back_N[MAX]; struct Tree { set<int> T, N; int pid; void add_T(int x) {T.insert(x);} void add_N(int x) {N.insert(x);} }; int n, m; vector<int> T[MAX], N[MAX]; int A[MAX]; int LW[MAX], POST[MAX], C[MAX], P[MAX]; Tree trees[10 * MAX]; vector<int> roots, old_roots; set<int> free_N; int V[MAX]; bool used[MAX]; bool dfs2(int x, int col) { V[x] = col; for(auto u: T[x]) { if(u == col) return false; if(V[u] < col && !dfs2(u, col)) return false; } return true; } int global_LW; int count_LW(int v) { int ret = 1 << 20; for(auto u: T[v]) { if(LW[u]) ret = min(ret, LW[u]); else ret = min(ret, count_LW(u)); } global_LW ++; POST[v] = global_LW; if(ret == 1 << 20) LW[v] = POST[v]; else LW[v] = ret; return LW[v]; } void add_to_tree(int x, int top) { //printf("add to tree %d %d\n", x, back_N[x]); trees[LW[x]].pid = top; for(auto u: N[x]) { if(LW[u] == LW[x]) { back_N[u] ++; if(back_N[u] == 1) { auto ptr = free_N.find(u); if(ptr != free_N.end()) free_N.erase(ptr); } } } } void create_trees(int dir) { used[dir] = true; roots = old_roots; for(auto x: T[dir]) { C[x] --; if(C[x] == 0) roots.push_back(x); } for(int i = 1; i <= 10 * n; i++) trees[i].pid = dir; for(auto x: roots) { if(x == dir) continue; add_to_tree(x, dir); if(back_N[x] == 0) free_N.insert(x); } } vector<int> G[MAX]; bool anc[MAX][MAX]; //anc[i][j] iff i ---- > j in the tree void dfs(int x, int top) { anc[top][x] = true; for(auto v: G[x]) dfs(v, top); } bool check_answer(int dir) { int c = 0; for(int i = 1; i <= n; i++) { //printf("%d\n", A[i]); if(A[i] != 0) G[A[i]].push_back(i); else c++; } if(c != 1) return false; for(int i = 1; i <= n; i++) dfs(i, i); for(int i = 1; i <= n; i++) for(auto x: N[i]) if(anc[x][i]) { //printf("%d %d\n", x, i); return false; } return true; } bool solve_for_dir(int dir) { int top; while(free_N.size()) { top = *free_N.begin(); free_N.erase(free_N.begin()); //printf("TOP %d\n", top); A[top] = trees[LW[top]].pid; used[top] = true; trees[LW[top]].pid = top; for(auto x: T[top]) { C[x] --; if(C[x] == 0) { if(back_N[x] == 0 && !used[x]) free_N.insert(x); add_to_tree(x, top); } } for(auto x: N[top]) { if(LW[top] == LW[x]) { back_N[x] --; if(back_N[x] == 0 && C[x] == 0 && !used[x]) free_N.insert(x); } } //for(int i = 1; i <= n; i++) printf("%d: %d\n", i, back_N[i]); } return check_answer(dir); } bool is_good_root(int dir) { for(int i = 1; i <= n; i++) { for(auto x: N[i]) if(x == dir) return false; } return true; } int main () { int a, b; char c; scanf("%d%d", &n, &m); for(int i = 0; i < m; i++) { scanf("%d%d %c", &a, &b, &c); switch(c) { case 'T': T[b].push_back(a); C[a] ++; break; case 'N': N[a].push_back(b); } } for(int i = 1; i <= n; i++) { if(C[i] == 0) old_roots.push_back(i); } for(int i = 1; i <= n; i++) { if(!dfs2(i, i)) { printf("NIE\n"); return 0; } } for(auto x: old_roots) count_LW(x); //for(int i = 1; i <= n; i++) printf("%d: %d\n", i, LW[i]); int great_root = 0; for(auto x: old_roots) if(is_good_root(x)) great_root = x; if(great_root == 0) { printf("NIE\n"); return 0; } create_trees(great_root); if(solve_for_dir(great_root)) { //printf("TAK\n"); for(int i = 1; i <= n; i++) printf("%d\n", A[i]); return 0; } printf("NIE\n"); }
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 | #include <vector> #include <cstdio> #include <set> using namespace std; const int MAX = 1010; int back_T[MAX], back_N[MAX]; struct Tree { set<int> T, N; int pid; void add_T(int x) {T.insert(x);} void add_N(int x) {N.insert(x);} }; int n, m; vector<int> T[MAX], N[MAX]; int A[MAX]; int LW[MAX], POST[MAX], C[MAX], P[MAX]; Tree trees[10 * MAX]; vector<int> roots, old_roots; set<int> free_N; int V[MAX]; bool used[MAX]; bool dfs2(int x, int col) { V[x] = col; for(auto u: T[x]) { if(u == col) return false; if(V[u] < col && !dfs2(u, col)) return false; } return true; } int global_LW; int count_LW(int v) { int ret = 1 << 20; for(auto u: T[v]) { if(LW[u]) ret = min(ret, LW[u]); else ret = min(ret, count_LW(u)); } global_LW ++; POST[v] = global_LW; if(ret == 1 << 20) LW[v] = POST[v]; else LW[v] = ret; return LW[v]; } void add_to_tree(int x, int top) { //printf("add to tree %d %d\n", x, back_N[x]); trees[LW[x]].pid = top; for(auto u: N[x]) { if(LW[u] == LW[x]) { back_N[u] ++; if(back_N[u] == 1) { auto ptr = free_N.find(u); if(ptr != free_N.end()) free_N.erase(ptr); } } } } void create_trees(int dir) { used[dir] = true; roots = old_roots; for(auto x: T[dir]) { C[x] --; if(C[x] == 0) roots.push_back(x); } for(int i = 1; i <= 10 * n; i++) trees[i].pid = dir; for(auto x: roots) { if(x == dir) continue; add_to_tree(x, dir); if(back_N[x] == 0) free_N.insert(x); } } vector<int> G[MAX]; bool anc[MAX][MAX]; //anc[i][j] iff i ---- > j in the tree void dfs(int x, int top) { anc[top][x] = true; for(auto v: G[x]) dfs(v, top); } bool check_answer(int dir) { int c = 0; for(int i = 1; i <= n; i++) { //printf("%d\n", A[i]); if(A[i] != 0) G[A[i]].push_back(i); else c++; } if(c != 1) return false; for(int i = 1; i <= n; i++) dfs(i, i); for(int i = 1; i <= n; i++) for(auto x: N[i]) if(anc[x][i]) { //printf("%d %d\n", x, i); return false; } return true; } bool solve_for_dir(int dir) { int top; while(free_N.size()) { top = *free_N.begin(); free_N.erase(free_N.begin()); //printf("TOP %d\n", top); A[top] = trees[LW[top]].pid; used[top] = true; trees[LW[top]].pid = top; for(auto x: T[top]) { C[x] --; if(C[x] == 0) { if(back_N[x] == 0 && !used[x]) free_N.insert(x); add_to_tree(x, top); } } for(auto x: N[top]) { if(LW[top] == LW[x]) { back_N[x] --; if(back_N[x] == 0 && C[x] == 0 && !used[x]) free_N.insert(x); } } //for(int i = 1; i <= n; i++) printf("%d: %d\n", i, back_N[i]); } return check_answer(dir); } bool is_good_root(int dir) { for(int i = 1; i <= n; i++) { for(auto x: N[i]) if(x == dir) return false; } return true; } int main () { int a, b; char c; scanf("%d%d", &n, &m); for(int i = 0; i < m; i++) { scanf("%d%d %c", &a, &b, &c); switch(c) { case 'T': T[b].push_back(a); C[a] ++; break; case 'N': N[a].push_back(b); } } for(int i = 1; i <= n; i++) { if(C[i] == 0) old_roots.push_back(i); } for(int i = 1; i <= n; i++) { if(!dfs2(i, i)) { printf("NIE\n"); return 0; } } for(auto x: old_roots) count_LW(x); //for(int i = 1; i <= n; i++) printf("%d: %d\n", i, LW[i]); int great_root = 0; for(auto x: old_roots) if(is_good_root(x)) great_root = x; if(great_root == 0) { printf("NIE\n"); return 0; } create_trees(great_root); if(solve_for_dir(great_root)) { //printf("TAK\n"); for(int i = 1; i <= n; i++) printf("%d\n", A[i]); return 0; } printf("NIE\n"); } |