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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Set;
import java.io.IOException;
import java.util.Deque;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.LinkedList;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class reo {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Reorganizacja solver = new Reorganizacja();
        solver.solve(1, in, out);
        out.close();
    }

    static class Reorganizacja {
        int N;
        int M;
        Reorganizacja.Node[] nodes;
        boolean[][] cannot;
        boolean hasOrder;
        boolean[] marked;
        boolean[] onStack;
        Deque<Integer> top;
        Deque<Integer> leaves;
        boolean possible;
        int[] parent;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            init(in);
            calculateTopologicalOrder();
            if (!hasOrder) {
                out.println("NIE");
                return;
            }
//        printOrder();

            filterOrder();
            sortOrder();

//        printOrder();

            fillReports();

            if (invalidOrder()) {
                out.println("NIE");
                return;
            }

            Reorganizacja.Node root = findRoot();
            if (root == null) {
                out.println("NIE");
                return;
            }

            parent[root.vertex] = 0;

            generateTree(root);

            if (possible) {
//            out.println("TAK");
                printAnswer(out);
            } else {
                out.println("NIE");
            }
        }

        private void fillReports() {
            marked = new boolean[N + 1];
            for (int v = 1; v <= N; v++) {
                if (!marked[v]) fill(v);
            }
        }

        private Set<Integer> fill(int v) {
            if (marked[v]) return nodes[v].ancescors;

            marked[v] = true;

            nodes[v].ancescors.addAll(nodes[v].reportsTo);

            for (int w : nodes[v].reportsTo) {
                nodes[v].ancescors.addAll(fill(w));
            }

            return nodes[v].ancescors;
        }

        private boolean invalidOrder() {
            for (int v = 1; v <= N; v++) {
                if (!Collections.disjoint(nodes[v].cannotBeChildOf, nodes[v].ancescors)) {
                    return true;
                }
            }

            return false;
        }

        private void sortOrder() {
            int cnt = 0;
            int tmp[] = new int[top.size()];
            for (int x : top) {
                tmp[cnt++] = x;
            }

            boolean flag = true;
            int limit = 100;
            while (flag && limit-- > 0) {
                flag = false;
                for (int i = 0; i < tmp.length; i++) {
                    for (int j = i + 1; j < tmp.length; j++) {
                        Reorganizacja.Node x = nodes[tmp[j]];
                        Reorganizacja.Node y = nodes[tmp[i]];

                        if (cannot[x.vertex][y.vertex] && cannot[y.vertex][x.vertex]) continue;

                        if (cannot[x.vertex][y.vertex]) {
                            int t = tmp[i];
                            tmp[i] = tmp[j];
                            tmp[j] = t;
                            flag = true;
                        }
                    }
                }
            }

//        for (int i = 0; i < tmp.length; i++) {
//            for (int j = i - 1; j >= 0; j--) {
//                Node x = nodes[tmp[j + 1]];
//                Node y = nodes[tmp[j]];
//
//                if (cannot[x.vertex][y.vertex] && cannot[y.vertex][x.vertex]) continue;
//
//                if (cannot[x.vertex][y.vertex]) {
//                    int t = tmp[j + 1];
//                    tmp[j + 1] = tmp[j];
//                    tmp[j] = t;
//                }
//            }
//        }

            Deque<Integer> newTop = new LinkedList<>();
            for (int x : tmp) {
                newTop.addLast(x);
            }
            top = newTop;
        }

        private void filterOrder() {
            Deque<Integer> newTop = new LinkedList<>();

            while (!top.isEmpty()) {
                int node = top.pollFirst();
                if (!nodes[node].reportsTo.isEmpty() || !nodes[node].subordinates.isEmpty()) {
                    newTop.addLast(node);
                } else {
                    leaves.add(node);
                }
            }

            top = newTop;
        }

        private void generateTree(Reorganizacja.Node root) {
            marked = new boolean[N + 1];
            onStack = new boolean[N + 1];
            marked[root.vertex] = true;
            onStack[root.vertex] = true;
            while (!top.isEmpty()) {
                int nodeId = top.pollFirst();
                if (marked[nodeId]) continue;

                generate(nodes[nodeId], root.vertex);
            }

            while (!leaves.isEmpty()) {
                int leaf = leaves.pollFirst();
                parent[leaf] = root.vertex;
                if (cannotBe(nodes[leaf])) {
                    possible = false;
                }
            }
            onStack[root.vertex] = false;
        }

        private void generate(Reorganizacja.Node node, int parentId) {
            marked[node.vertex] = true;
            onStack[node.vertex] = true;
            parent[node.vertex] = parentId;

            if (cannotBe(node)) {
                possible = false;
                return;
            }

            if (node.subordinates.isEmpty()) {
                onStack[node.vertex] = false;
                return;
            }

            while (!top.isEmpty()) {
                int nodeId = top.pollFirst();
                if (marked[nodeId]) continue;
                if (nodes[nodeId].reportsTo.contains(node.vertex)) {
                    generate(nodes[nodeId], node.vertex);
                } else {
                    generate(nodes[nodeId], parentId);
                }

            }

            onStack[node.vertex] = false;
        }

        private boolean cannotBe(final Reorganizacja.Node node) {
            for (int v = 1; v <= N; v++) {
                if (cannot[node.vertex][v] && onStack[v]) {
                    return true;
                }
            }
            return false;
        }

        private void printAnswer(final PrintWriter out) {
            for (int v = 1; v <= N; v++) {
                out.println(parent[v]);
            }
        }

        private Reorganizacja.Node findRoot() {
            for (int v = 1; v <= N; v++) {
                if (nodes[v].reportsTo.isEmpty() && nodes[v].cannotBeParentOf.isEmpty()) {
                    return nodes[v];
                }
            }
            return null;
        }

        private void calculateTopologicalOrder() {
            for (int v = 1; v <= N; v++) {
                if (!marked[v]) {
                    dfs_top(v);
                }
            }
        }

        private void dfs_top(final int v) {
            marked[v] = true;
            onStack[v] = true;

            for (int w : nodes[v].reportsTo) {
                if (!marked[w]) {
                    dfs_top(w);
                } else if (onStack[w]) {
                    hasOrder = false;
                }
            }
            top.addLast(v);
            onStack[v] = false;
        }

        private void init(final InputReader in) {
            N = in.nextInt();
            M = in.nextInt();
            nodes = new Reorganizacja.Node[N + 1];
            for (int i = 1; i <= N; i++) {
                nodes[i] = new Reorganizacja.Node(i);
            }

            cannot = new boolean[N + 1][N + 1];
            hasOrder = true;
            possible = true;
            top = new LinkedList<>();
            leaves = new LinkedList<>();
            marked = new boolean[N + 1];
            onStack = new boolean[N + 1];
            parent = new int[N + 1];

            for (int i = 0; i < M; i++) {
                int v = in.nextInt();
                int parent = in.nextInt();
                char relation = in.next().charAt(0);

                if (relation == 'T') {
                    nodes[v].reportsTo.add(parent);
                    nodes[parent].subordinates.add(v);
                } else {
                    cannot[v][parent] = true;
                    nodes[parent].cannotBeParentOf.add(v);
                    nodes[v].cannotBeChildOf.add(parent);
                }
            }
        }

        private static class Node {
            private int vertex;
            private Set<Integer> reportsTo = new TreeSet<>();
            private Set<Integer> ancescors = new TreeSet<>();
            private Set<Integer> subordinates = new TreeSet<>();
            private Set<Integer> cannotBeParentOf = new TreeSet<>();
            private Set<Integer> cannotBeChildOf = new TreeSet<>();

            public Node(final int vertex) {
                this.vertex = vertex;
            }

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}