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
#include <bits/stdc++.h>

using namespace std;


//#define DEBUG

#define ll long long

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)

#ifdef DEBUG
#define debug(a) cout << a << endl;
#else
#define debug(a) ;
#endif

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;

typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;

typedef pair<int,int> ii;

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

const int MOD = 998244353;

ll read(){
    ll i;
    cin>>i;
    return i;
}

vi readvi(int n,int off=0,int shift=0){
    vi v(n+shift);
    rep(i,shift)v[i]=0;
    rep(i,n)v[i+shift]=read()+off;
    return v;
}

void YesNo(bool condition, bool do_exit=false) {
    if (condition)
        cout << "TAK" << endl;
    else
        cout << "NIE" << endl;
    if (do_exit)
        exit(0);
}

// find root and make root as parent of i (path compression)
int find_set(vi & parent, int i)
{
    if (parent[i] >= 0) {
        parent[i] = find_set(parent, parent[i]);
        return parent[i];
    }
    else
        return i;
}

// (uses union by rank; rank = (-1) * parent[root])
void union_set(vi & parent, int i, int j)
{
    int root_i = find_set(parent, i);
    int root_j = find_set(parent, j);

    if (root_i == root_j)
        return;

    // (Union by Rank)
    if (-parent[root_i] < -parent[root_j])
        parent[root_i] = root_j;
    else if (-parent[root_j] < -parent[root_i])
        parent[root_j] = root_i;
    else {
        parent[root_j] = root_i;
        parent[root_i] -= 1;
    }
}


void dfs(int v, int num_comp, vi & a, vector<bool> & visited, vvi & neigh, vvi & comp_neigh_vtcs, vi & comp) {
    visited[v] = true;
    comp[v] = num_comp;
    for (auto w : neigh[v])
        if (a[w] != a[v])
            comp_neigh_vtcs[num_comp].push_back(w);
        else
            if (!visited[w])
                dfs(w, num_comp, a , visited, neigh, comp_neigh_vtcs, comp);

}

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

    int t;
    cin >> t;
    rep(_, t) {
        int n,m,k;
        cin >> n >> m >> k;
        vi a = readvi(n, -1);

        vvi neigh(n);


        rep(i,m) {
            int x,y;
            cin >> x >> y;
            --x; --y;
            neigh[x].push_back(y);
            neigh[y].push_back(x);
        }

#ifdef  DEBUG
        for (int v : a)
            cout << v << " ";
        cout << endl;
        rep(i,n) {
            cout << i << ":";
            for (int v : neigh[i])
                cout << v << " ";
            cout << endl;
        }

#endif

        vi parent_fu(n, -1); //find-union

        vector<set<int>> comp_neigh(n); // sasiedzi skladowych
        vvi comp_neigh_vtcs(n); // sasiednie wierzchołki skladowych, na początku.
        vi comp(n);
        vi color(n); // kolory składowych
        vi num_comp_of_color(k); // liczba składowych danego koloru
        vi comp_of_color(k); // gdy num_comp_of_color(k) == 1 tu jest nr składowej
        vector<bool> visited(n, false);
        vector<bool> solved(k, false);

        visited.resize(n, false);

        queue<int> connected_colors; //kolejka kolorów spójnych

        int num_comp = 0;
        rep(v,n)
            if (!visited[v]) {
                ++num_comp;
                color[num_comp-1] = a[v];
                num_comp_of_color[a[v]] += 1;
                comp_of_color[a[v]] = num_comp-1;
                dfs(v, num_comp-1, a, visited, neigh, comp_neigh_vtcs, comp);
            }

        int solved_colors = 0;

        rep(i,k) {
            if (num_comp_of_color[i] == 0) {
                solved[i] = true;
                solved_colors += 1;
            }
            if (num_comp_of_color[i] == 1)
                connected_colors.push(comp_of_color[i]);
        }

        debug(num_comp);

        rep(i, num_comp)
            for (int v : comp_neigh_vtcs[i])
                comp_neigh[i].insert(comp[v]);

        while (not connected_colors.empty()) {
            int c = connected_colors.front();
            //assert(parent_fu[c] < 0);
            connected_colors.pop();
#ifdef DEBUG
            cout << "SZ" << comp_neigh[c].size() << endl;
#endif

            set<int> comp_neigh_to_add;
            vi to_remove;
            for (int d : comp_neigh[c])
                if (solved[color[d]] and find_set(parent_fu, d) != find_set(parent_fu, c)) {
                    to_remove.push_back(d);
                    int dd = find_set(parent_fu, d);
                    if (comp_neigh_to_add.size() < comp_neigh[dd].size())
                        swap(comp_neigh_to_add, comp_neigh[dd]);
                    for (auto x : comp_neigh[dd]) {
                        int xx = find_set(parent_fu, x);
                        if (xx != c)
                            comp_neigh_to_add.insert(xx);
                    }
                    parent_fu[dd] = c;
                }

            for (int d : to_remove)
                comp_neigh[c].erase(d);

            if (comp_neigh_to_add.size() > comp_neigh[c].size())
                swap(comp_neigh_to_add, comp_neigh[c]);
            for (auto x : comp_neigh_to_add)
                comp_neigh[c].insert(find_set(parent_fu, x));

            // tutaj c skleiło się ze wszystkimi sąsiednimi rozwiązanymi kolorami.
            // c nie ma już sąsiednich rozwiązanych kolorów
            // teraz sklejamy sąsiednie składowe tych samych kolorów.

            map<int,int> neigh_colors;

            for (int d : comp_neigh[c]) {
                if (neigh_colors.find(color[d]) != neigh_colors.end()) {
                    int x = find_set(parent_fu, neigh_colors[color[d]]);
                    int y = find_set(parent_fu, d);
                    if (x != y) {
                        assert(!solved[color[x]]);
                        assert(!solved[color[y]]);
                        union_set(parent_fu, x, y);
                        int z = find_set(parent_fu, x);
                        if (z == y)
                            z = x;

                        // łączymy z oraz y.
                        if (comp_neigh[z].size() < comp_neigh[y].size())
                            swap(comp_neigh[z], comp_neigh[y]);
                        for (auto w : comp_neigh[y])
                            comp_neigh[z].insert(find_set(parent_fu, w));

                        --num_comp_of_color[color[z]];
                        if (num_comp_of_color[color[z]] == 1) {
                            connected_colors.push(z);
                        }

                    }
                }
                else {
                    neigh_colors[color[d]] = d;
                }
            }

            // porządkujemy sąsiadów, eliminujemy sklejonych, aktualne identyfikatory
            comp_neigh[c].clear();
            for (auto [col, x] : neigh_colors)
                comp_neigh[c].insert(find_set(parent_fu, x));

            solved[color[c]] = true;
            solved_colors += 1;
        }

        YesNo(solved_colors == k);
    }


    return 0;
}