#include <bits/stdc++.h> using namespace std; struct Vertex { unordered_set<int> wants; // unordered_set<int> bans; // unordered_set<int> wantedBy; unordered_set<int> bannedBy; int parent; Vertex(): parent(-1) {} }; void filter(unordered_set<int> &a, unordered_set<int> const &b) { for(unordered_set<int>::iterator it = a.begin(); it != a.end();) { if(b.count(*it) == 1) { ++it; } else { it = a.erase(it); } } } int fff(vector<int> &uf, int i) { int ufi = uf[i]; if(uf[ufi] == ufi) return ufi; return uf[i] = fff(uf, ufi); } void uuu(vector<int> &uf, int i, int j) { uf[fff(uf, i)] = fff(uf, j); } bool solve(vector<Vertex> &data, unordered_set<int> &subset, int parent) { static vector<int> uf(data.size()); if(subset.size() == 1) { data[*subset.begin()].parent = parent; return true; } for(int i: subset) { filter(data[i].wants, subset); // filter(data[i].bans, subset); // filter(data[i].wantedBy, subset); filter(data[i].bannedBy, subset); } int sroot = -1; for(int i: subset) { if(data[i].wants.empty() and data[i].bannedBy.empty()) { sroot = i; } } if(sroot == -1) return false; subset.erase(sroot); data[sroot].parent = parent; for(int i: subset) { uf[i] = i; } for(int i: subset) { for(int j: data[i].wants) { if(j == sroot) continue; uuu(uf, i, j); } } for(int i: subset) { if(i != fff(uf, i)) continue; unordered_set<int> ssubset; for(int j: subset) { if(i != fff(uf, j)) continue; ssubset.insert(j); } if(not solve(data, ssubset, sroot)) return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<Vertex> data(n); unordered_set<int> all; for(int i = 0; i < n; ++i) all.insert(i); for(int i = 0; i < m; ++i) { int a, b; string w; cin >> a >> b >> w; --a; --b; if(w[0] == 'T') { if(data[b].wants.count(a) == 1) goto NO; data[a].wants.insert(b); // data[b].wantedBy.insert(a); } else { // data[a].bans.insert(b); data[b].bannedBy.insert(a); } } if(not solve(data, all, -1)) goto NO; for(int i = 0; i < n; ++i) cout << 1 + data[i].parent << '\n'; goto END; NO: cout << "NIE\n"; goto END; END: 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 | #include <bits/stdc++.h> using namespace std; struct Vertex { unordered_set<int> wants; // unordered_set<int> bans; // unordered_set<int> wantedBy; unordered_set<int> bannedBy; int parent; Vertex(): parent(-1) {} }; void filter(unordered_set<int> &a, unordered_set<int> const &b) { for(unordered_set<int>::iterator it = a.begin(); it != a.end();) { if(b.count(*it) == 1) { ++it; } else { it = a.erase(it); } } } int fff(vector<int> &uf, int i) { int ufi = uf[i]; if(uf[ufi] == ufi) return ufi; return uf[i] = fff(uf, ufi); } void uuu(vector<int> &uf, int i, int j) { uf[fff(uf, i)] = fff(uf, j); } bool solve(vector<Vertex> &data, unordered_set<int> &subset, int parent) { static vector<int> uf(data.size()); if(subset.size() == 1) { data[*subset.begin()].parent = parent; return true; } for(int i: subset) { filter(data[i].wants, subset); // filter(data[i].bans, subset); // filter(data[i].wantedBy, subset); filter(data[i].bannedBy, subset); } int sroot = -1; for(int i: subset) { if(data[i].wants.empty() and data[i].bannedBy.empty()) { sroot = i; } } if(sroot == -1) return false; subset.erase(sroot); data[sroot].parent = parent; for(int i: subset) { uf[i] = i; } for(int i: subset) { for(int j: data[i].wants) { if(j == sroot) continue; uuu(uf, i, j); } } for(int i: subset) { if(i != fff(uf, i)) continue; unordered_set<int> ssubset; for(int j: subset) { if(i != fff(uf, j)) continue; ssubset.insert(j); } if(not solve(data, ssubset, sroot)) return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<Vertex> data(n); unordered_set<int> all; for(int i = 0; i < n; ++i) all.insert(i); for(int i = 0; i < m; ++i) { int a, b; string w; cin >> a >> b >> w; --a; --b; if(w[0] == 'T') { if(data[b].wants.count(a) == 1) goto NO; data[a].wants.insert(b); // data[b].wantedBy.insert(a); } else { // data[a].bans.insert(b); data[b].bannedBy.insert(a); } } if(not solve(data, all, -1)) goto NO; for(int i = 0; i < n; ++i) cout << 1 + data[i].parent << '\n'; goto END; NO: cout << "NIE\n"; goto END; END: return 0; } |