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
#include <bits/stdc++.h>

using namespace std;

#define pb push_back

const int N = 1007;

int licz;
int n, m;
int rep[N];
int ans[N];
int help[N];
int order[N];
int number[N];
vector <int> V;
vector <int> UF[N];
vector <int> bad[N];
vector <int> anc[N];

bool ok(int a){
	for(auto v: bad[a])
		if(number[v] == number[a])
			return false;
	
	for(auto v: anc[a])
		if(ans[v] == -1)
			return false;

	return true;
}

void Union(int a, int b){
	if(rep[a] == rep[b])
		return;
	
	a = rep[a];
	b = rep[b];
	
	if(UF[a].size() < UF[b].size())
		swap(a, b);
	for(auto v: UF[b]){
		rep[v] = a;
		UF[a].pb(v);
	}
	
	UF[b].clear();
}

void Union_all(int p, int k){
	for(int i = p; i <= k; ++i)
		if(number[order[i]] != -1)
			for(auto v: anc[order[i]])
				if(number[v] == number[order[i]])
					Union(v, order[i]);
}

void rekurence(int from, int to, int p){
	if(from > to)
		return;
	
	for(int i = from; i <= to; ++i)
		if(ok(order[i]))
			V.pb(order[i]);
	
	if(V.size() == 0)
		return;
	for(auto v: V){
		ans[v] = p;
		p = v;
		number[v] = -1;
	}
	
	V.resize(0);
	for(int i = from; i <= to; ++i)
		if(number[order[i]] != -1){
			rep[order[i]] = order[i];
			UF[order[i]].clear();
			UF[order[i]].pb(order[i]);
		}
	
	Union_all(from, to);
	int last = from;
	vector <int> starts;
	starts.pb(from);

	for(int i = from; i <= to; ++i)
		if(rep[order[i]] == order[i] && number[order[i]] != -1){
			++licz;
			for(int j = 0; j < UF[order[i]].size(); ++j){
				help[last + j] = UF[order[i]][j];
				number[help[last + j]] = licz;
			}

			last += UF[order[i]].size();
			starts.pb(last);
		}
	
	for(int i = from; i < last; ++i)
		order[i] = help[i];
	for(int i = 1; i < starts.size(); ++i)
		rekurence(starts[i - 1], starts[i] - 1, p);
}

int main(){
	scanf("%d %d", &n, &m);
//	printf("%d %d\n", n, m);
	for(int i = 0; i < m; ++i){
		int a, b;
		char c[2];
		scanf("%d %d %s", &a, &b, c);
//		printf("%d %d %s\n", a, b, c);
		if(c[0] == 'T')
			anc[a].pb(b);
		else
			bad[b].pb(a);
	}
	
	for(int i = 1; i <= n; ++i){
		order[i] = i;
		ans[i] = -1;
	}
	
	rekurence(1, n, 0);
	
	bool answer = true;
	for(int i = 1; i <= n; ++i)
		if(ans[i] == -1)
			answer = false;
	
	if(!answer){
		puts("NIE");
		return 0;
	}
	
//	puts("TAK");
	for(int i = 1; i <= n; ++i)
		printf("%d\n", ans[i]);
	return 0;
}