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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string>

using namespace std;

bool solve() {
    int n, m, k;
    cin >> n >> m >> k;

    vector<int> a(n + 1);
    vector<vector<int>> party_cities(k + 1);

    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        party_cities[a[i]].push_back(i);
    }

    vector<vector<int>> adj(n + 1);
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    vector<bool> is_wildcard(n + 1, false);
    vector<bool> in_queue(k + 1, false);
    vector<bool> processed(k + 1, false);
    queue<int> q;

    vector<int> visited_tag(n + 1, 0);
    int current_tag = 0;

    auto can_remove = [&](int p) {
        if (party_cities[p].empty()) return true;

        current_tag++;
        queue<int> bfs_q;
        int start_node = party_cities[p][0];

        bfs_q.push(start_node);
        visited_tag[start_node] = current_tag;

        int found_count = 0;

        while (!bfs_q.empty()) {
            int u = bfs_q.front();
            bfs_q.pop();

            if (a[u] == p) {
                found_count++;
            }

            for (int v : adj[u]) {
                if (visited_tag[v] != current_tag) {
                    if (a[v] == p || is_wildcard[v]) {
                        visited_tag[v] = current_tag;
                        bfs_q.push(v);
                    }
                }
            }
        }
        return found_count == party_cities[p].size();
        };

    for (int i = 1; i <= k; ++i) {
        if (!party_cities[i].empty() && can_remove(i)) {
            q.push(i);
            in_queue[i] = true;
        }
    }

    while (!q.empty()) {
        int p = q.front();
        q.pop();
        processed[p] = true;

        for (int u : party_cities[p]) {
            is_wildcard[u] = true;
        }

        vector<int> to_check;
        for (int u : party_cities[p]) {
            for (int v : adj[u]) {
                int neighbor_party = a[v];
                if (!processed[neighbor_party] && !in_queue[neighbor_party]) {
                    to_check.push_back(neighbor_party);
                }
            }
        }

        sort(to_check.begin(), to_check.end());
        to_check.erase(unique(to_check.begin(), to_check.end()), to_check.end());

        for (int np : to_check) {
            if (!in_queue[np] && can_remove(np)) {
                q.push(np);
                in_queue[np] = true;
            }
        }
    }

    bool ok = true;
    for (int i = 1; i <= k; ++i) {
        if (!party_cities[i].empty() && !processed[i]) {
            ok = false;
            break;
        }
    }

    // Zwracamy wynik zamiast go wypisywać
    return ok;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    if (cin >> t) {
        vector<string> results;
        while (t--) {
            if (solve()) {
                results.push_back("TAK");
            }
            else {
                results.push_back("NIE");
            }
        }
        for (const string& res : results) {
            cout << res << endl;
        }
    }
    return 0;
}