#include <cstdio> #include <vector> #include <set> #include <queue> using namespace std; struct Preferences { vector<set<int> > hasObjection; vector<set<int> > hasPreference; vector<set<int> > isObjection; vector<set<int> > isPreference; Preferences(int n) { hasObjection.assign(n+1, set<int>()); hasPreference.assign(n + 1, set<int>()); isObjection.assign(n + 1, set<int>()); isPreference.assign(n + 1, set<int>()); } }; struct Department { int supervisor; set<int> employees; }; inline void readPreferences(Preferences& preferences, int m) { int a, b; char c; for (int i = 0; i < m; ++i) { scanf("%d %d %c", &a, &b, &c); if (c == 'T') { preferences.isPreference[b].insert(a); preferences.hasPreference[a].insert(b); } else { preferences.isObjection[b].insert(a); preferences.hasObjection[a].insert(b); } } } inline void prepareWholeCompany(queue<Department>& toProcess, int n) { Department company; company.supervisor = 0; for (int i = 1; i <= n; ++i) { company.employees.insert(i); } toProcess.push(company); } inline int chooseSupervisor(const set<int>& staff, const Preferences& preferences) { int supervisor = -1; int maxPref = -1; for (set<int>::iterator it = staff.begin(); it != staff.end(); ++it) { if (preferences.hasPreference[*it].empty() && preferences.isObjection[*it].empty() && (maxPref < (int)preferences.isPreference[*it].size())) { maxPref = preferences.isPreference[*it].size(); supervisor = *it; } } return supervisor; } inline void removeWorkerFromPreferences(int worker, Preferences& preferences) { for (set<int>::iterator it = preferences.hasObjection[worker].begin(); it != preferences.hasObjection[worker].end(); ++it) { preferences.isObjection[*it].erase(worker); } for (set<int>::iterator it = preferences.isObjection[worker].begin(); it != preferences.isObjection[worker].end(); ++it) { preferences.hasObjection[*it].erase(worker); } for (set<int>::iterator it = preferences.hasPreference[worker].begin(); it != preferences.hasPreference[worker].end(); ++it) { preferences.isPreference[*it].erase(worker); } for (set<int>::iterator it = preferences.isPreference[worker].begin(); it != preferences.isPreference[worker].end(); ++it) { preferences.hasPreference[*it].erase(worker); } } inline void extractGroupByWorker(int worker, set<int>& staff, set<int>& group, const Preferences& preferences) { queue<int> toAdd; toAdd.push(worker); group.insert(worker); staff.erase(worker); int current; while (!toAdd.empty()) { current = toAdd.front(); toAdd.pop(); for (set<int>::iterator it = preferences.isPreference[current].begin(); it != preferences.isPreference[current].end(); ++it) { if (group.find(*it) == group.end()) { toAdd.push(*it); group.insert(*it); staff.erase(*it); } } for (set<int>::iterator it = preferences.hasPreference[current].begin(); it != preferences.hasPreference[current].end(); ++it) { if (group.find(*it) == group.end()) { toAdd.push(*it); group.insert(*it); staff.erase(*it); } } } } inline void splitPreferences(const set<int>& staff, const set<int>& group, Preferences& preferences) { queue<int> toErase; for (set<int>::iterator it = group.begin(); it != group.end(); ++it) { for (set<int>::iterator it2 = preferences.isObjection[*it].begin(); it2 != preferences.isObjection[*it].end(); ++it2) { if (group.find(*it2) == group.end()) { toErase.push(*it2); preferences.hasObjection[*it2].erase(*it); } } while (!toErase.empty()) { preferences.isObjection[*it].erase(toErase.front()); toErase.pop(); } for (set<int>::iterator it2 = preferences.hasObjection[*it].begin(); it2 != preferences.hasObjection[*it].end(); ++it2) { if (group.find(*it2) == group.end()) { toErase.push(*it2); preferences.isObjection[*it2].erase(*it); } } while (!toErase.empty()) { preferences.hasObjection[*it].erase(toErase.front()); toErase.pop(); } } } inline void splitDepartment(int supervisor, set<int>& staff, queue<Department>& toProcess, Preferences& preferences) { while (!staff.empty()) { Department subDep; subDep.supervisor = supervisor; extractGroupByWorker(*staff.begin(), staff, subDep.employees, preferences); splitPreferences(staff, subDep.employees, preferences); toProcess.push(subDep); } } inline int makeDepartmentHierarchy(Department& dep, queue<Department>& toProcess, Preferences& preferences) { int supervisor = chooseSupervisor(dep.employees, preferences); if (supervisor > 0) { removeWorkerFromPreferences(supervisor, preferences); dep.employees.erase(supervisor); splitDepartment(supervisor, dep.employees, toProcess, preferences); } return supervisor; } int main() { int n, m; scanf("%d %d", &n, &m); Preferences preferences(n); readPreferences(preferences, m); queue<Department> toProcess; prepareWholeCompany(toProcess, n); vector<int> result(n + 1); int supervisor = 0; while (supervisor >= 0 && !toProcess.empty()) { supervisor = makeDepartmentHierarchy(toProcess.front(), toProcess, preferences); if (supervisor >= 0) { result[supervisor] = toProcess.front().supervisor; } toProcess.pop(); } if (supervisor > 0) { for (int i = 1; i <= n; ++i) { printf("%d\n", result[i]); } } else { printf("NIE\n"); } 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #include <cstdio> #include <vector> #include <set> #include <queue> using namespace std; struct Preferences { vector<set<int> > hasObjection; vector<set<int> > hasPreference; vector<set<int> > isObjection; vector<set<int> > isPreference; Preferences(int n) { hasObjection.assign(n+1, set<int>()); hasPreference.assign(n + 1, set<int>()); isObjection.assign(n + 1, set<int>()); isPreference.assign(n + 1, set<int>()); } }; struct Department { int supervisor; set<int> employees; }; inline void readPreferences(Preferences& preferences, int m) { int a, b; char c; for (int i = 0; i < m; ++i) { scanf("%d %d %c", &a, &b, &c); if (c == 'T') { preferences.isPreference[b].insert(a); preferences.hasPreference[a].insert(b); } else { preferences.isObjection[b].insert(a); preferences.hasObjection[a].insert(b); } } } inline void prepareWholeCompany(queue<Department>& toProcess, int n) { Department company; company.supervisor = 0; for (int i = 1; i <= n; ++i) { company.employees.insert(i); } toProcess.push(company); } inline int chooseSupervisor(const set<int>& staff, const Preferences& preferences) { int supervisor = -1; int maxPref = -1; for (set<int>::iterator it = staff.begin(); it != staff.end(); ++it) { if (preferences.hasPreference[*it].empty() && preferences.isObjection[*it].empty() && (maxPref < (int)preferences.isPreference[*it].size())) { maxPref = preferences.isPreference[*it].size(); supervisor = *it; } } return supervisor; } inline void removeWorkerFromPreferences(int worker, Preferences& preferences) { for (set<int>::iterator it = preferences.hasObjection[worker].begin(); it != preferences.hasObjection[worker].end(); ++it) { preferences.isObjection[*it].erase(worker); } for (set<int>::iterator it = preferences.isObjection[worker].begin(); it != preferences.isObjection[worker].end(); ++it) { preferences.hasObjection[*it].erase(worker); } for (set<int>::iterator it = preferences.hasPreference[worker].begin(); it != preferences.hasPreference[worker].end(); ++it) { preferences.isPreference[*it].erase(worker); } for (set<int>::iterator it = preferences.isPreference[worker].begin(); it != preferences.isPreference[worker].end(); ++it) { preferences.hasPreference[*it].erase(worker); } } inline void extractGroupByWorker(int worker, set<int>& staff, set<int>& group, const Preferences& preferences) { queue<int> toAdd; toAdd.push(worker); group.insert(worker); staff.erase(worker); int current; while (!toAdd.empty()) { current = toAdd.front(); toAdd.pop(); for (set<int>::iterator it = preferences.isPreference[current].begin(); it != preferences.isPreference[current].end(); ++it) { if (group.find(*it) == group.end()) { toAdd.push(*it); group.insert(*it); staff.erase(*it); } } for (set<int>::iterator it = preferences.hasPreference[current].begin(); it != preferences.hasPreference[current].end(); ++it) { if (group.find(*it) == group.end()) { toAdd.push(*it); group.insert(*it); staff.erase(*it); } } } } inline void splitPreferences(const set<int>& staff, const set<int>& group, Preferences& preferences) { queue<int> toErase; for (set<int>::iterator it = group.begin(); it != group.end(); ++it) { for (set<int>::iterator it2 = preferences.isObjection[*it].begin(); it2 != preferences.isObjection[*it].end(); ++it2) { if (group.find(*it2) == group.end()) { toErase.push(*it2); preferences.hasObjection[*it2].erase(*it); } } while (!toErase.empty()) { preferences.isObjection[*it].erase(toErase.front()); toErase.pop(); } for (set<int>::iterator it2 = preferences.hasObjection[*it].begin(); it2 != preferences.hasObjection[*it].end(); ++it2) { if (group.find(*it2) == group.end()) { toErase.push(*it2); preferences.isObjection[*it2].erase(*it); } } while (!toErase.empty()) { preferences.hasObjection[*it].erase(toErase.front()); toErase.pop(); } } } inline void splitDepartment(int supervisor, set<int>& staff, queue<Department>& toProcess, Preferences& preferences) { while (!staff.empty()) { Department subDep; subDep.supervisor = supervisor; extractGroupByWorker(*staff.begin(), staff, subDep.employees, preferences); splitPreferences(staff, subDep.employees, preferences); toProcess.push(subDep); } } inline int makeDepartmentHierarchy(Department& dep, queue<Department>& toProcess, Preferences& preferences) { int supervisor = chooseSupervisor(dep.employees, preferences); if (supervisor > 0) { removeWorkerFromPreferences(supervisor, preferences); dep.employees.erase(supervisor); splitDepartment(supervisor, dep.employees, toProcess, preferences); } return supervisor; } int main() { int n, m; scanf("%d %d", &n, &m); Preferences preferences(n); readPreferences(preferences, m); queue<Department> toProcess; prepareWholeCompany(toProcess, n); vector<int> result(n + 1); int supervisor = 0; while (supervisor >= 0 && !toProcess.empty()) { supervisor = makeDepartmentHierarchy(toProcess.front(), toProcess, preferences); if (supervisor >= 0) { result[supervisor] = toProcess.front().supervisor; } toProcess.pop(); } if (supervisor > 0) { for (int i = 1; i <= n; ++i) { printf("%d\n", result[i]); } } else { printf("NIE\n"); } return 0; } |