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

using namespace std;

vector<vector<int>> v;
vector<char> odw, nie;

void dfs(int w){

odw[w] = 1;

for(int sasiad : v[w]){
    if(!odw[sasiad] && !nie[sasiad]){
        dfs(sasiad);
    }
}

}

signed main(){

cin.tie(0) -> sync_with_stdio(0);

int t;
cin>>t;

while(t--){
    int n,m,k;
    cin>>n>>m>>k;

    v.resize(n+1);
    vector<vector<int>> jakie(k+1);
    vector<int> v2(n+1);

    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        v2[i] = x;

        jakie[x].push_back(i);
    }

    for(int i=0;i<m;i++){
        int a,b;
        cin>>a>>b;

        v[a].push_back(b);
        v[b].push_back(a);
    }

    odw.resize(n+1, 0);
    nie.resize(n+1, 1);
    bool tok2 = true;
    int ile = 0;

    while(ile <= n){
        bool tok3 = false;
        for(int i=1;i<=k;i++){
            if(jakie[i].empty()){
                continue;
            }
            for(int x : jakie[i]){
                nie[x] = 0;
            }
            dfs(jakie[i][0]);
            bool tok = true;

            for(int x : jakie[i]){
                if(!odw[x]){
                    tok = false;
                }
                odw[x] = 0;
                nie[x] = 1;
            }
            if(tok){
                tok3 = true;
                for(int x : jakie[i]){
                    nie[x] = 0;
                }
                ile += jakie[i].size();
                break;
            }
        }
        if(!tok3){
            tok2 = false;
            break;
        }
    }

    cout<<(tok2 ? "TAK\n" : "NIE\n");

    v.clear();
    odw.clear();
    nie.clear();
}

}