#include <cstdio> #include <cstring> #include <list> using namespace std; int main() { int n, m; scanf(" %d %d", &n, &m); int yeys[n]; // outgoing std::memset(yeys, 0, sizeof yeys); int neighs[n]; // incoming std::memset(neighs, 0, sizeof neighs); list<int> * inyeys = new list<int>[n]; // incoming list<int> * outneighs = new list<int>[n]; // outgoing list<int> * all_yeys = new list<int>[n]; // incoming // read requirements, fill out basic structures for (int i = 0; i<m; i++){ int a, b; char c; scanf(" %d %d %c", &a, &b, &c); a-=1; b-=1; if (c == 'T') { inyeys[b].push_back(a); all_yeys[b].push_back(a); all_yeys[a].push_back(b); yeys[a] += 1; } else { outneighs[a].push_back(b); neighs[b] += 1; } } // choose the root - no outgoing yeys, no incoming neighs, otherwise not possible: NIE int root = -1; for(int i = 0; i<n; i++){ if (yeys[i] == 0 && neighs[i] == 0) { root = i; break; } } if (root == -1){ printf("NIE\n"); return 0; } // update yeys and neighs in the remaining items for(list<int>::iterator it = outneighs[root].begin(); it != outneighs[root].end(); it++){ neighs[*it] -= 1; } for(list<int>::iterator it = inyeys[root].begin(); it != inyeys[root].end(); it++){ yeys[*it] -= 1; } int parents[n]; std::memset(parents, -1, sizeof parents); parents[root] = 0; // mark lineages (components connected by yeys) int lineage_count = 0; int lineage_ix[n]; std::memset(lineage_ix, -1, sizeof lineage_ix); for(int i = 0; i<n; i++){ if (lineage_ix[i] == -1){ list<int> queue; queue.push_back(i); int debug = 0; while(!queue.empty()){ int current = queue.front(); queue.pop_front(); if (lineage_ix[current] == -1){ lineage_ix[current] = lineage_count; queue.insert(queue.begin(), all_yeys[current].begin(), all_yeys[current].end()); } } lineage_count++; } } // remove neighs across lineages for (int i = 0; i < n; i++){ list<int> to_remove; for(list<int>::iterator it = outneighs[i].begin(); it != outneighs[i].end(); it++){ if (lineage_ix[i] != lineage_ix[*it]) { to_remove.push_back(*it); } } for(list<int>::iterator it = to_remove.begin(); it != to_remove.end(); it++){ outneighs[i].remove(*it); neighs[*it] -= 1; } } int lineages_parents[lineage_count]; for (int i = 0; i < lineage_count; i++){ lineages_parents[i] = root; } int lineages_empty = 0; while(lineages_empty < lineage_count){ // one per lineage: int chosen[lineage_count]; std::memset(chosen, -1, sizeof chosen); bool empty[lineage_count]; std::memset(empty, true, sizeof empty); // choose and remove current children with no outgoing yeys, no incoming neighs otherwise not possible: NIE for(int i = 0; i < n; i++){ if (parents[i] == -1 && chosen[lineage_ix[i]] == -1) { empty[lineage_ix[i]] = false; if (yeys[i] == 0 && neighs[i] == 0){ chosen[lineage_ix[i]] = i; } } } // update it's parent and lineages_current lineages_empty = 0; for(int i = 0; i < lineage_count; i++){ if (chosen[i] == -1){ if (empty[i]) { lineages_empty += 1; } else { printf("NIE\n"); return 0; } } else { parents[chosen[i]] = lineages_parents[i]; lineages_parents[i] = chosen[i]; // update yeys and neighs in the remaining items for(list<int>::iterator it = outneighs[chosen[i]].begin(); it != outneighs[chosen[i]].end(); it++){ if (*it != -1) neighs[*it] -= 1; } for(list<int>::iterator it = inyeys[chosen[i]].begin(); it != inyeys[chosen[i]].end(); it++){ if (*it != -1) yeys[*it] -= 1; } } } } for (int i = 0; i < n; i++){ if (i == root) printf("0\n"); else printf("%d\n", parents[i]+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 | #include <cstdio> #include <cstring> #include <list> using namespace std; int main() { int n, m; scanf(" %d %d", &n, &m); int yeys[n]; // outgoing std::memset(yeys, 0, sizeof yeys); int neighs[n]; // incoming std::memset(neighs, 0, sizeof neighs); list<int> * inyeys = new list<int>[n]; // incoming list<int> * outneighs = new list<int>[n]; // outgoing list<int> * all_yeys = new list<int>[n]; // incoming // read requirements, fill out basic structures for (int i = 0; i<m; i++){ int a, b; char c; scanf(" %d %d %c", &a, &b, &c); a-=1; b-=1; if (c == 'T') { inyeys[b].push_back(a); all_yeys[b].push_back(a); all_yeys[a].push_back(b); yeys[a] += 1; } else { outneighs[a].push_back(b); neighs[b] += 1; } } // choose the root - no outgoing yeys, no incoming neighs, otherwise not possible: NIE int root = -1; for(int i = 0; i<n; i++){ if (yeys[i] == 0 && neighs[i] == 0) { root = i; break; } } if (root == -1){ printf("NIE\n"); return 0; } // update yeys and neighs in the remaining items for(list<int>::iterator it = outneighs[root].begin(); it != outneighs[root].end(); it++){ neighs[*it] -= 1; } for(list<int>::iterator it = inyeys[root].begin(); it != inyeys[root].end(); it++){ yeys[*it] -= 1; } int parents[n]; std::memset(parents, -1, sizeof parents); parents[root] = 0; // mark lineages (components connected by yeys) int lineage_count = 0; int lineage_ix[n]; std::memset(lineage_ix, -1, sizeof lineage_ix); for(int i = 0; i<n; i++){ if (lineage_ix[i] == -1){ list<int> queue; queue.push_back(i); int debug = 0; while(!queue.empty()){ int current = queue.front(); queue.pop_front(); if (lineage_ix[current] == -1){ lineage_ix[current] = lineage_count; queue.insert(queue.begin(), all_yeys[current].begin(), all_yeys[current].end()); } } lineage_count++; } } // remove neighs across lineages for (int i = 0; i < n; i++){ list<int> to_remove; for(list<int>::iterator it = outneighs[i].begin(); it != outneighs[i].end(); it++){ if (lineage_ix[i] != lineage_ix[*it]) { to_remove.push_back(*it); } } for(list<int>::iterator it = to_remove.begin(); it != to_remove.end(); it++){ outneighs[i].remove(*it); neighs[*it] -= 1; } } int lineages_parents[lineage_count]; for (int i = 0; i < lineage_count; i++){ lineages_parents[i] = root; } int lineages_empty = 0; while(lineages_empty < lineage_count){ // one per lineage: int chosen[lineage_count]; std::memset(chosen, -1, sizeof chosen); bool empty[lineage_count]; std::memset(empty, true, sizeof empty); // choose and remove current children with no outgoing yeys, no incoming neighs otherwise not possible: NIE for(int i = 0; i < n; i++){ if (parents[i] == -1 && chosen[lineage_ix[i]] == -1) { empty[lineage_ix[i]] = false; if (yeys[i] == 0 && neighs[i] == 0){ chosen[lineage_ix[i]] = i; } } } // update it's parent and lineages_current lineages_empty = 0; for(int i = 0; i < lineage_count; i++){ if (chosen[i] == -1){ if (empty[i]) { lineages_empty += 1; } else { printf("NIE\n"); return 0; } } else { parents[chosen[i]] = lineages_parents[i]; lineages_parents[i] = chosen[i]; // update yeys and neighs in the remaining items for(list<int>::iterator it = outneighs[chosen[i]].begin(); it != outneighs[chosen[i]].end(); it++){ if (*it != -1) neighs[*it] -= 1; } for(list<int>::iterator it = inyeys[chosen[i]].begin(); it != inyeys[chosen[i]].end(); it++){ if (*it != -1) yeys[*it] -= 1; } } } } for (int i = 0; i < n; i++){ if (i == root) printf("0\n"); else printf("%d\n", parents[i]+1); } return 0; } |