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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.LinkedHashSet;

public class reo{
  /*reader template*/
  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(), " \t\n\r\f/");
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }
    
    public int nextInt() {
      return Integer.parseInt(next());
    }
    
  }
  
  public static class Vertex{
    
    /*nodes i like directly*/
    HashSet<Integer> required = new HashSet<>();
    /*nodes that like me directly*/
    HashSet<Integer> requiringMe = new HashSet<>();
    
    
    /*nodes which could be attached, as they are built in tree and require me*/
    HashSet<Integer> toAttach = new HashSet<Integer>();
    
    /*nodes which can not be my supervisor*/
    HashSet<Integer> forbidden = new HashSet<Integer>();
    
    HashSet<Integer> hateMe = new HashSet<Integer>();
    int initialHateMe = 0;
    
    
    public void addForbidden(int id){
      forbidden.add(id);
    }
    public void addRequired(int id){
      required.add(id);
    }
    public void addRequiring(int id){
      requiringMe.add(id);
      toAttach.add(id);
    }
    public void addHatingMe(int id){
      hateMe.add(id);
      initialHateMe ++;
    }
    
  }
  
  public static void printNo(PrintWriter pw){
    pw.print("NIE\n");
    pw.flush();
    pw.close();
  }
  
  static int[] boss = new int[1024];
  static int[] visited = new int[1024];
  static final int DEBUG = 0;
  
  
  
  
  public static void solve(int n, ArrayList<Vertex> nodes, int[] indegs){
    LinkedHashSet<Integer> queue = new LinkedHashSet<Integer>();
    int tmp[] = new int[n+1];
    for(int i=1;i<=n;++i){
      if(indegs[i] == 0){
        queue.add(i);
      }
      tmp[i] = indegs[i];
    }

    int processed = 0;
    while(queue.size()>0){
      int c = queue.iterator().next();
      queue.remove(c);
      
      tmp[c] = -1;
      processed ++;
      HashSet<Integer> neigh = nodes.get(c).required;
      for(int i: neigh){
        if(tmp[i] == 1){
          queue.add(i);
        }
        tmp[i]--;
        nodes.get(i).forbidden.addAll(nodes.get(c).forbidden);
        nodes.get(i).requiringMe.addAll(nodes.get(c).requiringMe);
      }
    }
    PrintWriter pw = new PrintWriter(System.out);
    
    
    if(processed < n){
      if(DEBUG>1){
        pw.println("because of cycle, processed " + processed + ", n= " + n);
      }
      printNo(pw);
      return;
    }
    
    for(int i=1; i<=n;++i){
      if(nodes.get(i).forbidden.contains(i)){
        if(DEBUG>1){
          pw.println("because of hate");
        }
        printNo(pw);
        return;
      }
    }
    
    for(int i=1;i<=n;++i){
      if(indegs[i] == 0){
        queue.add(i);
      }
      tmp[i] = indegs[i];
    }
    
    for(int i=1;i<=n;++i){
      Vertex curr = nodes.get(i);
      for(int h : curr.forbidden){
        nodes.get(h).hateMe.add(i);
      }
      for(int p : curr.requiringMe){
        nodes.get(p).required.add(i);
      }
    }
    
    
    
    
    int uber = 0;
    for(int i=1;i<=n;++i){
      if(nodes.get(i).hateMe.size() == 0 && nodes.get(i).required.size() == 0){
        uber = i;
        break;
      }
    }
    
    if(uber == 0){
      if(DEBUG>1){
        pw.println("unable to select d");
      }
      printNo(pw);
      return;
    } 
    
    for(int i=1;i<=n;++i){
      nodes.get(i).required.remove(uber);
      nodes.get(i).hateMe.remove(uber);
    }
    queue.remove(uber);
    
    
    while(queue.size()>0){
      int c = 0;
      
      for(int cand : queue){
        Vertex potential = nodes.get(cand);
        for(int a : potential.toAttach){
          if(intersectionSize(nodes.get(a).required, potential.forbidden) > 0){
            c = -1;
            break;
          }
        }
        if(c == 0){
          c = cand;
          break;
        } else {
         c = 0;
        }
      }
      if(c == 0){
        if(DEBUG > 1){
          pw.println("candidate not found");
        }
        printNo(pw);
        return;
      }
      /*
      for(int cand : queue){
        if(nodes.get(cand).forbidden.size() == 0){
          c = cand;
          break;
        }
      }
      if(c == 0){
        int time = 1;
        for(int cand : queue){
          if(tms[cand].inTime == 0){
            time += hateDfs(cand,time, queue, nodes);
            break;
          }
        }
        int maxTime = 0;
        for(int cand : queue){
          if(maxTime < tms[cand].inTime){
            maxTime = tms[cand].inTime;
            c = cand;
          }
        }
      }
      
      */
      //System.out.println("" + c  + " has " + nodes.get(c).hateMe.size() + " hate");
      //mark as processed - he gather all hate lists of predecessors and forward them to succesors
      visited[c] = 1;
      if(DEBUG>2){
        System.out.println(queue);
        System.out.println("\ncandidate " + c);
      }
      queue.remove(c);
      
      Vertex curr = nodes.get(c);
      //nodes which are pred intree
      HashSet<Integer> pred = curr.toAttach;
      //nodes which are at my path to root
      
      for(int p : pred){
        boss[p] = c;
      }
      if(DEBUG>2){
        System.out.println("to update: " + curr.required);
        System.out.println("toAttach: " + curr.toAttach);
        System.out.println("forbidden: " + curr.forbidden);
      }
      
      for(int t : curr.forbidden){
        //remove c, replace with succesors of c
        nodes.get(t).hateMe.remove(c);
        nodes.get(t).hateMe.addAll(curr.required);
      }
      
      for(Vertex t : nodes){
        //remove c, replace with succesors of c
        t.forbidden.remove(c);
        
      }
      
      for(int i: curr.toAttach){
        curr.required.addAll(nodes.get(i).required);
      }
      
      for(int t : curr.required){
        if(visited[t] > 0){
          continue;
        }
        if(DEBUG>2){
          System.out.println("node " + t + " before updates ");
          System.out.println("to attach " + nodes.get(t).toAttach);
          System.out.println("forbidden " + nodes.get(t).forbidden);
          System.out.println("requiring " + nodes.get(t).requiringMe);
          
        }

        
        //remove linked to me
        nodes.get(t).toAttach.removeAll(curr.toAttach);
        //add me
        nodes.get(t).toAttach.add(c);
        //update hatelists
        nodes.get(t).forbidden.addAll(curr.forbidden);
        //remove requiring link and replace with me, as i'm supervisor
        
        nodes.get(t).requiringMe.removeAll(curr.toAttach);
        nodes.get(t).requiringMe.remove(c);
        
        if(nodes.get(t).requiringMe.size() == 0){
          queue.add(t);
        }
        if(DEBUG>2){
          System.out.println("node " + t + " after updates ");
          System.out.println("to attach " + nodes.get(t).toAttach);
          System.out.println("forbidden " + nodes.get(t).forbidden);
          System.out.println("requiring " + nodes.get(t).requiringMe);
        }
      }

    }
    
    
    
    
    
    
    
    for(int i=1;i<=n;++i){
      if(boss[i] == 0){
        boss[i] = uber;
      }
    }
    boss[uber] = 0;
    
    if(DEBUG == 0){
      for(int i=1;i<=n;++i){
      
        pw.println(""+boss[i]);
      }
    } else {
      pw.println("TAK");
    }
    
    pw.flush();
    pw.close();
  }
  
  public static class Order{
    int inTime = 0;
    int outTime = 0;
    int back = 0;
    @Override
    public String toString(){
      return "(" + inTime + ", " + outTime+ ")";
    }
  }
  
  public static int dfs(int time, ArrayList<Vertex> nodes, ArrayList<Order> times, int v, int n){
    Order tmp = new Order();
    tmp.inTime = time;
    for(int i=1;i<=n;++i){
      if(boss[i] == v){
        time = dfs(time+1,nodes, times, i, n);
      }
    }
    tmp.outTime = time;
    times.set(v, tmp);
    return time+1;
  }
  
  
  public static int intersectionSize(HashSet<Integer> one, HashSet<Integer> other){
    int ret = 0;
    for(Integer i : one){
      if(other.contains(i)){
        ret++;
      }
    }
    return ret;
  }
  
  public static void main(String args[]){
    InputReader reader = new InputReader(System.in);
    int n = reader.nextInt();
    int m = reader.nextInt();
    
    ArrayList<Vertex> nodes = new ArrayList<Vertex>(n+1);
    int[] indegs = new int[n+1];
    for(int i=0;i<=n;++i){
      nodes.add(new Vertex());
    }
    
    while(m-->0){
      int a = reader.nextInt();
      int b = reader.nextInt();
      String mark = reader.next();
      if(mark.equals("T")){
        nodes.get(a).addRequired(b);
        nodes.get(b).addRequiring(a);
      } else {
        nodes.get(a).addForbidden(b);
        nodes.get(b).addHatingMe(a);
        
      }
    }
    for(int i=1;i<=n;++i){
      indegs[i] = nodes.get(i).requiringMe.size();
    }
    
    solve(n, nodes, indegs);
  }
  
}
//