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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Jest tak na oko z 40 razy za wolne... Wysylam co jest, nie wiem, czy bede mial ochote na dalsze debugowanie...

#include <cstdio>
#include <vector>
#include <set>
#include <map>

using namespace std;

const int NOTHING = -999;
const int NODE_TYPE_CONNECTOR = -1;

const set<int> EMPTY_SET = set<int>();

int test_cases_number;

int nodes_number;
int roads_number;
int parties_number;

vector<int> node_type;
vector<int> pointer_to_list_of_connections;
vector<set<int>> connections_to_node;

vector<int> fu_rank;
vector<int> fu_parent;

vector<int> number_of_disjoint_sets_for_party;
vector<int> initial_node_id_for_party;

int number_of_parties_to_process;

vector<int> list_of_party_ids_to_process;

vector<int> master_temp_list_of_node_ids_to_unify_with;

map<int, int> master_temp_map_of_parties_groups_to_merge;
set<int> master_temp_set_of_connectors_to_iterate;

int fu_find(int item) {
  if (fu_parent[item] != item) {
    fu_parent[item] = fu_find(fu_parent[item]);
  }
  return fu_parent[item];
}

bool fu_union(int item_a, int item_b) {
  // standard unionize process
  int parent_a = fu_find(item_a);
  int parent_b = fu_find(item_b);

  if (parent_a == parent_b) { // already unionized
    // fprintf(stderr, "ITEMS %d AND %d WERE ALREADY UNIONIZED!\n", item_a, item_b);
    return false;
  }

  if (fu_rank[parent_a] < fu_rank[parent_b]) {
    fu_parent[parent_a] = parent_b;
  } else {
    fu_parent[parent_b] = parent_a;
    if (fu_rank[parent_a] == fu_rank[parent_b]) {
      fu_rank[parent_a] += 1;
    }
  }

  // fprintf(stderr, "ITEMS %d AND %d NEWLY CONNECTED!\n", item_a, item_b);
  return true; // it means that they became union now (ane were not before)
}

void unify_connections(int item_a_original_parent, int item_b_original_parent) {
  // fprintf(stderr, "Unifying connections for %d and %d...\n", item_a_original_parent, item_b_original_parent);
  // get parent id (should be the same for both, I assume fu_union was already called)
  int parent_id = fu_find(item_a_original_parent);

  // get pointers to connection lists
  int parent_connections_pointer = pointer_to_list_of_connections[parent_id];
  int item_id_with_new_connections;
  if (parent_connections_pointer != pointer_to_list_of_connections[item_a_original_parent]) {
    item_id_with_new_connections = item_a_original_parent;
  } else {
    item_id_with_new_connections = item_b_original_parent;

    // if (parent_connections_pointer == pointer_to_list_of_connections[item_b_original_parent]) {
    //   fprintf(stderr, "!!!!!!!!!!!!!!!!!!!! MAIN PROBLEM !!!!!!!!!! - item id with new connections NOT DETECTED (%d, %d)\n", item_a_original_parent, item_b_original_parent);
    // }
  }
  int new_item_connections_pointer = pointer_to_list_of_connections[item_id_with_new_connections];

  // we should import list of connections from smaller set - so if source is bigger, do the switch first! (and we need to set source and target)
  int source_pointer;
  int target_pointer;
  // fprintf(stderr, "Source number: %zu, target number: %zu\n", connections_to_node[new_item_connections_pointer].size(), connections_to_node[parent_connections_pointer].size());
  if (connections_to_node[new_item_connections_pointer].size() > connections_to_node[parent_connections_pointer].size()) {
    // fprintf(stderr, "Doing connections switch (%d, %d)\n", item_a_original_parent, item_b_original_parent);
    pointer_to_list_of_connections[parent_id] = new_item_connections_pointer;
    source_pointer = parent_connections_pointer;
    target_pointer = new_item_connections_pointer;
  } else {
    // if not, then we're not switching, but still we need to assign proper pointer to the item_id with new connections
    pointer_to_list_of_connections[item_id_with_new_connections] = parent_connections_pointer;
    source_pointer = new_item_connections_pointer;
    target_pointer = parent_connections_pointer;
  }

  // merge lists!
  // fprintf(stderr, "Starting merge iteration - number to iterate through: %zu\n", connections_to_node[source_pointer].size());
  for (int source_connection_to : connections_to_node[source_pointer]) {
    int connection_to_parent = fu_find(source_connection_to);
    connections_to_node[target_pointer].insert(connection_to_parent);
  }
}

void deduplicate_connections(int item_id) {
  // fprintf(stderr, "Deduplication for %d\n", item_id);
  int target_pointer = pointer_to_list_of_connections[item_id];
  vector<int> connections_to_delete;
  set<int> connections_to_add;
  for (int target_connection_to : connections_to_node[target_pointer]) {
    int target_connection_to_parent = fu_find(target_connection_to);
    if (target_connection_to_parent != target_connection_to) {
      connections_to_delete.push_back(target_connection_to);
      connections_to_add.insert(target_connection_to_parent);
    }
  }

  for (int connection_to_delete : connections_to_delete) {
    connections_to_node[target_pointer].erase(connection_to_delete);
  }

  for (int connection_to_add : connections_to_add) {
    connections_to_node[target_pointer].insert(connection_to_add);
  }
}

int main() {
  // read number of test cases (voivodeships)
  scanf("%d", &test_cases_number);

  for (int test_case = 1; test_case <= test_cases_number; test_case++) {
    // fprintf(stderr, "=== TEST CASE %d ===\n", test_case);

    // clear vectors, init variables
    node_type.clear();
    connections_to_node.clear();
    pointer_to_list_of_connections.clear();
    fu_rank.clear();
    fu_parent.clear();
    number_of_disjoint_sets_for_party.clear();
    list_of_party_ids_to_process.clear(); // do not push anything initially there below!

    // read basic numbers
    scanf("%d %d %d", &nodes_number, &roads_number, &parties_number);

    // init simple variables
    number_of_parties_to_process = parties_number;

    // add aritificial items, to start numbering from 1
    node_type.push_back(NOTHING);
    pointer_to_list_of_connections.push_back(NOTHING);
    connections_to_node.push_back(EMPTY_SET);
    fu_rank.push_back(NOTHING);
    fu_parent.push_back(NOTHING);
    number_of_disjoint_sets_for_party.push_back(NOTHING);
    initial_node_id_for_party.push_back(NOTHING);

    // init data sets for parties
    for (int party_id = 1; party_id <= parties_number; party_id++) {
      number_of_disjoint_sets_for_party.push_back(0);
      initial_node_id_for_party.push_back(NOTHING);
    }

    // read party ID assigned (at the end) to each node (city) + do some initialization
    for (int node_id = 1; node_id <= nodes_number; node_id++) {
      // read and assign party ID
      int party_id;
      scanf("%d", &party_id);
      node_type.push_back(party_id);

      // init data for find-union
      fu_rank.push_back(0);
      fu_parent.push_back(node_id);

      // initially that node is disjoint, so increase number of disjoint sets for that party
      number_of_disjoint_sets_for_party[party_id] += 1;

      // set initial node ID for that party
      initial_node_id_for_party[party_id] = node_id;

      // init data for node
      pointer_to_list_of_connections.push_back(node_id);
      connections_to_node.push_back(EMPTY_SET);
    }

    // read roads
    for (int road_id = 1; road_id <= roads_number; road_id++) {
      // read road
      int road_a;
      int road_b;
      scanf("%d %d", &road_a, &road_b);

      // apply connections
      connections_to_node[road_a].insert(road_b);
      connections_to_node[road_b].insert(road_a);
    }

    // fprintf(stderr, "DATA READ, VARS INITIALIZED, STARTING JOINING NODES\n");

    // itearte over nodes and connect the same parties together (unonize using find-union)
    for (int node_id = 1; node_id <= nodes_number; node_id++) {
      // fprintf(stderr, "=> initially connecting from node_id=%d\n", node_id);

      // clear helper vector
      master_temp_list_of_node_ids_to_unify_with.clear();

      // iterate over connections from the node
      for (int connection_to : connections_to_node[pointer_to_list_of_connections[node_id]]) {
        // fprintf(stderr, "---> connection to=%d\n", connection_to);

        if (node_type[node_id] == node_type[connection_to]) { // if source and target of connections are the same party, then try to connect
          // fprintf(stderr, "-----> saving to unify with=%d\n", connection_to);
          // save to the list of node ids to unify with (don't unify right away to not mess with the connections_to_node during iteration)
          master_temp_list_of_node_ids_to_unify_with.push_back(connection_to);
        }
      }

      // iterate over saved nodes to unify with
      for (int connection_to : master_temp_list_of_node_ids_to_unify_with) {
        // fprintf(stderr, "---> trying to unify with=%d\n", connection_to);

        // try to unify
        int node_id_parent = fu_find(node_id);
        int connection_to_parent = fu_find(connection_to);
        bool connection_result = fu_union(node_id_parent, connection_to_parent);
        if (connection_result) { // if newly connected, then do more post connection work
          // reduce number of disjoint sets for that party
          number_of_disjoint_sets_for_party[node_type[node_id]] -= 1;

          // unify connections
          unify_connections(node_id_parent, connection_to_parent);

          // test...
          // deduplicate_connections(fu_find(node_id_parent));// TODO: potrzebne? czy zwalnia?
        }
      }
    }

    // review parties and already mark as processed all with disjoint sets == 0, and add to the list to process those with == 1
    for (int party_id = 1; party_id <= parties_number; party_id++) {
      int temp_number_of_disjoint_sets_for_party = number_of_disjoint_sets_for_party[party_id];
      if (temp_number_of_disjoint_sets_for_party == 0) { // if 0, it means that no city has this party assigned, we can mark as processed already
        number_of_parties_to_process -= 1;
      } else if (temp_number_of_disjoint_sets_for_party == 1) { // if 1, it means that all parties are grouped together, ready to process
        list_of_party_ids_to_process.push_back(party_id);
      }
    }

    // fprintf(stderr, "NODES INITIALLY JOINED, number_of_parties_to_process=%d, size_of_list_to_process=%zu\n", number_of_parties_to_process, list_of_party_ids_to_process.size());

    // process all parties, unless list to process is cleared (it can grow in the meantime)
    while (!list_of_party_ids_to_process.empty()) {
      // get party ID to process
      int party_id_to_process = list_of_party_ids_to_process.back();
      // fprintf(stderr, "****>> processing party id=%d; number_of_parties_to_process=%d\n", party_id_to_process, number_of_parties_to_process);
      list_of_party_ids_to_process.pop_back();

      // decrease number of parties to process
      number_of_parties_to_process -= 1;

      // get parent ID for that party
      int parent_id = fu_find(initial_node_id_for_party[party_id_to_process]);

      // change type of node to connector (after processing all nodes from the party becomes connectors for other parties)
      node_type[parent_id] = NODE_TYPE_CONNECTOR;

      // get pointer to connections
      int connections_pointer = pointer_to_list_of_connections[parent_id];

      // clear helper structures
      master_temp_map_of_parties_groups_to_merge.clear();
      master_temp_set_of_connectors_to_iterate.clear();







      // iterate over connections
      // fprintf(stderr, "    >> number of connections to iterate through (A)=%zu\n", connections_to_node[connections_pointer].size());
      for (int connection_to : connections_to_node[connections_pointer]) {
        int connection_to_parent = fu_find(connection_to);
        if (connection_to_parent != parent_id) { // probably it can be equal (sometimes connections not deduplicated)
          int connection_to_node_type = node_type[connection_to_parent];
          if (connection_to_node_type == NODE_TYPE_CONNECTOR) { // if target is connector, save it to iterate through it as well (and to merge with!)
            master_temp_set_of_connectors_to_iterate.insert(connection_to_parent);
          }
        }
      }

      // merge connectors
      for (int connector_parent_id : master_temp_set_of_connectors_to_iterate) {
        // try to unify
        int fresh_parent_id = fu_find(parent_id);
        int fresh_connector_parent_id = fu_find(connector_parent_id);
        bool connection_result = fu_union(parent_id, connector_parent_id);
        if (connection_result) { // if newly connected, then do more post connection work
          // unify connections
          unify_connections(fresh_parent_id, fresh_connector_parent_id);
        }
      }

      deduplicate_connections(fu_find(parent_id));

      connections_pointer = pointer_to_list_of_connections[fu_find(parent_id)];
      
      // iterate over connections
      // fprintf(stderr, "    >> number of connections to iterate through (B)=%zu\n", connections_to_node[connections_pointer].size());
      for (int connection_to : connections_to_node[connections_pointer]) {
        int connection_to_parent = fu_find(connection_to);
        if (connection_to_parent != parent_id) { // probably it can be equal (sometimes connections not deduplicated)
          int connection_to_node_type = node_type[connection_to_parent];
          if (connection_to_node_type != NODE_TYPE_CONNECTOR) { // if target is connector, save it to iterate through it as well (and to merge with!)
            if (master_temp_map_of_parties_groups_to_merge.contains(connection_to_node_type)) {
              int saved_target_node = master_temp_map_of_parties_groups_to_merge[connection_to_node_type];
              if (saved_target_node != connection_to_parent) { // new group from the same party - merge!!!
                // try to unify
                int fresh_saved_target_node = fu_find(saved_target_node);
                int fresh_connection_to_parent = fu_find(connection_to_parent);
                bool connection_result = fu_union(saved_target_node, connection_to_parent);
                if (connection_result) { // if newly connected, then do more post connection work
                  // reduce number of disjoint sets for that party
                  number_of_disjoint_sets_for_party[connection_to_node_type] -= 1;

                  // add to list to process if party become one connected set
                  if (number_of_disjoint_sets_for_party[connection_to_node_type] == 1) {
                    list_of_party_ids_to_process.push_back(connection_to_node_type);
                  }

                  // unify connections
                  unify_connections(fresh_saved_target_node, fresh_connection_to_parent);
                }
              }
            } else {
              // if for given party we don't have any targets yet, add it to the map
              master_temp_map_of_parties_groups_to_merge[connection_to_node_type] = connection_to_parent;
            }
          }
        }
      }





      // // iterate over connections
      // fprintf(stderr, "    >> number of connections to iterate through=%zu\n", connections_to_node[connections_pointer].size());
      // for (int connection_to : connections_to_node[connections_pointer]) {
      //   int connection_to_parent = fu_find(connection_to);
      //   if (connection_to_parent != parent_id) { // probably it can be equal (sometimes connections not deduplicated)
      //     int connection_to_node_type = node_type[connection_to_parent];
      //     if (connection_to_node_type == NODE_TYPE_CONNECTOR) { // if target is connector, save it to iterate through it as well (and to merge with!)
      //       master_temp_set_of_connectors_to_iterate.insert(connection_to_parent);
      //     } else { // otherwise it's party
      //       if (master_temp_map_of_parties_groups_to_merge.contains(connection_to_node_type)) {
      //         int saved_target_node = master_temp_map_of_parties_groups_to_merge[connection_to_node_type];
      //         if (saved_target_node != connection_to_parent) { // new group from the same party - merge!!!
      //           // try to unify
      //           int fresh_saved_target_node = fu_find(saved_target_node);
      //           int fresh_connection_to_parent = fu_find(connection_to_parent);
      //           bool connection_result = fu_union(saved_target_node, connection_to_parent);
      //           if (connection_result) { // if newly connected, then do more post connection work
      //             // reduce number of disjoint sets for that party
      //             number_of_disjoint_sets_for_party[connection_to_node_type] -= 1;

      //             // add to list to process if party become one connected set
      //             if (number_of_disjoint_sets_for_party[connection_to_node_type] == 1) {
      //               list_of_party_ids_to_process.push_back(connection_to_node_type);
      //             }

      //             // unify connections
      //             unify_connections(fresh_saved_target_node, fresh_connection_to_parent);
      //           }
      //         }
      //       } else {
      //         // if for given party we don't have any targets yet, add it to the map
      //         master_temp_map_of_parties_groups_to_merge[connection_to_node_type] = connection_to_parent;
      //       }
      //     }
      //   }
      // }

      // // iterate over connected connectors - and over their connections
      // fprintf(stderr, "    >> number of connections (connectors) to iterate through=%zu\n", master_temp_set_of_connectors_to_iterate.size());
      // for (int connector_parent_id : master_temp_set_of_connectors_to_iterate) {
      //   // iterate over connections
      //   for (int connection_to : connections_to_node[pointer_to_list_of_connections[connector_parent_id]]) {
      //     int connection_to_parent = fu_find(connection_to);
      //     if (connection_to_parent != parent_id) { // probably it can be equal (sometimes connections not deduplicated)
      //       int connection_to_node_type = node_type[connection_to_parent];
      //       if (connection_to_node_type != NODE_TYPE_CONNECTOR) { // iterating through connectors we ignore connection to other connectors
      //         if (master_temp_map_of_parties_groups_to_merge.contains(connection_to_node_type)) {
      //           int saved_target_node = master_temp_map_of_parties_groups_to_merge[connection_to_node_type];
      //           if (saved_target_node != connection_to_parent) { // new group from the same party - merge!!!
      //             // try to unify
      //             int fresh_saved_target_node = fu_find(saved_target_node);
      //             int fresh_connection_to_parent = fu_find(connection_to_parent);
      //             bool connection_result = fu_union(saved_target_node, connection_to_parent);
      //             if (connection_result) { // if newly connected, then do more post connection work
      //               // reduce number of disjoint sets for that party
      //               number_of_disjoint_sets_for_party[connection_to_node_type] -= 1;

      //               // add to list to process if party become one connected set
      //               if (number_of_disjoint_sets_for_party[connection_to_node_type] == 1) {
      //                 list_of_party_ids_to_process.push_back(connection_to_node_type);
      //               }

      //               // unify connections
      //               unify_connections(fresh_saved_target_node, fresh_connection_to_parent);
      //             }
      //           }
      //         } else {
      //           // if for given party we don't have any targets yet, add it to the map
      //           master_temp_map_of_parties_groups_to_merge[connection_to_node_type] = connection_to_parent;
      //         }
      //       }
      //     }
      //   }
      // }

      // // merge connectors
      // for (int connector_parent_id : master_temp_set_of_connectors_to_iterate) {
      //   // try to unify
      //   int fresh_parent_id = fu_find(parent_id);
      //   int fresh_connector_parent_id = fu_find(connector_parent_id);
      //   bool connection_result = fu_union(parent_id, connector_parent_id);
      //   if (connection_result) { // if newly connected, then do more post connection work
      //     // unify connections
      //     unify_connections(fresh_parent_id, fresh_connector_parent_id);
      //   }
      // }

      // // deduplicate connections (not sure if it helps or breaks time complexity overall...)
      // deduplicate_connections(fu_find(parent_id));




    }

    // processing finished, prepare response
    // fprintf(stderr, "@@@@ number_of_parties_to_process=%d\n", number_of_parties_to_process);
    if (number_of_parties_to_process > 0) {
      printf("NIE\n");
    } else {
      printf("TAK\n");
    }
  }

  return 0;
}