#include "bits/stdc++.h" using namespace std; struct relation { vector<vector<int>> related; vector<vector<int>> inv_related; relation(int n): related(n), inv_related(n){} void add_pair(int x, int y){ related[x].push_back(y); inv_related[y].push_back(x); } }; int pick_director( const unordered_set<int>& subset, const relation& t_relation, const relation& n_relation) { unordered_set<int> candidates = subset; for (int u : subset) { for (int v : t_relation.inv_related[u]) { candidates.erase(v); } } for (int u : subset) { for(int v : n_relation.related[u]) { candidates.erase(v); } } if(candidates.empty()) return -1; return *candidates.begin(); } bool solve_for_subset( const unordered_set<int>& subset, const relation& t_relation, const relation& n_relation, vector<int>& bosses, const int subset_director_boss) { int subset_director = pick_director(subset, t_relation, n_relation); if (subset_director == -1) return false; bosses[subset_director] = subset_director_boss; unordered_set<int> visited({subset_director}); const function<void(int, unordered_set<int>&)> dfs = [&](int u, unordered_set<int>& subsubset) { if ((!subset.count(u)) || subsubset.count(u) || u == subset_director){ return; } subsubset.insert(u); for (int v : t_relation.related[u]) { dfs(v, subsubset); } for (int v : t_relation.inv_related[u]) { dfs(v, subsubset); } }; for(int u : subset) { if (visited.count(u)) { continue; } unordered_set<int> subsubset; dfs(u, subsubset); if (!solve_for_subset( subsubset, t_relation, n_relation, bosses, subset_director)){ return false; }; visited.insert(subsubset.begin(), subsubset.end()); } return true; } void verify_solution( const relation& t_relation, const relation& n_relation, const vector<int>& bosses) { int n = bosses.size(); vector<unordered_set<int>> superiors(n); int directors = 0; for(int u = 0; u < n; ++u){ assert(bosses[u] != u); assert(bosses[u] >= -1); assert(bosses[u] < n); directors += (bosses[u] == -1); } assert(directors == 1); for(int u = 0; u < n; ++u){ for(int v = bosses[u]; v != -1; v = bosses[v]) { assert(!superiors[u].count(v)); superiors[u].insert(v); } } for(int u = 0; u < n; ++u){ for(int v : t_relation.related[u]){ assert(superiors[u].count(v) == 1); } for(int v : n_relation.related[u]){ assert(superiors[u].count(v) == 0); } } } int main(){ int n, m; ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; relation t_relation(n), n_relation(n); for(int i = 0; i < m; ++i){ int a, b; string c; cin >> a >> b >> c; --a; --b; if(c == "T"){t_relation.add_pair(a, b); continue;} if(c == "N"){n_relation.add_pair(a, b); continue;} assert(0); } vector<int> bosses(n, -2); unordered_set<int> full_set; for(int i = 0; i < n; ++i) full_set.insert(i); if(solve_for_subset(full_set, t_relation, n_relation, bosses, -1)) { #if defined(YES_NO_TESTING) verify_solution(t_relation, n_relation, bosses); cout << "TAK\n"; #else for(int b : bosses) { cout << b + 1 << "\n"; } #endif } else { cout << "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 | #include "bits/stdc++.h" using namespace std; struct relation { vector<vector<int>> related; vector<vector<int>> inv_related; relation(int n): related(n), inv_related(n){} void add_pair(int x, int y){ related[x].push_back(y); inv_related[y].push_back(x); } }; int pick_director( const unordered_set<int>& subset, const relation& t_relation, const relation& n_relation) { unordered_set<int> candidates = subset; for (int u : subset) { for (int v : t_relation.inv_related[u]) { candidates.erase(v); } } for (int u : subset) { for(int v : n_relation.related[u]) { candidates.erase(v); } } if(candidates.empty()) return -1; return *candidates.begin(); } bool solve_for_subset( const unordered_set<int>& subset, const relation& t_relation, const relation& n_relation, vector<int>& bosses, const int subset_director_boss) { int subset_director = pick_director(subset, t_relation, n_relation); if (subset_director == -1) return false; bosses[subset_director] = subset_director_boss; unordered_set<int> visited({subset_director}); const function<void(int, unordered_set<int>&)> dfs = [&](int u, unordered_set<int>& subsubset) { if ((!subset.count(u)) || subsubset.count(u) || u == subset_director){ return; } subsubset.insert(u); for (int v : t_relation.related[u]) { dfs(v, subsubset); } for (int v : t_relation.inv_related[u]) { dfs(v, subsubset); } }; for(int u : subset) { if (visited.count(u)) { continue; } unordered_set<int> subsubset; dfs(u, subsubset); if (!solve_for_subset( subsubset, t_relation, n_relation, bosses, subset_director)){ return false; }; visited.insert(subsubset.begin(), subsubset.end()); } return true; } void verify_solution( const relation& t_relation, const relation& n_relation, const vector<int>& bosses) { int n = bosses.size(); vector<unordered_set<int>> superiors(n); int directors = 0; for(int u = 0; u < n; ++u){ assert(bosses[u] != u); assert(bosses[u] >= -1); assert(bosses[u] < n); directors += (bosses[u] == -1); } assert(directors == 1); for(int u = 0; u < n; ++u){ for(int v = bosses[u]; v != -1; v = bosses[v]) { assert(!superiors[u].count(v)); superiors[u].insert(v); } } for(int u = 0; u < n; ++u){ for(int v : t_relation.related[u]){ assert(superiors[u].count(v) == 1); } for(int v : n_relation.related[u]){ assert(superiors[u].count(v) == 0); } } } int main(){ int n, m; ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; relation t_relation(n), n_relation(n); for(int i = 0; i < m; ++i){ int a, b; string c; cin >> a >> b >> c; --a; --b; if(c == "T"){t_relation.add_pair(a, b); continue;} if(c == "N"){n_relation.add_pair(a, b); continue;} assert(0); } vector<int> bosses(n, -2); unordered_set<int> full_set; for(int i = 0; i < n; ++i) full_set.insert(i); if(solve_for_subset(full_set, t_relation, n_relation, bosses, -1)) { #if defined(YES_NO_TESTING) verify_solution(t_relation, n_relation, bosses); cout << "TAK\n"; #else for(int b : bosses) { cout << b + 1 << "\n"; } #endif } else { cout << "NIE\n"; } return 0; } |