#include <bits/stdc++.h> using namespace std; typedef long long int64; #define DEBUG(x) cerr << #x << " = " << x << endl; #define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x) #define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e))) const int INF = 1000000001; const double EPS = 10e-9; struct FindAndUnion { vector<int> p, w; FindAndUnion(int n) : p(n, -1), w(n, -1) { } int find(int x) { return (p[x] < 0) ? x : p[x] = find(p[x]); } void join(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (w[x] > w[y]) { p[y] = x; } else { p[x] = y; } if (w[x] == w[y]) { w[y]++; } } }; struct Solver { vector<int> solution; vector<vector<int>> want; vector<vector<int>> dont; Solver(int n) : solution(n, -2), want(n), dont(n) { } int getBoss(unordered_set<int>& vert) { int boss = -1; for (auto x : vert) { bool ok = true; for (auto it : want[x]) { if (vert.find(it) != vert.end()) { ok = false; break; } } if (!ok) { continue; } for (auto it : dont[x]) { if (vert.find(it) != vert.end()) { ok = false; break; } } if (ok) { boss = x; break; } } return boss; } vector<unordered_set<int>> divide(unordered_set<int> vert) { FindAndUnion fau(vert.size()); unordered_map<int, int> ids, verts; int i = 0; for (auto it : vert) { ids[it] = i; verts[i++] = it; } for (auto x : vert) { auto b = ids.find(x); for (auto it : want[x]) { auto v = ids.find(it); if (v != ids.end()) { fau.join(b->second, v->second); } } } vector<vector<int>> sets(vert.size()); REP(x, vert.size()) { int v = fau.find(x); sets[v].push_back(x); } vector<unordered_set<int>> result; for (auto x : sets) { if (!x.empty()) { unordered_set<int> s; for (auto it : x) { int v = verts.find(it)->second; s.insert(v); } result.push_back(s); } } return result; } bool solve(unordered_set<int> vert, int mainBoss) { int boss = getBoss(vert); if (boss == -1) { return false; } solution[boss] = mainBoss; vert.erase(vert.find(boss)); auto division = divide(vert); for (auto it : division) { if (!solve(it, boss)) { return false; } } return true; } void solve() { unordered_set<int> vert; REP(x, solution.size()) { vert.insert(x); } if (solve(vert, -1)) { for (auto it : solution) { cout << it + 1 << "\n"; } } else { solution.clear(); cout << "NIE\n"; } } }; #ifndef CATCH_TEST int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; Solver solver(n); REP(x, m) { int b, e; char c; cin >> b >> e >> c; if (c == 'T') { solver.want[b - 1].push_back(e - 1); } else { solver.dont[e - 1].push_back(b - 1); } } solver.solve(); return 0; } #endif
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 | #include <bits/stdc++.h> using namespace std; typedef long long int64; #define DEBUG(x) cerr << #x << " = " << x << endl; #define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x) #define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e))) const int INF = 1000000001; const double EPS = 10e-9; struct FindAndUnion { vector<int> p, w; FindAndUnion(int n) : p(n, -1), w(n, -1) { } int find(int x) { return (p[x] < 0) ? x : p[x] = find(p[x]); } void join(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (w[x] > w[y]) { p[y] = x; } else { p[x] = y; } if (w[x] == w[y]) { w[y]++; } } }; struct Solver { vector<int> solution; vector<vector<int>> want; vector<vector<int>> dont; Solver(int n) : solution(n, -2), want(n), dont(n) { } int getBoss(unordered_set<int>& vert) { int boss = -1; for (auto x : vert) { bool ok = true; for (auto it : want[x]) { if (vert.find(it) != vert.end()) { ok = false; break; } } if (!ok) { continue; } for (auto it : dont[x]) { if (vert.find(it) != vert.end()) { ok = false; break; } } if (ok) { boss = x; break; } } return boss; } vector<unordered_set<int>> divide(unordered_set<int> vert) { FindAndUnion fau(vert.size()); unordered_map<int, int> ids, verts; int i = 0; for (auto it : vert) { ids[it] = i; verts[i++] = it; } for (auto x : vert) { auto b = ids.find(x); for (auto it : want[x]) { auto v = ids.find(it); if (v != ids.end()) { fau.join(b->second, v->second); } } } vector<vector<int>> sets(vert.size()); REP(x, vert.size()) { int v = fau.find(x); sets[v].push_back(x); } vector<unordered_set<int>> result; for (auto x : sets) { if (!x.empty()) { unordered_set<int> s; for (auto it : x) { int v = verts.find(it)->second; s.insert(v); } result.push_back(s); } } return result; } bool solve(unordered_set<int> vert, int mainBoss) { int boss = getBoss(vert); if (boss == -1) { return false; } solution[boss] = mainBoss; vert.erase(vert.find(boss)); auto division = divide(vert); for (auto it : division) { if (!solve(it, boss)) { return false; } } return true; } void solve() { unordered_set<int> vert; REP(x, solution.size()) { vert.insert(x); } if (solve(vert, -1)) { for (auto it : solution) { cout << it + 1 << "\n"; } } else { solution.clear(); cout << "NIE\n"; } } }; #ifndef CATCH_TEST int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; Solver solver(n); REP(x, m) { int b, e; char c; cin >> b >> e >> c; if (c == 'T') { solver.want[b - 1].push_back(e - 1); } else { solver.dont[e - 1].push_back(b - 1); } } solver.solve(); return 0; } #endif |