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
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;

typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<int,int> pii;
#define mp make_pair

#define REP(i,n) for (int i=0,___=(n); i<___; ++i)
#define FOR(i,a,b) for (int i=(a),___=(b); i<=___; ++i)
#define FORD(i,a,b) for (int i=(a),___=(b); i>=___; --i)

int read() { int n; scanf("%d", &n); return n; }
ll readl() { ll n; scanf("%lld", &n); return n; }
char readc() { static char s[32]; scanf("%s", s); return *s; }



///////////////////////////////////////////////////
/// ZBIORY ROZŁĄCZNE
template <int N>
class FindUnion {
	int parent[N];
public:
	FindUnion() { for (int i=0; i<N; ++i) parent[i] = -1; }
	int Find(int x) {
		int result = x, tmp;
		while (parent[result] >= 0) result = parent[result];
		while (x != result) {
			tmp = parent[x];
			parent[x] = result;
			x = tmp;
		}
		return result;
	}
	void Union(int x, int y) {
		x = Find(x);
		y = Find(y);
		if (x == y) return;
		if (parent[x] < parent[y]) {
			parent[x] += parent[y];
			parent[y] = x;
		} else {
			parent[y] += parent[x];
			parent[x] = y;
		}
	}
	int Size(int x) {
		return -parent[Find(x)];
	}
};

int n;
vector<set<int> > przel;
vector<set<int> > przelPrev;
vector<set<int> > notPrzel;
vector<bool> canBeRoot;

int findRoot() {
	REP(i, n) if (canBeRoot[i]) return i;
	return -1;
}

bool solve() {
	int root = findRoot();
	if (root < 0) return false;

	// find cycles
	REP(i, n) {
		vector<bool> visited(n, false);
		queue<int> q;
		for (set<int>::iterator it = przel[i].begin(); it != przel[i].end(); ++it) {
			q.push(*it);
			visited[*it] = true;
		}
		while (!q.empty()) {
			int x = q.front();
			q.pop();
			if (x == i) return false; // cycle found
			if (notPrzel[i].count(x) > 0) return false; // i want x to be przel (indirect), but i dont want x to be przel (direct)
			for (set<int>::iterator it = przel[x].begin(); it != przel[x].end(); ++it) {
				if (visited[*it] == false) {
					q.push(*it);
					visited[*it] = true;
				}
			}
		}
	}

	// topological sort
	vector<int> cnt(n, 0);
	REP(i, n) {
		for (set<int>::iterator it = przel[i].begin(); it != przel[i].end(); ++it) {
			cnt[*it]++;
		}
	}
	cnt[root] = n+n;
	vector<int> order;
	queue<int> q;
	REP(i, n) if (cnt[i] == 0) q.push(i);
	while (!q.empty()) {
		int x = q.front();
		q.pop();
		order.push_back(x);
		for (set<int>::iterator it = przel[x].begin(); it != przel[x].end(); ++it) {
			if (0 == --cnt[*it]) {
				q.push(*it);
			}
		}
	}

	// mamy kolejnosc, robimy zbior drzew
	vector<int> res(n, -1);
	set<int> trees;

	FindUnion<1001> fu;
	vector<int> rootsByFuId(n, -1);

	REP(i, order.size()) {
		int x = order[i];
		if (przelPrev[x].empty()) {
			trees.insert(x);
			rootsByFuId[x] = x;
			continue;
		}
		set<int> treesToMerge;
		for (set<int>::iterator it = przelPrev[x].begin(); it != przelPrev[x].end(); ++it) {
			treesToMerge.insert(rootsByFuId[fu.Find(*it)]);
		}
		for (set<int>::iterator it = treesToMerge.begin(); it != treesToMerge.end(); ++it) {
			res[*it] = x;
			trees.erase(*it);
			fu.Union(x, *it);
		}
		trees.insert(x);
		rootsByFuId[fu.Find(x)] = x;
	}

	for (set<int>::iterator it = trees.begin(); it != trees.end(); ++it) {
		res[*it] = root;
	}

	REP(i, n) printf("%d\n", res[i] + 1);

	return true;
}

int main() {
	n = read();
	int m = read();

	przel.resize(n);
	przelPrev.resize(n);
	notPrzel.resize(n);
	canBeRoot.resize(n, true);

	REP(i, m) {
		int a = read() - 1;
		int b = read() - 1;
		char c = readc();
		if (c == 'T') {
			przel[a].insert(b);
			przelPrev[b].insert(a);
			canBeRoot[a] = false;
		} else {
			notPrzel[a].insert(b);
			canBeRoot[b] = false;
		}
	}

	if (!solve()) printf("NIE\n");

	return 0;
}