#include <cstdio> #include <set> #include <unordered_set> #include <unordered_map> using namespace std; struct elem { int parent; int rank; }; elem node[1001]; unordered_set <int> uv[1001], dv[1001], uiv[1001], dinv[1001]; int res[1001], n, m, a, b; char c; void init(const unordered_set<int>& v) { for (int a : v) { node[a].parent = a; node[a].rank = 0; } } int find(int a) { if (node[a].parent != a) node[a].parent = find(node[a].parent); return node[a].parent; } void uni(int a, int b) { int setA = find(a); int setB = find(b); if (setA == setB) return; if (node[setA].rank < node[setB].rank) node[setA].parent = setB; if (node[setA].rank >= node[setB].rank) node[setB].parent = setA; if (node[setA].rank == node[setB].rank) node[setA].rank++; return; } bool checkIfRoot(int a, const unordered_set<int>& us) { if (us.size() > uv[a].size()) { for (int b : uv[a]) { if (us.find(b) != us.end()) return false; } } else { for (int b : us) { if (uv[a].find(b) != uv[a].end()) return false; } } if (us.size() > dinv[a].size()) { for (int b : dinv[a]) { if (us.find(b) != us.end()) return false; } } else { for (int b : us) { if (dinv[a].find(b) != dinv[a].end()) return false; } } return true; } int build(unordered_set<int>& us) { bool gut; if (us.size() == 1) return *us.begin(); for (int a : us) { if(checkIfRoot(a, us)) { us.erase(a); gut = 1; init(us); for (int b : us) { if (dv[b].size() < us.size()) { for (int i : dv[b]) { if (us.find(i) != us.end()) { uni(i, b); } } } else { for (int i : us) { if (dv[b].find(i) != dv[b].end()) { uni(i, b); } } } } unordered_map<int, unordered_set<int>> mapa; for (int b : us) { if (b != a) mapa[find(b)].insert(b); } for (auto st : mapa) { int ret = build(st.second); if (ret == -1) { gut = 0; break; } else { res[ret] = a; } } us.insert(a); if (!gut) continue; else return a; } } return -1; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d %c", &a, &b, &c); if (c == 'T') { uv[a].insert(b); dv[b].insert(a); } else { uiv[a].insert(b); dinv[b].insert(a); } } unordered_set<int> beg; for (int i = 1; i <= n; i++) { beg.insert(i); } int ret = build(beg); if (ret == -1) printf("NIE"); else { for (int i = 1; i <= n; i++) printf("%d\n", res[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 | #include <cstdio> #include <set> #include <unordered_set> #include <unordered_map> using namespace std; struct elem { int parent; int rank; }; elem node[1001]; unordered_set <int> uv[1001], dv[1001], uiv[1001], dinv[1001]; int res[1001], n, m, a, b; char c; void init(const unordered_set<int>& v) { for (int a : v) { node[a].parent = a; node[a].rank = 0; } } int find(int a) { if (node[a].parent != a) node[a].parent = find(node[a].parent); return node[a].parent; } void uni(int a, int b) { int setA = find(a); int setB = find(b); if (setA == setB) return; if (node[setA].rank < node[setB].rank) node[setA].parent = setB; if (node[setA].rank >= node[setB].rank) node[setB].parent = setA; if (node[setA].rank == node[setB].rank) node[setA].rank++; return; } bool checkIfRoot(int a, const unordered_set<int>& us) { if (us.size() > uv[a].size()) { for (int b : uv[a]) { if (us.find(b) != us.end()) return false; } } else { for (int b : us) { if (uv[a].find(b) != uv[a].end()) return false; } } if (us.size() > dinv[a].size()) { for (int b : dinv[a]) { if (us.find(b) != us.end()) return false; } } else { for (int b : us) { if (dinv[a].find(b) != dinv[a].end()) return false; } } return true; } int build(unordered_set<int>& us) { bool gut; if (us.size() == 1) return *us.begin(); for (int a : us) { if(checkIfRoot(a, us)) { us.erase(a); gut = 1; init(us); for (int b : us) { if (dv[b].size() < us.size()) { for (int i : dv[b]) { if (us.find(i) != us.end()) { uni(i, b); } } } else { for (int i : us) { if (dv[b].find(i) != dv[b].end()) { uni(i, b); } } } } unordered_map<int, unordered_set<int>> mapa; for (int b : us) { if (b != a) mapa[find(b)].insert(b); } for (auto st : mapa) { int ret = build(st.second); if (ret == -1) { gut = 0; break; } else { res[ret] = a; } } us.insert(a); if (!gut) continue; else return a; } } return -1; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d %c", &a, &b, &c); if (c == 'T') { uv[a].insert(b); dv[b].insert(a); } else { uiv[a].insert(b); dinv[b].insert(a); } } unordered_set<int> beg; for (int i = 1; i <= n; i++) { beg.insert(i); } int ret = build(beg); if (ret == -1) printf("NIE"); else { for (int i = 1; i <= n; i++) printf("%d\n", res[i]); } return 0; } |