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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class reo {
    BufferedReader rd;
    int n;

    reo() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        int[] q = intarr();
        n = q[0];
        int m = q[1];
        int[][] a = new int[n][n];
        for(int i=0;i<m;i++) {
            String[] h = split(rd.readLine());
            int u = pint(h[0])-1;
            int v = pint(h[1])-1;
            if(h[2].charAt(0) == 'T') {
                a[u][v] = 1;
            } else {
                a[u][v] = -1;
            }
        }
        int[] res = solve(a);
        if(res == null) {
            out("NIE");
        } else {
            StringBuilder buf = new StringBuilder();
            for(int i=0;i<res.length;i++) {
                if(i > 0) {
                    buf.append('\n');
                }
                buf.append(res[i]+1);
            }
            out(buf);
        }
    }

    private int[] solve(int[][] a) {
        int dir = -1;
        for(int i=0;i<n;i++) {
            boolean ok = true;
            for(int j=0;j<n;j++) {
                if(a[j][i] == -1) {
                    ok = false;
                    break;
                }
                if(a[i][j] == 1) {
                    ok = false;
                    break;
                }
            }
            if(ok) {
                dir = i;
                break;
            }
        }
        if(dir == -1) {
            return null;
        }
        for(int i=0;i<n;i++) if(i != dir) {
            a[i][dir] = 1;
        }

        List<List<Integer>> e = toEdges(a,1);
        if(dfs(e) == null) {
            return null;
        }

        List<List<Integer>> e2 = toEdges(a,-1);
        boolean[][] exists = new boolean[n][n];
        for(int i=0;i<n;i++) {
            Queue<Integer> q = new ArrayDeque<>();
            q.add(i);
            boolean[] vis = new boolean[n];
            vis[i] = true;
            while(!q.isEmpty()) {
                int v = q.poll();
                for(Integer c: e2.get(v)) {
                    if(!vis[c]) {
                        vis[c] = true;
                        q.add(c);
                        exists[i][c] = true;
                    }
                }
            }
        }
        final int INF = 1234567890;
        List<List<Integer>> e3 = new ArrayList<>();
        for(int i=0;i<n;i++) {
            e3.add(new ArrayList<>());
        }
        int[] boss = new int[n];
        Arrays.fill(boss, -1);
        for(int k=0;k<2;k++) {
            List<List<Integer>> ek = k==0?e:e3;
            for (int i = 0; i < n; i++) {
                Queue<Integer> q = new ArrayDeque<>();
                q.add(i);
                int[] dist = new int[n];
                Arrays.fill(dist, INF);
                dist[i] = 0;
                while (!q.isEmpty()) {
                    int v = q.poll();
                    for (Integer c : ek.get(v)) {
                        dist[c] = dist[v] + 1;
                        q.add(c);
                    }
                }
                for (int j = 0; j < n; j++) {
                    if (dist[j] == 1) {
                        if(k == 0) {
                            e3.get(i).add(j);
                        } else {
                            boss[i] = j;
                        }
                    }
                }
            }
        }

        for(int i=0;i<n;i++) {
            if(boss[i] == -1 && i != dir) {
                boss[i] = dir;
            }
        }
        if(boss[dir] != -1) {
            return null;
        }
        for(int i=0;i<n;i++) {
            for(int j=0;j<n;j++) {
                if(a[i][j] == -1) {
                    int k = boss[i];
                    while (k != -1) {
                        if (k == j) {
                            return null;
                        }
                        k = boss[k];
                    }
                }
                if(a[i][j] == 1) {
                    int k = boss[i];
                    while (k != j) {
                        if (k == -1) {
                            return null;
                        }
                        k = boss[k];
                    }
                }
            }
        }
        return boss;
    }

    private List<List<Integer>> toEdges(int[][] a, int x) {
        List<List<Integer>> e = new ArrayList<>();
        for(int i=0;i<n;i++) {
            e.add(new ArrayList<>());
        }
        for(int i=0;i<n;i++) {
            for(int j=0;j<n;j++) {
                if(a[i][j] == x) {
                    e.get(i).add(j);
                }
            }
        }
        return e;
    }

    private int[] dfs(List<List<Integer>> e) {
        Deque<int[]> d = new ArrayDeque<>();
        int[] vis = new int[n];
        int[] tm = new int[n];
        int t = 0;
        for(int i=0;i<n;i++) {
            if(vis[i] == 0) {
                vis[i] = 1;
                d.addFirst(new int[] { i, 0 });
                while(!d.isEmpty()) {
                    t++;
                    int[] v = d.peekFirst();
                    List<Integer> ed = e.get(v[0]);
                    int limit = ed.size();
                    while(v[1] < limit) {
                        Integer u = ed.get(v[1]);
                        if (vis[u] == 0) {
                            vis[u] = 1;
                            d.addFirst(new int[]{u, 0});
                            break;
                        } else if (vis[u] == 1) {
                            return null;
                        }
                        v[1]++;
                    }
                    if(v[1] == limit) {
                        d.pollFirst();
                        tm[v[0]] = t;
                        vis[v[0]] = 2;
                    }
                }
            }
        }
        return tm;
    }

    private int pint(String s) {
        return Integer.parseInt(s);
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = split(s);
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private String[] split(String s) {
        if(s == null) {
            return new String[0];
        }
        int n = s.length();
        int start = -1;
        int end = 0;
        int sp = 0;
        boolean lastWhitespace = true;
        for(int i=0;i<n;i++) {
            char c = s.charAt(i);
            if(isWhitespace(c)) {
                lastWhitespace = true;
            } else {
                if(lastWhitespace) {
                    sp++;
                }
                if(start == -1) {
                    start = i;
                }
                end = i;
                lastWhitespace = false;
            }
        }
        if(start == -1) {
            return new String[0];
        }
        String[] res = new String[sp];
        int last = start;
        int x = 0;
        lastWhitespace = true;
        for(int i=start;i<=end;i++) {
            char c = s.charAt(i);
            boolean w = isWhitespace(c);
            if(w && !lastWhitespace) {
                res[x++] = s.substring(last,i);
            } else if(!w && lastWhitespace) {
                last = i;
            }
            lastWhitespace = w;
        }
        res[x] = s.substring(last,end+1);
        return res;
    }

    private boolean isWhitespace(char c) {
        return c==' ' || c=='\t';
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new reo();
    }
}