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
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;

int n, m;
int wp[1007];
vector <int> GN[1007];
vector <int> GT[1007];
vector <int> GTR[1007];
int zb[1007];
int zbc[1007];
bool wynik = 1;
int ok1[1007];
int jest[1007];

int gp;

void dfs(int v) {
	zbc[gp++] = v;
	jest[v] = 0;
	for(auto u : GT[v]) if(jest[u]) dfs(u);
	for(auto u : GTR[v]) if(jest[u]) dfs(u);
}

void f(int a, int b, int root = 0) {
	//cout << "f: ";
	//for(int i = a; i <= b; ++i) cout << zb[i] << ' ';
	//cout << endl;
	
	for(int i = a; i <= b; ++i) jest[zb[i]] = ok1[zb[i]] = 1;
	
	for(int i = a; i <= b; ++i) {
		for(auto u : GN[zb[i]]) ok1[u] = 0;
		for(auto u : GT[zb[i]]) if(jest[u]) ok1[zb[i]] = 0;
	}
	
	int il = 0;
	for(int i = a; i <= b; ++i) {
		if(ok1[zb[i]]) {
			il++;
			//cout << "korz - " << zb[i] << endl;
			wp[zb[i]] = root;
			root = zb[i];
			jest[zb[i]] = 0;
		}
	}
	
	if(il == 0) {
		wynik = 0;
		return;
	}
	
	vector <int> ends;
	
	gp = a;
	for(int i = a; i <= b; ++i) {
		if(jest[zb[i]]) {
			dfs(zb[i]);
			ends.push_back(gp - 1);
		}
	}
	
	for(int i = a; i <= b; ++i) zb[i] = zbc[i];
	
	gp = a;
	for(auto i : ends) {
		f(gp, i, root);
		gp = i + 1;
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin >> n >> m;
	for(int i = 1; i <= m; ++i) {
		int a, b;
		char c;
		cin >> a >> b >> c;
		if(c == 'T') {
			GT[a].push_back(b);
			GTR[b].push_back(a);
		}
		else GN[a].push_back(b);
	}
	
	for(int i = 1; i <= n; ++i) zb[i] = i;
	f(1, n);
	if(wynik) for(int i = 1; i <= n; ++i) cout << wp[i] << '\n';
	else cout << "NIE\n";
	return 0;
}