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
#include <cinttypes>
#include <cstdio>
#include <cstring>

char relations[1000][1000];

enum ParentRel {
	PR_Can,
	PR_Cant,
	PR_Must,
	PR_Invalid,
};

struct Node
{
	uint32_t emp;
	uint32_t level;
	Node* parent;
	Node* child;
	Node* next;
	Node* prev;

	Node* trueParent;

	void init(uint32_t e) {
		emp = e;
		next = prev = this;
	}

	ParentRel checkParentRel(uint32_t e) {
		ParentRel pr = PR_Can;
		Node* n = this;
		do {
			switch (relations[e][n->emp]) {
			case 'N':
				if (pr == PR_Must) {
					return PR_Invalid;
				}
				pr = PR_Cant;
				break;
			case 'T':
				if (pr == PR_Cant) {
					return PR_Invalid;
				}
				pr = PR_Must;
				break;
			}
			n = n->next;
		} while (n != this);
		return pr;
	}

	ParentRel checkParentRel(Node* e) {
		ParentRel pr = PR_Can;
		Node* n = e;
		do {
			switch (checkParentRel(n->emp)) {
			case PR_Can:
				break;
			case PR_Cant:
				if (pr == PR_Must) {
					return PR_Invalid;
				}
				pr = PR_Cant;
				break;
			case PR_Must:
				if (pr == PR_Cant) {
					return PR_Invalid;
				}
				pr = PR_Must;
				break;
			case PR_Invalid:
				return PR_Invalid;
			}
			n = n->next;
		} while (n != e);
		return pr;
	}

	void setLevel(uint32_t lvl) {
		Node* n = this;
		do {
			n->level = lvl;
			n = n->next;
		} while (n != this);
	}

	Node* findParent(Node* n) {
		Node* p = nullptr;
		Node* i = this;
		do {
			if (relations[n->emp][i->emp] == 'T') {
				if (p) {
					return n;
				}
				p = i;
			}
			i = i->next;
		} while (i != this);
		return p;
	}

	void print(const char* tag) {
		printf("%s", tag);
		for (Node* i = this; i; i = i->child) {
			printf("(");
			Node* j = i;
			do {
				printf(" %u", j->emp + 1);
				j = j->next;
			} while (j != i);
			printf(" )");
		}
		printf("\n");
	}
};

Node nodes[1000];

Node* head = &nodes[0];
Node* tail = &nodes[0];

void addParent(Node* o, Node* n) {
	n->parent = o->parent;
	n->child = o;
	o->parent = n;
	if (n->parent) {
		n->parent->child = n;
	} else {
		head = n;
	}
}

void addTail(Node* n) {
	tail->child = n;
	n->parent = tail;
	tail = n;
}

void addSibling(Node* o, Node* n) {
	n->parent = o->parent;
	o->parent = nullptr;
	if (n->parent) {
		n->parent->child = n;
	} else {
		head = n;
	}
	n->child = o->child;
	o->child = nullptr;
	if (n->child) {
		n->child->parent = n;
	} else {
		tail = n;
	}

	Node* otail = o->prev;
	Node* ntail = n->prev;
	n->prev = otail;
	otail->next = n;
	o->prev = ntail;
	ntail->next = o;
}

Node* remNode(Node* n) {
	if (n->parent) {
		n->parent->child = n->child;
	} else {
		head = n->child;
	}
	if (n->child) {
		n->child->parent = n->parent;
	} else {
		tail = n->parent;
	}
	n->child = nullptr;
	n->parent = nullptr;
	return n;
}

Node* remEmp(Node* n) {
	if (n->next != n) {
		n->next->parent = n->parent;
		if (n->parent) {
			n->parent->child = n->next;
		} else {
			head = n->next;
		}
		n->next->child = n->child;
		if (n->child) {
			n->child->parent = n->next;
		} else {
			tail = n->next;
		}
		n->next->prev = n->prev;
		n->prev->next = n->next;
		n->next = n;
		n->prev = n;
	} else {
		if (n->parent) {
			n->parent->child = n->child;
		} else {
			head = n->child;
		}
		if (n->child) {
			n->child->parent = n->parent;
		} else {
			tail = n->parent;
		}
	}
	n->child = nullptr;
	n->parent = nullptr;
	return n;
}

int main(int argc, const char* argv[])
{
	memset(relations, 0, sizeof(relations));
	memset(nodes, 0, sizeof(nodes));

	uint32_t emps, rels;
	scanf("%u %u", &emps, &rels);

	for (uint32_t ri = 0; ri < rels; ++ri) {
		uint32_t a, b;
		char r;
		scanf("%u %u %c", &a, &b, &r);
		relations[a - 1][b - 1] = r;
	}

	head->init(0);
	for (uint32_t e = 1; e < emps; ++e) {
		Node* n = &nodes[e];
		n->init(e);

		for (Node* i = head; i; i = i->child) {
			switch (ParentRel inPr = i->checkParentRel(n)) {
			case PR_Cant:
				switch (ParentRel niPr = n->checkParentRel(i)) {
				case PR_Cant:
					addSibling(i, n);
					i = tail;
					break;
				case PR_Can:
				case PR_Must:
					addParent(i, n);
					i = tail;
					break;
				case PR_Invalid:
					printf("NIE\n");
					return 0;
				}
				break;
			case PR_Can:
			case PR_Must:
				break;
			case PR_Invalid:
				printf("NIE\n");
				return 0;
			}
		}

		if (!n->child && n->next == n) {
			addTail(n);

		} else {
			for (Node* i = n->child; i; i = i->child) {
				switch (ParentRel inPr = i->checkParentRel(n)) {
				case PR_Must:
					for (Node* j = i->parent; j != n; j = j->parent) {
						switch (ParentRel jiPr = j->checkParentRel(i)) {
						case PR_Can:
						case PR_Must:
							break;
						case PR_Cant:
						case PR_Invalid:
							printf("NIE\n");
							return 0;
						}
					}
					i = i->parent;
					addParent(n, remNode(i->child));
					break;
				case PR_Can:
				case PR_Cant:
					break;
				case PR_Invalid:
					printf("NIE\n");
					return 0;
				}
			}
		}
	}
	for (Node* n = head; n; n = n->child) {
		if (n->next == n) {
			bool newHead = true;
			for (Node* m = n->parent; m && newHead; m = m->parent) {
				switch (ParentRel nmPr = n->checkParentRel(m)) {
				case PR_Can:
				case PR_Must:
					break;
				case PR_Cant:
					newHead = false;
					break;
				case PR_Invalid:
					printf("NIE\n");
					return 0;
				}
			}
			if (newHead) {
				if (n != head) {
					addParent(head, remNode(n));
				}
				break;
			}
		}
	}

	uint32_t level = 0;
	for (Node* n = head; n; n = n->child) {
		n->setLevel(level++);
	}

	while (head->child || head->next != head) {
		Node* n = remEmp(tail);
		Node* np = n;
		for (Node* i = tail; i; i = i->parent) {
			Node* p = i->findParent(n);
			if (p == n) {
				printf("NIE\n");
				return 0;
			}
			if (p) {
				if (np->trueParent) {
					Node* npp = np->trueParent;
					while (npp && npp->level > p->level) {
						np = npp;
						npp = np->trueParent;
					}
					if (npp) {
						if (npp->level == p->level) {
							printf("NIE\n");
							return 0;
						}
						if (p->trueParent) {
							if (p->trueParent->level >= npp->level) {
								printf("NIE\n");
							}
							npp->trueParent = p->trueParent;
						}
						p->trueParent = npp;
					}
				}
				np->trueParent = p;
				np = p;
			}
		}
	}

	for (uint32_t e = 0; e < emps; ++e) {
		Node* n = &nodes[e];
		if (n != head && !n->trueParent) {
			if (n->level == 0) {
				printf("NIE\n");
				return 0;
			}
			n->trueParent = head;
		}
	}

	for (uint32_t e = 0; e < emps; ++e) {
		Node* n = &nodes[e];
		if (n == head) {
			printf("0\n");
		} else {
			printf("%u\n", n->trueParent->emp + 1);
		}
	}
	return 0;
}