#include<cstdio> #include<vector> #define PB push_back using namespace std; int n, m, a, b; char c; vector<int> v[1010]; vector<int> v1[1010]; vector<int> v2[1010]; int ancestors[1010]; vector<int> parents[1010]; vector<int> children[1010]; bool is_child[1010][1010]; vector<int> not_children[1010]; bool is_not_child[1010][1010]; vector<int> to_add[1010]; bool is_in_actual[1010][1010]; vector<int> actual[1010]; int is_used[1010][1010]; bool added[1010]; int parent[1010]; void find_children(int x, int y){ for(int i = 0; i < v1[y].size(); ++i){ int act = v1[y][i]; if(!is_child[x][act]){ is_child[x][act] = true; children[x].PB(act); ancestors[act]++; parents[act].PB(x); //printf("%d is child of %d\n", act, x); find_children(x, act); } } } int find_root(){ for(int i = 1; i <= n; ++i) if(!ancestors[i] && !not_children[i].size()) return i; return -1; } void nie(){ printf("NIE\n"); } void find_subtree(int root, int x, int sub){ is_used[root][x] = sub; actual[root].PB(x); is_in_actual[root][x] = true; //printf("%d ",x); for(int i = 0; i < v[x].size(); ++i){ int act = v[x][i]; if(!added[act] && !is_used[root][act]){ find_subtree(root, act, sub); } } } int find_next_root(int root){ bool can_be; for(int i = 0; i < actual[root].size(); ++i){ int now = actual[root][i], siz; can_be = true; for(int j = 0; j < parents[now].size();){ siz = parents[now].size(); //printf("checking parentage: %d -> %d\n", parents[now][j], now); if(!is_in_actual[root][parents[now][j]]){ if(siz==1){ parents[now].clear(); break; } else{ parents[now][0] = parents[now][siz-1]; parents[now].pop_back(); } } else{ can_be = false; //printf("err\n"); break; } } for(int j = 0; j < not_children[now].size();){ siz = not_children[now].size(); //printf("checking notchildrenage: %d -> %d\n", not_children[now][j], now); if(!is_in_actual[root][not_children[now][j]]){ if(siz==1){ not_children[now].clear(); break; } else{ not_children[now][0] = not_children[now][siz-1]; not_children[now].pop_back(); } } else{ can_be = false; //printf("err\n"); break; } } if(can_be) return now; } return -1; } bool make_tree(int root){ //printf("making tree of %d:\n",root); //printf("to add: ");for(int i=0;i<to_add[root].size();++i)printf(" %d",to_add[root][i]);printf("\n"); if(to_add[root].size() == 0){ return true; } if(to_add[root].size() == 1){ parent[to_add[root][0]] = root; return true; } int subtree = 1; for(int i = 0; i < to_add[root].size(); ++i){ int act = to_add[root][i]; if(!is_used[root][act]){ for(int j = 0; j < actual[root].size(); ++j) is_in_actual[root][actual[root][j]] = false; actual[root].clear(); //printf("subtree of %d #%d: \n",root, subtree); find_subtree(root, act, subtree);//printf("\n"); int next_root = find_next_root(root); if(next_root == -1) return false; //printf("next root: %d\n", next_root); parent[next_root] = root; added[next_root] = true; for(int j = 0; j < actual[root].size(); ++j) if(actual[root][j] != next_root) to_add[next_root].PB(actual[root][j]); if(!make_tree(next_root)) return false; } } return true; } void solve(){ for(int i = 1; i <= n; ++i) find_children(i, i); for(int i = 1; i <= n; ++i){ for(int j = 1; j <= n; ++j){ if(is_child[i][j] && is_not_child[i][j]){ nie(); return; } } } int root = find_root(); if(root == -1){ nie(); return; } for(int i = 1; i <= n; ++i){ if(i != root) to_add[root].PB(i); } added[root] = true; //printf("first root: %d\n",root); if(make_tree(root)){ for(int i = 1; i <= n; ++i) printf("%d\n", parent[i]); } else nie(); } int main(){ scanf("%d %d",&n, &m); for(int i = 0; i < m; ++i){ scanf("%d %d %c", &a, &b, &c); if(c == 'T'){ v1[b].PB(a); v[a].PB(b); v[b].PB(a); } else{ is_not_child[b][a] = true; not_children[b].PB(a); } } for(int i = 1; i <= n; ++i) is_not_child[i][i] = true; solve(); } /* 4 5 2 1 T 3 1 T 4 2 T 4 3 T 2 3 N 4 5 2 1 T 3 1 T 4 2 T 4 3 T 3 2 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 | #include<cstdio> #include<vector> #define PB push_back using namespace std; int n, m, a, b; char c; vector<int> v[1010]; vector<int> v1[1010]; vector<int> v2[1010]; int ancestors[1010]; vector<int> parents[1010]; vector<int> children[1010]; bool is_child[1010][1010]; vector<int> not_children[1010]; bool is_not_child[1010][1010]; vector<int> to_add[1010]; bool is_in_actual[1010][1010]; vector<int> actual[1010]; int is_used[1010][1010]; bool added[1010]; int parent[1010]; void find_children(int x, int y){ for(int i = 0; i < v1[y].size(); ++i){ int act = v1[y][i]; if(!is_child[x][act]){ is_child[x][act] = true; children[x].PB(act); ancestors[act]++; parents[act].PB(x); //printf("%d is child of %d\n", act, x); find_children(x, act); } } } int find_root(){ for(int i = 1; i <= n; ++i) if(!ancestors[i] && !not_children[i].size()) return i; return -1; } void nie(){ printf("NIE\n"); } void find_subtree(int root, int x, int sub){ is_used[root][x] = sub; actual[root].PB(x); is_in_actual[root][x] = true; //printf("%d ",x); for(int i = 0; i < v[x].size(); ++i){ int act = v[x][i]; if(!added[act] && !is_used[root][act]){ find_subtree(root, act, sub); } } } int find_next_root(int root){ bool can_be; for(int i = 0; i < actual[root].size(); ++i){ int now = actual[root][i], siz; can_be = true; for(int j = 0; j < parents[now].size();){ siz = parents[now].size(); //printf("checking parentage: %d -> %d\n", parents[now][j], now); if(!is_in_actual[root][parents[now][j]]){ if(siz==1){ parents[now].clear(); break; } else{ parents[now][0] = parents[now][siz-1]; parents[now].pop_back(); } } else{ can_be = false; //printf("err\n"); break; } } for(int j = 0; j < not_children[now].size();){ siz = not_children[now].size(); //printf("checking notchildrenage: %d -> %d\n", not_children[now][j], now); if(!is_in_actual[root][not_children[now][j]]){ if(siz==1){ not_children[now].clear(); break; } else{ not_children[now][0] = not_children[now][siz-1]; not_children[now].pop_back(); } } else{ can_be = false; //printf("err\n"); break; } } if(can_be) return now; } return -1; } bool make_tree(int root){ //printf("making tree of %d:\n",root); //printf("to add: ");for(int i=0;i<to_add[root].size();++i)printf(" %d",to_add[root][i]);printf("\n"); if(to_add[root].size() == 0){ return true; } if(to_add[root].size() == 1){ parent[to_add[root][0]] = root; return true; } int subtree = 1; for(int i = 0; i < to_add[root].size(); ++i){ int act = to_add[root][i]; if(!is_used[root][act]){ for(int j = 0; j < actual[root].size(); ++j) is_in_actual[root][actual[root][j]] = false; actual[root].clear(); //printf("subtree of %d #%d: \n",root, subtree); find_subtree(root, act, subtree);//printf("\n"); int next_root = find_next_root(root); if(next_root == -1) return false; //printf("next root: %d\n", next_root); parent[next_root] = root; added[next_root] = true; for(int j = 0; j < actual[root].size(); ++j) if(actual[root][j] != next_root) to_add[next_root].PB(actual[root][j]); if(!make_tree(next_root)) return false; } } return true; } void solve(){ for(int i = 1; i <= n; ++i) find_children(i, i); for(int i = 1; i <= n; ++i){ for(int j = 1; j <= n; ++j){ if(is_child[i][j] && is_not_child[i][j]){ nie(); return; } } } int root = find_root(); if(root == -1){ nie(); return; } for(int i = 1; i <= n; ++i){ if(i != root) to_add[root].PB(i); } added[root] = true; //printf("first root: %d\n",root); if(make_tree(root)){ for(int i = 1; i <= n; ++i) printf("%d\n", parent[i]); } else nie(); } int main(){ scanf("%d %d",&n, &m); for(int i = 0; i < m; ++i){ scanf("%d %d %c", &a, &b, &c); if(c == 'T'){ v1[b].PB(a); v[a].PB(b); v[b].PB(a); } else{ is_not_child[b][a] = true; not_children[b].PB(a); } } for(int i = 1; i <= n; ++i) is_not_child[i][i] = true; solve(); } /* 4 5 2 1 T 3 1 T 4 2 T 4 3 T 2 3 N 4 5 2 1 T 3 1 T 4 2 T 4 3 T 3 2 N */ |