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
#include<bits/stdc++.h>
#define PII pair<int,int>
#define f first
#define s second
#define VI vector<int>
#define LL long long
#define MP make_pair
#define LD long double
#define PB push_back
#define ALL(V) V.begin(),V.end()
#define abs(x) max((x),-(x))
#define PDD pair<LD,LD> 
#define VPII vector< PII > 
#define siz(V) ((int)V.size())
#define FOR(x, b, e)  for(int x=b;x<=(e);x++)
#define FORD(x, b, e) for(int x=b;x>=(e);x--)
#define REP(x, n)     for(int x=0;x<(n);x++)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#ifdef DEB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
using namespace std;
#define copy asteh
const int MN = 1e3 + 44;
vector <int> under[MN];
vector <int> over[MN];
vector <int> notunder[MN];
const int NONE = -1;
int ans[MN];
void err() {
	printf("NIE\n");
	exit(0);
}
int last_layer[MN];
int call_count = 0;
int last_added[MN];
int dfs(int x, int res[]) {
	debug("dfs in %d\n", x);
	if (last_layer[x] < call_count) return 0;
	if (last_added[x] == call_count) return 0;
	last_added[x] = call_count;
	res[0] = x;
	int cou = 1;
	for (auto v : over[x])
		cou += dfs(v, res + cou);
	for (auto v : under[x])
		cou += dfs(v, res + cou);
	return cou;
}
int copy[MN];
void solve(int content[], int count, int parent) {
	debug("enter solve\n");
	for (int i = 0; i < count; ++i)
		debug("%d ", content[i]);
	debug("\n");
	call_count++;
	if (count < 2) {
		ans[content[0]] = parent;
		return;
	}
	for (int i = 0; i < count; ++i)
		last_layer[content[i]] = call_count;
	int root = NONE;
	for (int i = 0; i < count; ++i) {
		int can = content[i];
		bool good = true;
		for (auto v : over[can])
			if (last_layer[v] == call_count) {
				good = false;
				break;
			}
		for (auto v : notunder[can])
			if (last_layer[v] == call_count) {
				good = false;
				break;
			}
		if (good) {
			root = can;
			break;
		}
	}
	debug("root = %d\n", root);
	if (root == NONE) err();
	ans[root] = parent;
	last_layer[root] = -1;
	for (int i = 1; i < count; ++i)
		if (content[i] == root) {
			swap(content[i], content[0]);
			break;
		}
	memcpy(copy, content + 1, (count - 1) * sizeof(int));
	debug("copy:\n");
	for (int i = 0; i < count - 1; ++i) 
		debug("%d ", copy[i]);
	debug("\n");
	vector <int> spl;
	int pos = 1;
	for (int i = 0; i < count - 1; ++i) {
		debug("call dfs from %d\n", copy[i]);
		int a = dfs(copy[i], content + pos);
		spl.push_back(a);
		pos += a;
	}
	pos = 1;
	for (int x : spl) 
		if (x) {
			solve(content + pos, x, root);
			pos += x;
		}
}
int all[MN];
int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; ++i) all[i] = i + 1;
	for (int i = 0; i < m; ++i) {
		int a, b;
		char c[2];
		scanf("%d%d%s", &a, &b, c);
		if (c[0] == 'T') {
			over[a].push_back(b);
			under[b].push_back(a);
		}
		else {
			notunder[b].push_back(a);
		}
	}
	solve(all, n, 0);
	debug("\n\n");
	for (int i = 1; i <= n; ++i) {
		debug("%d.", i);
		printf("%d\n", ans[i]);
	}
}