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
#include <iostream>
#include <set>
#include <queue>
#include <iterator>

using namespace std;

struct node {
    set<unsigned short> parents;
    set<unsigned short> disliked;
};

node nodes[1010];
set<unsigned short> leafs;
set<unsigned short> possible_directors;
bool visited[1010] = {0};
bool visited_persistent[1010] = {0};

bool propagate_dislikes(unsigned short node, const set<unsigned short>& dislikes, bool breaking)
{
  if (visited[node])
    {
      return false;
    }
  visited_persistent[node] = true;
  visited[node] = true;
  size_t tmp_size = nodes[node].disliked.size();
  nodes[node].disliked.insert(dislikes.begin(), dislikes.end());
  if (breaking && tmp_size == nodes[node].disliked.size())
    {
      return true;
    }
  for (auto i : nodes[node].parents)
    {
      if (!propagate_dislikes(i, nodes[node].disliked, breaking))
        {
          return false;
        }
    }
  if (nodes[node].parents.empty())
    {
      possible_directors.insert(node);
    }

  visited[node] = false;
  return true;
}

bool choose_director()
{
  bool can_be_director[1010] = {0};
  unsigned short found = 0;

  for (auto i : possible_directors)
    {
#ifdef DEBUG
      cerr << i << " can be director" << endl;
#endif
      can_be_director[i] = true;
    }
  for (auto j : possible_directors)
    {
      for (auto i : nodes[j].disliked)
        {
#ifdef DEBUG
          cerr << i << " can't be director" << endl;
#endif
          can_be_director[i] = false;
        }
    }
  for (unsigned short i = 1; i < sizeof(can_be_director); ++i)
    {
      if (can_be_director[i] == true)
        {
#ifdef DEBUG
          cerr << "Found " << i << " director" << endl;
#endif
          found = i;
          break;
        }
    }
  if (found == 0)
    {
#ifdef DEBUG
      cerr << "Did not find any director" << endl;
#endif
      return false;
    }

  for (auto i : possible_directors)
    {
      if (i != found)
        {
          nodes[i].parents.insert(found);
        }
    }
  leafs.erase(found);

  return true;
}

bool check_dislikes(unsigned short node)
{
  if (nodes[node].disliked.find(node) != nodes[node].disliked.end())
    {
      return false;
    }
  if (nodes[node].parents.empty())
    {
      return true;
    }
  for (auto i : nodes[node].parents)
    {
      if (!check_dislikes(i))
        {
          return false;
        }
    }
  return true;
}

bool check_if_can_descendant(unsigned short node, unsigned short descendant)
{
#ifdef DEBUG
  cerr << __FUNCTION__ << " node " << node << " parents ";
  for (auto i : nodes[node].parents)
    {
      cerr << i << " ";
    }
  cerr << "descendant " << descendant << " parents ";
  for (auto i : nodes[descendant].parents)
    {
      cerr << i << " ";
    }
  cerr << endl;
#endif
  if (nodes[descendant].disliked.find(node) != nodes[descendant].disliked.end())
    {
#ifdef DEBUG
      cerr << "Node " << node << " is disliked by " << descendant << endl;
#endif
      return false;
    }
  for (auto i : nodes[node].parents)
    {
      if (i == descendant)
        {
          return false;
        }
      if (!check_if_can_descendant(i, descendant))
        {
          return false;
        }
    }
  return true;
}

bool merge_paths(unsigned short node)
{
#ifdef DEBUG
  cerr << __FUNCTION__ << " " << node << " parents: ";
  for (auto i : nodes[node].parents)
    {
      cerr << i << " ";
    }
  cerr << endl;
#endif
  if (nodes[node].parents.empty())
    {
      return true;
    }
  while (nodes[node].parents.size() > 1)
    {
      set<unsigned short>::iterator it = nodes[node].parents.begin();
      unsigned short candidate = *it;
      advance(it, 1);
      unsigned short descendant = *it;
      unsigned short candidate2 = descendant;
      unsigned short descendant2 = candidate;
      if (check_if_can_descendant(candidate, descendant))
        {
#ifdef DEBUG
          cerr << "Doing candidate " << candidate << " descendant " << descendant << endl;
#endif
          nodes[node].parents.erase(candidate);
          nodes[descendant].parents.insert(candidate);
          propagate_dislikes(descendant, nodes[node].disliked, true);
        }
      else if(check_if_can_descendant(candidate2, descendant2))
        {
#ifdef DEBUG
          cerr << "Doing candidate2 " << candidate2 << " descendant2 " << descendant2 << endl;
#endif
          nodes[node].parents.erase(candidate2);
          nodes[descendant2].parents.insert(candidate2);
          propagate_dislikes(descendant2, nodes[node].disliked, true);
        }
      else
        {
          return false;
        }
    }
  return merge_paths(*nodes[node].parents.begin());
}

unsigned short output[1010] = {0};
void prepare_output(unsigned short node)
{
  if (output[node] || nodes[node].parents.empty())
    {
      return;
    }
  output[node] = *nodes[node].parents.begin();
  prepare_output(*nodes[node].parents.begin());
}

int main()
{
  ios_base::sync_with_stdio(false);

  unsigned short n, m, a, b;
  char c;
  cin >> n >> m;

  for (unsigned short i = 1; i <= n; ++i)
    {
      leafs.insert(leafs.end(), i);
    }
  for (unsigned short i = 0; i < m; ++i)
    {
      cin >> a >> b >> c;
      if (c == 'T')
        {
          nodes[a].parents.insert(b);
          leafs.erase(b);
        }
      else
        {
          nodes[a].disliked.insert(b);
        }
    }

  if (n == 1)
    {
      cout << "0" << endl;
      return 0;
    }

  if (leafs.empty())
    {
#ifdef DEBUG
      cerr << "Leafs empty" << endl;
#endif
      cout << "NIE" << endl;
      return 0;
    }
  for (auto i : leafs)
    {
      if (!propagate_dislikes(i, set<unsigned short>{}, false))
        {
          cout << "NIE" << endl;
          return 0;
        }
    }
  for (unsigned short i = 1; i <= n; ++i)
    {
      if (!visited_persistent[i])
        {
          cout << "NIE" << endl;
          return 0;
        }
    }
#ifdef DEBUG
  for (unsigned short i = 1; i <= n; ++i)
    {
      cerr << i << ": parents ";
      for (auto j : nodes[i].parents)
        {
          cerr << j << " ";
        }
      cerr << "disliked ";
      for (auto j : nodes[i].disliked)
        {
          cerr << j << " ";
        }
      cerr << endl;
    }
#endif
  for (auto i : leafs)
    {
      if (!check_dislikes(i))
        {
          cout << "NIE" << endl;
          return 0;
        }
    }
  if (!choose_director())
    {
#ifdef DEBUG
      cerr << "Choosing director failed" << endl;
#endif
      cout << "NIE" << endl;
      return 0;
    }
#ifdef DEBUG
  for (unsigned short i = 1; i <= n; ++i)
    {
      cerr << i << ": parents ";
      for (auto j : nodes[i].parents)
        {
          cerr << j << " ";
        }
      cerr << "disliked ";
      for (auto j : nodes[i].disliked)
        {
          cerr << j << " ";
        }
      cerr << endl;
    }
#endif
  for (auto i : leafs)
    {
      if (!merge_paths(i))
        {
#ifdef DEBUG
          cerr << "Merging paths " << i << " failed" << endl;
#endif
          cout << "NIE" << endl;
          return 0;
        }
    }
  for (auto i : leafs)
    {
      prepare_output(i);
    }
  for (unsigned short i = 1; i <= n; ++i)
    {
      cout << output[i] << endl;
    }

  return 0;
}