#include <iostream>
#include <vector>
#include <set>
#include <map>
#define REP(x,n) for(int x=0;x<(n);++x)
#define FOREACH(x, c) for(auto x = (c).begin(); x != (c).end(); ++x)
//#define DEBUG_LEVEL -10
#ifdef DEBUG_LEVEL
#define DEBUG(level, x) {if (level >= DEBUG_LEVEL) {x}}
#else
#define DEBUG(level, x)
#endif
using namespace std;
const int MAX_N = 100001;
const int GLOBAL_PARTY = -1;
int scores[MAX_N];
int scoresPerParty[MAX_N];
struct Node {
vector<int> edges;
map<int, set<Node*>> neighbourRootsPerParty;
Node* ancestor;
int party;
} graph[MAX_N];
Node* getRoot(Node* current) {
if (current->ancestor == nullptr) {
return current;
}
return current->ancestor = getRoot(current->ancestor);
}
set<Node*> queue;
set<Node*> rootsPerParty[MAX_N];
void processInitial(int start, int depth) {
Node& thisNode = graph[start];
Node* thisRoot = getRoot(&thisNode);
DEBUG(0, cerr << __LINE__ << string(2*depth, ' ')<< " Processing node " << start << " with party " << thisNode.party << " and root " << thisRoot-graph << endl;)
FOREACH(it, thisNode.edges) {
int targetNo = *it;
Node& targetNode = graph[targetNo];
Node* targetRoot = getRoot(&targetNode);
DEBUG(-3, cerr << __LINE__ << string(2*depth, ' ')<< " Checking neighbour " << targetNo << " with party " << targetNode.party << " and root " << targetRoot-graph << endl;)
if (targetRoot->party == thisRoot->party && targetRoot != thisRoot) {
DEBUG(-1, cerr << __LINE__ << string(2*depth, ' ')<< " Adding node " << targetNo << " to current set" << endl;)
targetNode.ancestor = thisRoot;
--scoresPerParty[targetNode.party];
DEBUG(-2, cerr << __LINE__ << string(2*depth, ' ') << " For party " << targetNode.party << " remain " << scoresPerParty[targetNode.party] << " nodes" << endl;)
processInitial(targetNo, depth+1);
}
}
if (scoresPerParty[thisNode.party] == 1) {
DEBUG(-1, cerr << __LINE__ << " Party " << thisNode.party << " is coherent" << endl;)
scoresPerParty[thisNode.party] = 0;
FOREACH(it, thisNode.edges) {
Node& neighbour = graph[*it];
neighbour.neighbourRootsPerParty[thisNode.party].erase(&thisNode);
getRoot(&neighbour)->neighbourRootsPerParty[thisNode.party].erase(&thisNode);
getRoot(&neighbour)->neighbourRootsPerParty[GLOBAL_PARTY].insert(thisRoot);
}
thisRoot->party = GLOBAL_PARTY;
queue.insert(thisRoot);
}
}
bool inline contains(set<Node*>& nodes, Node* element) {
return nodes.find(element) != nodes.end();
}
bool allCanReach(set<Node*>& nodes, Node* target) {
FOREACH(nodeIt, nodes) {
if (!contains((*nodeIt)->neighbourRootsPerParty[target->party], target)) {
DEBUG(-3, cerr << __LINE__ << " Node " << (*nodeIt)-graph << " cannot reach " << target-graph << endl;)
return false;
}
}
DEBUG(-2, cerr << __LINE__ << " All nodes for party " << (*nodes.begin())->party << " can reach " << target-graph << endl;)
return true;
}
void reassignNeighbours(Node* from, Node* to) {
DEBUG(-3, cerr << __LINE__ << " Reassigning neighbours from " << from-graph << " to " << to-graph << endl;)
FOREACH(nrppIt, from->neighbourRootsPerParty) {
FOREACH(it, nrppIt->second) {
Node* neighbour = *it;
if (neighbour != to) {
DEBUG(-4, cerr << __LINE__ << " Reassigning neighbour " << neighbour-graph << " for party " << nrppIt->first << endl;)
Node* neighbourRoot = getRoot(neighbour);
auto& nrpp = neighbourRoot->neighbourRootsPerParty[from->party];
nrpp.erase(from);
if (neighbourRoot != to) {
neighbourRoot->neighbourRootsPerParty[to->party].insert(to);
to->neighbourRootsPerParty[nrppIt->first].insert(neighbourRoot);
}
DEBUG(-4, cerr << __LINE__ << " Finished reassigning neighbour " << neighbour-graph << " for party " << nrppIt->first << endl;)
}
}
}
DEBUG(-3, cerr << __LINE__ << " Finished reassigning neighbours from " << from-graph << " to " << to-graph << endl;)
}
void mergeParty(set<Node*>& partyRoots) {
Node* mainRoot = *partyRoots.begin();
DEBUG(-1, cerr << __LINE__ << " Merging party " << mainRoot->party << " with main root " << mainRoot-graph << endl;)
partyRoots.erase(mainRoot);
while (!partyRoots.empty()) {
Node* root = *partyRoots.begin();
partyRoots.erase(root);
if (root != mainRoot) {
DEBUG(-2, cerr << __LINE__ << " Merging " << root-graph << " into " << mainRoot-graph << endl;)
root->ancestor = mainRoot;
rootsPerParty[mainRoot->party].erase(root);
reassignNeighbours(root, mainRoot);
}
}
partyRoots.insert(mainRoot);
DEBUG(-1, cerr << __LINE__ << " Finished merging party " << mainRoot->party << " with main root " << mainRoot-graph << endl;)
}
void mergeGlobal(Node* mainRoot, set<Node*>& nodesToMerge) {
DEBUG(-2, cerr << __LINE__ << " Started merging global neighbours into " << mainRoot-graph << endl;)
while (!nodesToMerge.empty()) {
Node* neighbour = *nodesToMerge.begin();
nodesToMerge.erase(neighbour);
if (neighbour->ancestor == nullptr && neighbour != mainRoot) {
DEBUG(-1, cerr << __LINE__ << " Merging global neighbour " << neighbour-graph << " into " << mainRoot-graph << endl;)
neighbour->ancestor = mainRoot;
reassignNeighbours(neighbour, mainRoot);
neighbour->party = GLOBAL_PARTY;
queue.insert(neighbour);
}
}
DEBUG(-2, cerr << __LINE__ << " Finished merging global neighbours into " << mainRoot-graph << endl;)
}
void processRoot(Node* currentRoot) {
if (currentRoot->ancestor != nullptr) {
DEBUG(-1, cerr << "Skip processing global root " << currentRoot-graph << ", remaining: " << queue.size() << endl;)
return;
}
DEBUG(-1, cerr << "Processing global root " << currentRoot-graph << ", remaining: " << queue.size() << endl;)
DEBUG(-3,
cerr << __LINE__ << " with neighbours: ";
FOREACH(partyIt, currentRoot->neighbourRootsPerParty) {
cerr << partyIt->first << ": { ";
FOREACH(neighbourIt, partyIt->second) {
cerr << (*neighbourIt)-graph << " ";
}
cerr << "} ";
}
cerr << endl;
)
auto globalNeighbourRootsIt = currentRoot->neighbourRootsPerParty.find(GLOBAL_PARTY);
if (globalNeighbourRootsIt != currentRoot->neighbourRootsPerParty.end()) {
DEBUG(-2,
cerr << __LINE__ << " Merging with global neighbours: ";
FOREACH(it, globalNeighbourRootsIt->second) {
cerr << " " << (*it) - graph;
}
cerr << endl;
)
mergeGlobal(currentRoot, globalNeighbourRootsIt->second);
currentRoot->neighbourRootsPerParty.erase(globalNeighbourRootsIt);
DEBUG(-3,
cerr << __LINE__ << " Neighbours after merging: ";
FOREACH(partyIt, currentRoot->neighbourRootsPerParty) {
cerr << partyIt->first << ": { ";
FOREACH(neighbourIt, partyIt->second) {
cerr << (*neighbourIt)-graph << " ";
}
cerr << "} ";
}
cerr << endl;
if (getRoot(currentRoot) != currentRoot) {
cerr << __LINE__ << " Root neighbours after merging: ";
FOREACH(partyIt, getRoot(currentRoot)->neighbourRootsPerParty) {
cerr << partyIt->first << ": { ";
FOREACH(neighbourIt, partyIt->second) {
cerr << (*neighbourIt)-graph << " ";
}
cerr << "} ";
}
cerr << endl;
}
)
}
Node* newRoot = getRoot(currentRoot); // maybe we should check the ancestor
FOREACH(nrppIt, currentRoot->neighbourRootsPerParty) {
if (scoresPerParty[nrppIt->first] == 0) {
DEBUG(-3, cerr << __LINE__ << " Party " << nrppIt->first << " already coherent, skipping" << endl;)
continue;
}
int party = nrppIt->first;
set<Node*>& fullNeighbours = newRoot->neighbourRootsPerParty[party];
if (fullNeighbours.size() == rootsPerParty[party].size()) {
scoresPerParty[party] = 0;
DEBUG(-3, cerr << __LINE__ << " Connects with all " << rootsPerParty[party].size() << " roots of party " << party << endl;)
auto& rpp = rootsPerParty[party];
while (!rpp.empty()) {
Node* partyRoot = *rpp.begin();
rpp.erase(partyRoot);
DEBUG(-3, cerr << __LINE__ << " Adding root " << partyRoot-graph << " to queue" << endl;)
partyRoot->party = GLOBAL_PARTY;
queue.insert(partyRoot);
}
} else if (nrppIt->second.size() > 1) {
DEBUG(-3,
cerr << __LINE__ << " Merging " << nrppIt->second.size() << " roots for party " << party << " together: ";
FOREACH(neighbourRootIt, nrppIt->second) {
cerr << (*neighbourRootIt)-graph << " ";
}
cerr << endl;
)
mergeParty(nrppIt->second);
}
}
}
void processRoots() {
DEBUG(-1, cerr << __LINE__ << " Processing queue of size: " << queue.size() << endl;)
while (!queue.empty()) {
Node* currentRoot = *queue.begin();
queue.erase(currentRoot);
processRoot(currentRoot);
}
}
bool solve() {
queue.clear();
int n,m,k, a,b, party;
cin >> n >> m >> k;
REP(x,k) {
scoresPerParty[x+1] = 0;
rootsPerParty[x+1].clear();
}
REP(x,n) {
cin >> party;
scores[x] = party;
graph[x].edges.clear();
graph[x].neighbourRootsPerParty.clear();
graph[x].ancestor = nullptr;
graph[x].party = scores[x];
++scoresPerParty[party];
}
REP(x,m) {
cin >> a >> b;
graph[a-1].edges.push_back(b-1);
graph[b-1].edges.push_back(a-1);
}
REP(x,n) {
if (graph[x].ancestor == nullptr) {
processInitial(x, 0);
}
}
set<Node*> roots;
REP(x,n) {
Node& thisNode = graph[x];
Node* thisRoot = getRoot(&thisNode);
roots.insert(thisRoot);
if (thisRoot->party != GLOBAL_PARTY) {
rootsPerParty[thisRoot->party].insert(thisRoot);
DEBUG(-3, cerr << __LINE__ << " Adding root " << thisRoot-graph << " for party " << thisRoot->party << " from node " << x << endl;)
}
FOREACH(it, thisNode.edges) {
Node& otherNode = graph[*it];
if (&otherNode != thisRoot) {
thisRoot->neighbourRootsPerParty[otherNode.party].insert(getRoot(&otherNode));
}
}
}
DEBUG(-1,
cerr << __LINE__ << " Neighbour roots per party:" << endl;
REP(x,k) {
cerr << " Party " << x+1 << ": " << endl;
FOREACH(it, rootsPerParty[x+1]) {
Node* root = *it;
cerr << " * Root " << root-graph << " with neighbours: ";
FOREACH(partyIt, root->neighbourRootsPerParty) {
cerr << partyIt->first << ": { ";
FOREACH(neighbourIt, partyIt->second) {
cerr << (*neighbourIt)-graph << " ";
}
cerr << "} ";
}
cerr << endl;
}
}
cerr << __LINE__ << " Remaining parties:" << endl << "\t";
REP(x,k) {
cerr << scoresPerParty[x+1] << " ";
}
cerr << endl;
)
processRoots();
DEBUG(-1,
cerr << __LINE__ << " Result party:" << endl << "\t";
REP(x,k) {
cerr << scoresPerParty[x+1] << " ";
}
cerr << endl;
)
REP(x,k) {
if (scoresPerParty[x+1] > 0) {
return false;
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int t;
cin>>t;
REP(x,t) {
cout << (solve() ? "TAK" : "NIE") << endl;
}
}
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | #include <iostream> #include <vector> #include <set> #include <map> #define REP(x,n) for(int x=0;x<(n);++x) #define FOREACH(x, c) for(auto x = (c).begin(); x != (c).end(); ++x) //#define DEBUG_LEVEL -10 #ifdef DEBUG_LEVEL #define DEBUG(level, x) {if (level >= DEBUG_LEVEL) {x}} #else #define DEBUG(level, x) #endif using namespace std; const int MAX_N = 100001; const int GLOBAL_PARTY = -1; int scores[MAX_N]; int scoresPerParty[MAX_N]; struct Node { vector<int> edges; map<int, set<Node*>> neighbourRootsPerParty; Node* ancestor; int party; } graph[MAX_N]; Node* getRoot(Node* current) { if (current->ancestor == nullptr) { return current; } return current->ancestor = getRoot(current->ancestor); } set<Node*> queue; set<Node*> rootsPerParty[MAX_N]; void processInitial(int start, int depth) { Node& thisNode = graph[start]; Node* thisRoot = getRoot(&thisNode); DEBUG(0, cerr << __LINE__ << string(2*depth, ' ')<< " Processing node " << start << " with party " << thisNode.party << " and root " << thisRoot-graph << endl;) FOREACH(it, thisNode.edges) { int targetNo = *it; Node& targetNode = graph[targetNo]; Node* targetRoot = getRoot(&targetNode); DEBUG(-3, cerr << __LINE__ << string(2*depth, ' ')<< " Checking neighbour " << targetNo << " with party " << targetNode.party << " and root " << targetRoot-graph << endl;) if (targetRoot->party == thisRoot->party && targetRoot != thisRoot) { DEBUG(-1, cerr << __LINE__ << string(2*depth, ' ')<< " Adding node " << targetNo << " to current set" << endl;) targetNode.ancestor = thisRoot; --scoresPerParty[targetNode.party]; DEBUG(-2, cerr << __LINE__ << string(2*depth, ' ') << " For party " << targetNode.party << " remain " << scoresPerParty[targetNode.party] << " nodes" << endl;) processInitial(targetNo, depth+1); } } if (scoresPerParty[thisNode.party] == 1) { DEBUG(-1, cerr << __LINE__ << " Party " << thisNode.party << " is coherent" << endl;) scoresPerParty[thisNode.party] = 0; FOREACH(it, thisNode.edges) { Node& neighbour = graph[*it]; neighbour.neighbourRootsPerParty[thisNode.party].erase(&thisNode); getRoot(&neighbour)->neighbourRootsPerParty[thisNode.party].erase(&thisNode); getRoot(&neighbour)->neighbourRootsPerParty[GLOBAL_PARTY].insert(thisRoot); } thisRoot->party = GLOBAL_PARTY; queue.insert(thisRoot); } } bool inline contains(set<Node*>& nodes, Node* element) { return nodes.find(element) != nodes.end(); } bool allCanReach(set<Node*>& nodes, Node* target) { FOREACH(nodeIt, nodes) { if (!contains((*nodeIt)->neighbourRootsPerParty[target->party], target)) { DEBUG(-3, cerr << __LINE__ << " Node " << (*nodeIt)-graph << " cannot reach " << target-graph << endl;) return false; } } DEBUG(-2, cerr << __LINE__ << " All nodes for party " << (*nodes.begin())->party << " can reach " << target-graph << endl;) return true; } void reassignNeighbours(Node* from, Node* to) { DEBUG(-3, cerr << __LINE__ << " Reassigning neighbours from " << from-graph << " to " << to-graph << endl;) FOREACH(nrppIt, from->neighbourRootsPerParty) { FOREACH(it, nrppIt->second) { Node* neighbour = *it; if (neighbour != to) { DEBUG(-4, cerr << __LINE__ << " Reassigning neighbour " << neighbour-graph << " for party " << nrppIt->first << endl;) Node* neighbourRoot = getRoot(neighbour); auto& nrpp = neighbourRoot->neighbourRootsPerParty[from->party]; nrpp.erase(from); if (neighbourRoot != to) { neighbourRoot->neighbourRootsPerParty[to->party].insert(to); to->neighbourRootsPerParty[nrppIt->first].insert(neighbourRoot); } DEBUG(-4, cerr << __LINE__ << " Finished reassigning neighbour " << neighbour-graph << " for party " << nrppIt->first << endl;) } } } DEBUG(-3, cerr << __LINE__ << " Finished reassigning neighbours from " << from-graph << " to " << to-graph << endl;) } void mergeParty(set<Node*>& partyRoots) { Node* mainRoot = *partyRoots.begin(); DEBUG(-1, cerr << __LINE__ << " Merging party " << mainRoot->party << " with main root " << mainRoot-graph << endl;) partyRoots.erase(mainRoot); while (!partyRoots.empty()) { Node* root = *partyRoots.begin(); partyRoots.erase(root); if (root != mainRoot) { DEBUG(-2, cerr << __LINE__ << " Merging " << root-graph << " into " << mainRoot-graph << endl;) root->ancestor = mainRoot; rootsPerParty[mainRoot->party].erase(root); reassignNeighbours(root, mainRoot); } } partyRoots.insert(mainRoot); DEBUG(-1, cerr << __LINE__ << " Finished merging party " << mainRoot->party << " with main root " << mainRoot-graph << endl;) } void mergeGlobal(Node* mainRoot, set<Node*>& nodesToMerge) { DEBUG(-2, cerr << __LINE__ << " Started merging global neighbours into " << mainRoot-graph << endl;) while (!nodesToMerge.empty()) { Node* neighbour = *nodesToMerge.begin(); nodesToMerge.erase(neighbour); if (neighbour->ancestor == nullptr && neighbour != mainRoot) { DEBUG(-1, cerr << __LINE__ << " Merging global neighbour " << neighbour-graph << " into " << mainRoot-graph << endl;) neighbour->ancestor = mainRoot; reassignNeighbours(neighbour, mainRoot); neighbour->party = GLOBAL_PARTY; queue.insert(neighbour); } } DEBUG(-2, cerr << __LINE__ << " Finished merging global neighbours into " << mainRoot-graph << endl;) } void processRoot(Node* currentRoot) { if (currentRoot->ancestor != nullptr) { DEBUG(-1, cerr << "Skip processing global root " << currentRoot-graph << ", remaining: " << queue.size() << endl;) return; } DEBUG(-1, cerr << "Processing global root " << currentRoot-graph << ", remaining: " << queue.size() << endl;) DEBUG(-3, cerr << __LINE__ << " with neighbours: "; FOREACH(partyIt, currentRoot->neighbourRootsPerParty) { cerr << partyIt->first << ": { "; FOREACH(neighbourIt, partyIt->second) { cerr << (*neighbourIt)-graph << " "; } cerr << "} "; } cerr << endl; ) auto globalNeighbourRootsIt = currentRoot->neighbourRootsPerParty.find(GLOBAL_PARTY); if (globalNeighbourRootsIt != currentRoot->neighbourRootsPerParty.end()) { DEBUG(-2, cerr << __LINE__ << " Merging with global neighbours: "; FOREACH(it, globalNeighbourRootsIt->second) { cerr << " " << (*it) - graph; } cerr << endl; ) mergeGlobal(currentRoot, globalNeighbourRootsIt->second); currentRoot->neighbourRootsPerParty.erase(globalNeighbourRootsIt); DEBUG(-3, cerr << __LINE__ << " Neighbours after merging: "; FOREACH(partyIt, currentRoot->neighbourRootsPerParty) { cerr << partyIt->first << ": { "; FOREACH(neighbourIt, partyIt->second) { cerr << (*neighbourIt)-graph << " "; } cerr << "} "; } cerr << endl; if (getRoot(currentRoot) != currentRoot) { cerr << __LINE__ << " Root neighbours after merging: "; FOREACH(partyIt, getRoot(currentRoot)->neighbourRootsPerParty) { cerr << partyIt->first << ": { "; FOREACH(neighbourIt, partyIt->second) { cerr << (*neighbourIt)-graph << " "; } cerr << "} "; } cerr << endl; } ) } Node* newRoot = getRoot(currentRoot); // maybe we should check the ancestor FOREACH(nrppIt, currentRoot->neighbourRootsPerParty) { if (scoresPerParty[nrppIt->first] == 0) { DEBUG(-3, cerr << __LINE__ << " Party " << nrppIt->first << " already coherent, skipping" << endl;) continue; } int party = nrppIt->first; set<Node*>& fullNeighbours = newRoot->neighbourRootsPerParty[party]; if (fullNeighbours.size() == rootsPerParty[party].size()) { scoresPerParty[party] = 0; DEBUG(-3, cerr << __LINE__ << " Connects with all " << rootsPerParty[party].size() << " roots of party " << party << endl;) auto& rpp = rootsPerParty[party]; while (!rpp.empty()) { Node* partyRoot = *rpp.begin(); rpp.erase(partyRoot); DEBUG(-3, cerr << __LINE__ << " Adding root " << partyRoot-graph << " to queue" << endl;) partyRoot->party = GLOBAL_PARTY; queue.insert(partyRoot); } } else if (nrppIt->second.size() > 1) { DEBUG(-3, cerr << __LINE__ << " Merging " << nrppIt->second.size() << " roots for party " << party << " together: "; FOREACH(neighbourRootIt, nrppIt->second) { cerr << (*neighbourRootIt)-graph << " "; } cerr << endl; ) mergeParty(nrppIt->second); } } } void processRoots() { DEBUG(-1, cerr << __LINE__ << " Processing queue of size: " << queue.size() << endl;) while (!queue.empty()) { Node* currentRoot = *queue.begin(); queue.erase(currentRoot); processRoot(currentRoot); } } bool solve() { queue.clear(); int n,m,k, a,b, party; cin >> n >> m >> k; REP(x,k) { scoresPerParty[x+1] = 0; rootsPerParty[x+1].clear(); } REP(x,n) { cin >> party; scores[x] = party; graph[x].edges.clear(); graph[x].neighbourRootsPerParty.clear(); graph[x].ancestor = nullptr; graph[x].party = scores[x]; ++scoresPerParty[party]; } REP(x,m) { cin >> a >> b; graph[a-1].edges.push_back(b-1); graph[b-1].edges.push_back(a-1); } REP(x,n) { if (graph[x].ancestor == nullptr) { processInitial(x, 0); } } set<Node*> roots; REP(x,n) { Node& thisNode = graph[x]; Node* thisRoot = getRoot(&thisNode); roots.insert(thisRoot); if (thisRoot->party != GLOBAL_PARTY) { rootsPerParty[thisRoot->party].insert(thisRoot); DEBUG(-3, cerr << __LINE__ << " Adding root " << thisRoot-graph << " for party " << thisRoot->party << " from node " << x << endl;) } FOREACH(it, thisNode.edges) { Node& otherNode = graph[*it]; if (&otherNode != thisRoot) { thisRoot->neighbourRootsPerParty[otherNode.party].insert(getRoot(&otherNode)); } } } DEBUG(-1, cerr << __LINE__ << " Neighbour roots per party:" << endl; REP(x,k) { cerr << " Party " << x+1 << ": " << endl; FOREACH(it, rootsPerParty[x+1]) { Node* root = *it; cerr << " * Root " << root-graph << " with neighbours: "; FOREACH(partyIt, root->neighbourRootsPerParty) { cerr << partyIt->first << ": { "; FOREACH(neighbourIt, partyIt->second) { cerr << (*neighbourIt)-graph << " "; } cerr << "} "; } cerr << endl; } } cerr << __LINE__ << " Remaining parties:" << endl << "\t"; REP(x,k) { cerr << scoresPerParty[x+1] << " "; } cerr << endl; ) processRoots(); DEBUG(-1, cerr << __LINE__ << " Result party:" << endl << "\t"; REP(x,k) { cerr << scoresPerParty[x+1] << " "; } cerr << endl; ) REP(x,k) { if (scoresPerParty[x+1] > 0) { return false; } } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); int t; cin>>t; REP(x,t) { cout << (solve() ? "TAK" : "NIE") << endl; } } |
English