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

using namespace std;

#define st first
#define nd second
#define pb push_back
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define irep(i,a,b) for(int i = a; i >= b; i--)
typedef long long ll;
typedef long double ld;
//typedef __int128 int128;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pi; 
typedef pair<double,double> pd;
typedef pair<ll,ll> pl;

const int max_n = 1e5+7;

vi g[max_n];

//dla kolorow
vi p[max_n];
int ind[max_n];
int ile_s[max_n];

//dla dsu!
int siz[max_n];
int rep[max_n];
set<pi> s[max_n]; //kolor, ind! dla dsu
bool odw[max_n]; //chyba niepotrzebne

queue<int> q; //dodajemy rep mniejszego!

int fin(int a){
    //okej jak tutaj jest 0 to sie psuje
    if(rep[a] == a) return a;
    return rep[a] = fin(rep[a]);
}

void uni(int a, int b, int c){
    //albo uni tych samych kolorkow albo roznych!!!
    // cerr << "\nuni, a: " << a << " b: " << b << " c: " << c << '\n';
    a = fin(a); b = fin(b);
    //cerr << "fin(a): " << a << " fin(b): " << b << '\n';
    if(a == b) return;
    if(siz[a] < siz[b]) swap(a,b);
    siz[a] += siz[b];

    //cerr << "po rep i swapie, a: " << a << " b: " << b << '\n';
    //cerr << "ind[a]: " << ind[a] << " ind[b]: " << ind[b] << '\n';

    for(auto x:s[b]){
        //do tamtego tez dodac i usunac stare!
        //cerr << "przechodzimy po grafie b, x.st: " << x.st << " x.nd: " << x.nd << '\n';
        int r = fin(x.nd);
        //aha jesli to a albo b to skip imo!
        if(r == a || r == b) continue;
        //cerr << "r: " << r << '\n';
        //przeciez nie usuwam nic z b xDDDD
        s[r].erase({ind[b],b});
        //if(r == 6) cerr << "\n\nUSUWAMY Z 6\n\n";
        //cerr << "erase r\n";
        s[r].insert({ind[a],a});
        //cerr << "insert r\n";
        s[a].insert({ind[r],r});
        //cerr << "insert a\n";
    }
    //cerr << "koniec przechodzenia\n";
    //o wlasnie, ktore teraz push???
    //imo ma sens!!!
    rep[b] = a;
    if(ind[a] == ind[b]){
        //cerr << "usuwam ind\n";
        ile_s[ind[a]]--;
        if(ile_s[ind[a]] == 1){
            //cerr << "ile_s == 1\n";
            if(c != 0) uni(c,a,0); //najpierw chcemy z jedynka??
            q.push(a); //dla bezpieczenstwa wrzucam caly kolor!!!
        }
    }
    else
        q.push(b);
}

int main(){

    //ZMIENIC!!!
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t; cin >> t;
    while(t--){

        //cerr << "\n\n\nNOWY TEST\n\n\n";

        int n,m,k;
        cin >> n >> m >> k;
        rep(i,1,n){
            cin >> ind[i];
            p[ind[i]].pb(i);
            rep[i] = i;
            ile_s[ind[i]]++;
            siz[i] = 1;
            //cerr << "i: " << i << " ind: " << ind[i] << '\n';
        }

        rep(i,1,m){
            int a,b; cin >> a >> b;
            g[a].pb(b); g[b].pb(a);
            s[a].insert({ind[b],b});
            s[b].insert({ind[a],a});
        }

        //cerr << "wczytane\n";

        //japierdole znowu zle bo za duzo krawedzi dodaje!!!
        rep(i,1,n){
            for(auto x:g[i])
                if(ind[i] == ind[x]) uni(i,x,0);
            if(p[ind[i]].size() == 1) q.push(i);
        }

        //cerr << "dodane na kolejke\n";
        
        //mozliwe ze nie potrzebujemy!!!
        /* rep(i,1,n)
            for(auto x:g[i])
                s[fin(i)].insert({ind[fin(x)],fin(x)}); */

        /* cerr << "POCZATKOWY GRAF 2\n";
        rep(i,1,n){
            cerr << "\ni: " << i << " fin: " << fin(i) << '\n';
            for(auto x:s[fin(i)]) cerr << "    c: " << x.st << " num: " << x.nd << '\n';
        } */

        int tim = 0;
        pair<int,int> deb = {5,1};
        while(!q.empty()){
            //teraz funkcja add!!!
            //dostaje kolor i wczesniejszy merge ktory chce zrobic!
            //USUNAC!!!
            //if(tim++ > 15) break; //debug
            int v = q.front(); q.pop();
            int c_v = ind[v];
            int r_v = fin(v); //bez fin!!!
            //cerr << "\nv: " << v << " c_v: " << c_v << " r_v: " << r_v << '\n';
            set<pi> pom = s[v]; //tutaj wazne ze nie r_v!!!
            for(auto pa:pom){
                int x = pa.nd;
                int r_x = fin(x);
                int c_x = ind[r_x];
                
                //cerr << "   x: " << x << " c_x: " << c_x << " r_x: " << r_x << '\n';
                //aha jak chce polaczyc z jedna spojna to tez?
                if(ile_s[c_x] == 1 && r_x != r_v){
                    //cerr << "       ile_s == 1\n";
                    uni(r_v,r_x,0);
                    continue;
                }
                
                while(1){
                    auto it = s[r_v].lower_bound({c_x,0});
                    if(it == s[r_v].end()) break;
                    //cerr << "       it{st,nd}: " << (*it).st << ' ' << (*it).nd << '\n';
                    if((*it).st != c_x) break;
                    int c_it = (*it).st; int r_it = fin((*it).nd);
                    s[r_v].erase(it);
                    if(r_it == r_x){
                        //ten sam rep, chcemy usunac!!!
                        /* if(r_v == 6 && (*it).st == 5 && (*it).nd == 1){
                            cerr << "\n\nUSUWAMY Z 6\n";
                            cerr << "v: " << v << " r_v: " << r_v << " x: " << x
                                 << " r_x: " << r_x << " c_x: " << c_x << '\n';
                            cerr << "it.st: " << (*it).st << " nd: " << (*it).nd << " rep: " << fin((*it).nd) << '\n';
                        } */
                        continue;
                    }
                    //mozemy zrobic union tych dwoch kolorow
                    uni(r_x,r_it,r_v); //no wlasnie ale robie unie!!!
                    /* if(r_v == 6 && (*it).st == 5 && (*it).nd == 1){
                        cerr << "\n\nUSUWAMY Z 6\n";
                        cerr << "v: " << v << " r_v: " << r_v << " x: " << x
                                 << " r_x: " << r_x << " c_x: " << c_x << '\n';
                        cerr << "it.st: " << (*it).st << " nd: " << (*it).nd << " rep: " << fin((*it).nd) << '\n';
                    } */
                    //s[r_v].erase(*it); //nie chce miec podwojnych!!
                }  
                //DO ROZPATRZENIA!!!
                if(r_x != r_v) s[r_v].insert({c_x,r_x}); //czy chce dodac na koniec?? to do ro
            }
        }

        bool ans = 1;
        //imo bedzie okej!
        rep(i,1,k){
            //cerr << "i: " << i << " ile_s: " << ile_s[i] << '\n';
            if(ile_s[i] > 1) ans = 0;
        }

        if(ans) cout << "TAK\n";
        else cout << "NIE\n";

        rep(i,1,n){
            g[i].clear();
            ind[i] = 0;
            s[i].clear();
        }
        rep(i,1,k){
            p[i].clear();
            ile_s[i] = 0;
        }

    }
    

    return 0;
}