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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
    
unordered_set<int> ancestors[1001];
unordered_set<int> not_decendents[1001];
unordered_set<int> all_decendents[1001];
unordered_set<int> all_ancestors[1001];
int parent[1001];
void getDirectDecententCandidates(int node, unordered_set<int>& result) {
  result.clear();
  for(int decendent: all_decendents[node]){
    all_ancestors[decendent].erase(node);
    if(all_ancestors[decendent].empty()) {
      result.insert(decendent);
    }
  }
}

bool haveSameDecendents(int a, int b) {
  for(int decendent : all_decendents[a]) {
    if (all_decendents[b].find(decendent) != all_decendents[b].end()){
      return true;
    }
  }
  return false;
}

bool canBeAncestor(int ancestor, int decendent) {
  if (not_decendents[ancestor].find(decendent) != not_decendents[ancestor].end()) {
    return false;
  }
  for(int decendent_decendent : all_decendents[decendent]) {
    if (not_decendents[ancestor].find(decendent_decendent) != not_decendents[ancestor].end()){
      return false;
    }
  }
  return true;
}
void makeDecendent(int ancestor, int decendent){
  all_decendents[ancestor].insert(decendent);
  all_ancestors[decendent].insert(ancestor);
  for(int decendent_decendent: all_decendents[decendent]){
    all_decendents[ancestor].insert(decendent_decendent);
    all_ancestors[decendent_decendent].insert(ancestor);
  }
  
}
bool calculateParents(int node){
  if(all_decendents[node].empty()) {
    return true;
  }
  unordered_set<int> candidates;
  getDirectDecententCandidates(node, candidates);
  unordered_set<int> direct_decendents;
  unordered_map<int,int> group_for_node;
  unordered_map<int,unordered_set<int>> groups;
  for(int candidate: candidates){
    group_for_node[candidate] = candidate;
    groups[candidate].insert(candidate);
  }
  while(!candidates.empty()) {
    int candidate = *candidates.begin();
    candidates.erase(candidate);
    for (int other_candidate : candidates){
      if(group_for_node[candidate] == group_for_node[other_candidate]) {
        continue;
      }
      if (haveSameDecendents(candidate, other_candidate)) {
        
        int g1 = group_for_node[candidate];
        int g2 = group_for_node[other_candidate];
        for(int member: groups[g2]){
          groups[g1].insert(member);
          group_for_node[member] = g1;
        }
        groups.erase(g2);
      }
    }
  }
  
  for(const auto& group: groups){
    const auto& nodes = group.second;
    int ancestor;
    for(int ancestor_candidate:nodes){
      ancestor = ancestor_candidate;
      for(int decedent:nodes){
        if(ancestor == decedent){
          continue;
        }
        if (!canBeAncestor(ancestor,decedent)) {
          ancestor = -1;
          break;
        }
      }
      if(ancestor >0){
        break;
      }
    }
    if(ancestor<0){
      return false;
    }
    for(int decedent:nodes){
      if(ancestor == decedent){
        continue;
      }
      makeDecendent(ancestor,decedent);
    }
    direct_decendents.insert(ancestor);
  }
  for(int direct_decendent : direct_decendents){
    parent[direct_decendent] = node;
    
    if(!calculateParents(direct_decendent)){
      return false;
    }
  }
  return true;
}
int main(int argc, char **argv){
	int n, m;
  scanf("%d %d", &n, &m);
  for(int i = 0; i < m; i++){
    int a,b;
    char c;
    scanf("%d %d %c", &a, &b, &c);
    
    if (c =='T') {
      ancestors[a].insert(b);
    } else {
      not_decendents[b].insert(a);
    }
  }
  int root = -1;
  for(int j = 1; j <= n; j++){
    if(ancestors[j].empty() && not_decendents[j].empty()) {
      root = j;
      break;
    }
  }
  if (root == -1) {
    printf("NIE\n");
    return 0;
  }
  
  for (int j = 1; j <= n; j++) {
    unordered_set<int> visited;
    queue<int> q;
    q.push(j);
    while(!q.empty()){
      for(int ancestor : ancestors[q.front()]) {
        if (ancestor == j) {
          printf("NIE\n");
          return 0;
        }
        if(visited.find(ancestor) == visited.end()){
          visited.insert(ancestor);
          q.push(ancestor);
          all_ancestors[j].insert(ancestor);
        }
      }
      q.pop();
    }
  }
  for(int j = 1; j <= n; j++){
    for(int ancestor : all_ancestors[j]){
      if(not_decendents[ancestor].find(j) != not_decendents[ancestor].end()) {
        printf("NIE\n");
        return 0;
      }
      all_decendents[ancestor].insert(j);
    }
  }
  
  parent[root] = 0;
  for (int i=1; i <= n; i++) {
    if(i != root){
      all_decendents[root].insert(i);
      all_ancestors[i].insert(root);
    }
  }
  if(calculateParents(root)) {
    for(int i=1; i<=n; i++){
      printf("%d\n", parent[i]);
    }
  } else {
    printf("NIE\n");
  }
  return 0;
}