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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define FORE(i, t) for(__typeof(t.begin())i=t.begin();i!=t.end();++i)
#define SZ(x) int((x).size())
#define REP(i, n) for(int i=0,_=(n);i<_;++i)
#define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i)
#define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1e9 + 9;
const int MX = 1003;

struct Vertex {
    int x, in_yes_left, in_no_left, out_no_left, out_yes_left;

    bool operator < (const Vertex &other) const {
        return make_pair(pii(-in_yes_left, in_no_left), pii(-out_no_left, out_yes_left))
             < make_pair(pii(-other.in_yes_left, other.in_no_left), pii(-other.out_no_left, other.out_yes_left));
    }
};

vi out_yes[MX];
set<int> out_yes_inherited[MX];
vi out_no[MX];
set<int> out_no_inherited[MX];
vi in_no[MX];
int in_yes_count[MX];
int in_no_count[MX];
int out_yes_count[MX];
int out_no_count[MX];
int parent[MX];
int ordered[MX];
int ordered_rev[MX];
bool visited[MX];
int update_time[MX];

Vertex create_vertex(int x) {
    return (Vertex){x, in_yes_count[x], in_no_count[x], out_no_count[x], out_yes_count[x]};
}

ull adj[1003][19] = {};

void add_edge(int a, int b) {
    adj[a][b / 64] |= ((ull)1 << (ull)(b % 64));
}

bool has_edge(int a, int b) {
    return (adj[a][b / 64] & ((ull)1 << (ull)(b % 64))) > 0;
}

void transitive_closure(int n) {
    FOR (k, 1, n) {
//        cout << "k: " << k << "\n";
        FOR (i, 1, n) {
//            cout << "  i: " << i << "\n";
            FOR (j, 0, (n / 64) + 1) {
//                cout << "    j: " << j << "\n";
//                cout << "      " << bitset<7>(adj[i][j]) << " " << bitset<7>(adj[k][j]) << " " << bitset<7>(adj[i][k / 64]) << "\n";
                if (has_edge(i, k)) {
//                    adj[i][j] |= adj[k][j] | adj[i][k / 64];
                    adj[i][j] |= adj[k][j];
                }

            }
        }
    }
}

void add_yes(int a, int b) {
    out_yes[a].PB(b);
    ++out_yes_count[a];
    ++in_yes_count[b];
    add_edge(a, b);
}

vector<pii> no_edges;

void add_no(int a, int b) {
    out_no[a].PB(b);
    in_no[b].PB(a);
    ++out_no_count[a];
    ++in_no_count[b];
    no_edges.PB(pii(a, b));
}

void inline one() {
    int n, m;
    cin >> n >> m;
    FOR (i, 1, n) {
        in_yes_count[i] = 0;
        in_no_count[i] = 0;
        out_no_count[i] = 0;
        visited[i] = false;
        update_time[i] = -1;
        parent[i] = -1;
    }

    REP (i, m) {
        int a, b;
        string c;
        cin >> a >> b >> c;
        if (c[0] == 'T') {
            add_yes(a, b);
        } else {
            add_no(a, b);
        }
    }
//    cout << "adj\n";
//    FOR (i, 1, n) {
//        cout << bitset<7>(adj[i][0]) << "\n";
//    }
    transitive_closure(n);
//    FOR (i, 1, n) {
//        FOR (j, 1, n) {
//            cout << (has_edge(i, j) ? 1 : 0) << " ";
//        }
//        cout << "\n";
//    }

    FORE (edge, no_edges) {
        int a = edge->first;
        int b = edge->second;
        FOR (from, 1, n) {
            if (has_edge(from, a) && has_edge(from, b)) {
//                cout << "add_yes from closure: " << a << ", " << b << "\n";
                add_yes(b, a);
            }
        }
    }

    priority_queue<Vertex> q;
    FOR (i, 1, n) {
        q.push(create_vertex(i));
    }

    int current_position_in_order = 0;
    while (!q.empty()) {
        Vertex top = q.top();
        q.pop();
        int x = top.x;
        if (visited[x]) {
            continue;
        }
        if (top.in_yes_left != 0) {
            cout << "NIE\n";
            return;
        }
        ordered[current_position_in_order] = x;
        ordered_rev[x] = current_position_in_order;

        FORE (yt, out_yes[x]) {
            int y = *yt;
            --in_yes_count[y];
        }
        FORE (yt, in_no[x]) {
            int y = *yt;
            --out_no_count[y];
        }


        FORE (yt, out_yes[x]) {
            int y = *yt;
            if (!visited[y]) {
                q.push(create_vertex(y));
                update_time[y] = current_position_in_order;
            }
        }
        FORE (yt, in_no[x]) {
            int y = *yt;
            if (!visited[y] && update_time[y] < current_position_in_order) {
                q.push(create_vertex(y));
            }
        }

        visited[x] = true;
        ++current_position_in_order;
    }
//    cout << "order:\n";
//    REP(i, n) {
//        cout << "i: " << i << " " << ordered[i] << "\n";
//    }

    REP (current_position_in_order, n) {
        int x = ordered[current_position_in_order];
//        cout << "x: " << x << "\n";
        int lowest_position_of_possible_parents = MX;
        int best_parent = -1;
        FORE (yt, out_yes[x]) {
            int y = *yt;
            assert(ordered_rev[y] > current_position_in_order);
            out_yes_inherited[x].insert(y);
        }
        FORE (yt, out_yes_inherited[x]) {
            int y = *yt;
//            cout << "  out_yes_inherited: " << y << "\n";
            // TODO: Moze nie zawsze wybierac najnizszego, tylko najnizszego, do ktorego nie mamy out_no?
            if (ordered_rev[y] < lowest_position_of_possible_parents) {
                lowest_position_of_possible_parents = ordered_rev[y];
                best_parent = y;
            }
        }
//        cout << "best_parent: " << best_parent << "\n";
        if (best_parent == -1) {
            continue;
        }
        parent[x] = best_parent;
        // Juz wyznaczony parent. Teraz sprawdzamy czy out_no nie koliduja.
        FORE (yt, out_no[x]) {
            int y = *yt;
//            cout << "  out_no: " << y << "\n";
            if (ordered_rev[y] > current_position_in_order) {
                out_no_inherited[x].insert(y);
            }
        }
        FORE (yt, out_no_inherited[x]) {
            int y = *yt;
//            cout << "  out_no_inherited: " << y << "\n";
            if (y == parent[x]) {
                cout << "NIE\n";
                return;
            }
        }
//        cout << "out_no nie koluduja\n";
        // Ten wierzcholek byl ok. Teraz propagujemy do parenta krawedzie out_yes i out_no:
        FORE (yt, out_yes_inherited[x]) {
            int y = *yt;
            if (parent[x] != y) {
                out_yes_inherited[parent[x]].insert(y);
//                cout << "  propagating out_yes to " << y << " to parent=" << parent[x] << "\n";
            }
        }
        FORE (yt, out_no_inherited[x]) {
            int y = *yt;
            if (parent[x] != y) {
                out_no_inherited[parent[x]].insert(y);
//                cout << "  propagating out_no to " << y << " to parent=" << parent[x] << "\n";
            }
        }
    }
//    FOR (i, 1, n) {
//        cout << "i: " << i << " parent: " << parent[i] << "\n";
//    }
    int root = -1;
    FOR (i, 1, n) {
        if (parent[i] == -1 && in_no_count[i] == 0) {
            root = i;
            break;
        }
    }
    if (root == -1) {
        cout << "NIE\n";
        return;
    }


//    cout << "TAK\n"; return;


    FOR (i, 1, n) {
        if (parent[i] == -1) {
            parent[i] = root;
        }
    }
    parent[root] = 0;
    FOR (i, 1, n) {
        cout << parent[i] << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    //int z; cin >> z; while(z--)
    one();
}