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
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <map>

using namespace std;

#define D(x) 
#define D2(x) 
#define D3(x) 

//#define TEST

#define MAX 1001


struct V {
    V(): wchodziT(0),wchodziN(0), stan_dotrz(0), wynik(0) {}

    int popT() {
        auto it = T.begin();
        int r = *it;
        T.erase(it);
        return r;
    }

    set<int> T;
    set<int> N;
    int wchodziT;
    int wchodziN;

    set<int> dotrzec;
    int stan_dotrz;

    int wynik;
};

V G[MAX];
int n;

void print(int v);


bool oblicz(int v) {
    if(G[v].stan_dotrz == 2) return false;
    if(G[v].stan_dotrz == 1) return true;
    G[v].stan_dotrz=1;
    for(auto v2: G[v].T) {
        if(oblicz(v2)) return true;
        G[v].dotrzec.insert(G[v2].dotrzec.begin(),G[v2].dotrzec.end());
        G[v].dotrzec.insert(v2);
    }
//    G[v].dotrzec.insert(v);
    G[v].stan_dotrz=2;
    return false;
}

multimap<int, int> s;

void dec_wcho_T(int v){
    if(--G[v].wchodziT == 0) s.insert({G[v].T.size(),v});
}
void copy_up(int v, int v1) {
    D(cout << "copy: " ; print(v); cout << "  to: "; print(v1));
    G[v1].N.insert(G[v].N.begin(),G[v].N.end());
    G[v].N.clear();
    for(auto mv: G[v].T) {
        if(G[v1].T.find(mv) == G[v1].T.end()) {
            G[v1].T.insert(mv);
//            G[v1].dotrzec.insert(G[mv].dotrzec.begin(),G[mv].dotrzec.end());
        } else {
            dec_wcho_T(mv);
        }
    }
    G[v].T.clear();

    D(cout << "copy: " ; print(v); cout << "  to: "; print(v1));
}


int wybierz(int v) {
    map<int, int> wyn;
    for(auto w : G[v].T) {
        bool moge = true;
        for(int nv : G[w].N) {
            moge = moge && G[v].dotrzec.find(nv) == G[v].dotrzec.end();
            if(!moge) break;
        }
        for(auto w2 : G[v].T) {
            moge = moge && G[w2].dotrzec.find(w) == G[w2].dotrzec.end();
            if(!moge) break;
        }
        if(moge) {
            wyn.insert({-G[w].wchodziN,w});
        }
    }
    int r = 0;
    if(!wyn.empty()) {
        r = wyn.begin()->second;
    }
    D3(cout << "wybrano:" << r << "\n");
    return r;
}

/*int ktory(int v1, int v2) {
    bool moge1 = G[v2].dotrzec.find(v1) == G[v2].dotrzec.end();
    bool moge2 = G[v1].dotrzec.find(v2) == G[v1].dotrzec.end();

    D3(cout << "_oge1: " << moge1 << " moge2: " << moge2 <<"\n"); 

    moge1 = moge1 && (G[v1].N.find(v2) == G[v1].N.end());
    moge2 = moge2 && (G[v2].N.find(v1) == G[v2].N.end());


    if(!moge1 && !moge2) return 0;
    if(moge1 && moge2) {
    D3(cout << "moge:G " << G[v2].wchodziN << " moge:G " << G[v1].wchodziN <<"\n"); 
        if(G[v2].wchodziN > G[v1].wchodziN)
            moge1 = false;
    }
    D3(cout << "v1: " << v1 << " v2: " << v2 <<"\n"); 
    D3(cout << "moge1: " << moge1 << " moge2: " << moge2 <<"\n"); 
    if(moge1) return 1;
    return 2;
}*/

void print(int v){
    cout << "v:" << v << " wchT:" << G[v].wchodziT << " T: ";
    for(int i: G[v].T) {
        cout << i << " ";
    }
    cout  << " wchN:" << G[v].wchodziN<< " N: ";
    for(int i: G[v].N) {
        cout << i << " ";
    }
    cout << "\n";
}

int main() {
    int a,b,m;
    char c;
    scanf("%d %d",&n,&m);
    for(int i=0;i<m;i++){
        scanf("%d %d %c",&a,&b,&c);
        if(c == 'T') {
            G[a].T.insert(b);
            G[b].wchodziT++;
        } else {
            G[b].wchodziN++;
            G[a].N.insert(b);
        }
    }
    for(int v=1;v<=n;v++) {
        if(oblicz(v)) {
            D(cout << "NIE4\n");
            printf("NIE\n");
            return 0;
        }
    }
    for(int v=1;v<=n;v++) {
        D(print(v));
        if(G[v].wchodziT == 0)
            s.insert({G[v].T.size(), v});
    }

    D2(cout << "!\n");

    while(!s.empty()){
        int v,w;
        auto it = s.begin();
        v = it->second;
        s.erase(it);
        D(print(v));
        int Tsize = G[v].T.size();
        if(G[v].N.find(v) != G[v].N.end()) {
                D(cout << "NIE2\n");
                printf("NIE\n");
                return 0;
        }
        if(Tsize == 0) continue;
        if(Tsize >= 1) {
            w = wybierz(v);
            if(w == 0) {
                D(cout << "NIE1\n");
                printf("NIE\n");
                return 0;
            }
            G[v].T.erase(w);
            D(cout << "wygrano v1: " << w << "\n");
        }
        copy_up(v, w);
        dec_wcho_T(w);
        G[v].wynik = w;
        D(cout << "koniec: " ; print(v));
    }

    D(cout << "-----\n");
    int vk = 0;

    for(int v=1;v<=n;v++) {
        int size = G[v].T.size();
        if(size==1 && G[v].wynik == 0) {
            size = 0;
            G[v].wynik = *G[v].T.begin();
        }
        if(size>=1) {
            D(cout << "NIE4\n");
            printf("NIE\n");
            return 0;
        }
    }


    for(int v=1;v<=n;v++) {
        if(G[v].wynik == 0 && G[v].wchodziN == 0) {
            vk =v;
            break;
        }
    }

    if(vk == 0) {
        D(cout << "NIE3\n");
        printf("NIE\n");
        return 0;
    }

#ifndef TEST
    for(int v=1;v<=n;v++) {
        int w = G[v].wynik;
        if(w == 0 && vk != v) w = vk;
        printf("%d\n",w);
    }
#else
    printf("TAK\n");
#endif
}