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
#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x) {cout << x; exit(0); }
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define scanf(...) scanf(__VA_ARGS__)?:0
typedef int ll;
typedef unsigned long long ull;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef pair <PLL, int> PLLI;
typedef pair <PLL, PLL> PP;
typedef pair <PII, int> PPI;
typedef pair <ll, int> PLI;
typedef unsigned int ui;
const int inf = 1e9+9;
const ll MOD = 1e9+696969;
const long long INF = 1e18+3;
const int maxn = 1010;
int n, m, a, b, root; char zn;
vector <PII> good, bad;
int parent[maxn], post[maxn], pre[maxn], DL = 0;

vector <int> graf[maxn], v[maxn], outgoing[maxn];
int indeg[maxn];
bool odw[maxn];

void vDFS(int x) {
	pre[x] = ++DL;
	for (int i=0; i<(int)graf[x].size(); ++i) {
		int u = graf[x][i];
		vDFS(u);
	}
	post[x] = ++DL;
}
inline bool xxxAncestor(int a, int b) { //czy a jest przodkiem b
	return pre[a] <= pre[b] && post[a] >= post[b];
}

bool validate() {
	int jest = 0;
	FOR(i, 1, n) 
	{
	  assert(parent[i] >= 0 && parent[i] <= n);
	  if (!parent[i]) jest++;
	  else graf[parent[i]].pb(i);
	}
	assert(jest == 1);
	FOR(i, 1, n)
	  if (!parent[i]) vDFS(i);
	  
	for (int i=0; i<(int)good.size();++i)
	  if (!xxxAncestor(good[i].e1, good[i].e2)) {
		  cout << "UpsxD\n";
		  return 0;
	  }
	
	for (int i=0; i<(int)bad.size(); ++i)
	  if (xxxAncestor(bad[i].e2, bad[i].e1)) {
		  cout << "H\n";
		  return 0;
	  }
	
	return 1;
}

bool cannotBeRoot[maxn];
int ostOdw[maxn], gold[maxn], nalezyDo[maxn];

void dfs(int x, int rr, int skl) {
	ostOdw[x] = rr;
	nalezyDo[x] = skl;
	for (int i=0; i<(int)v[x].size(); ++i) {
		int u = v[x][i];
		if (!gold[u] && ostOdw[u] != rr) dfs(u, rr, skl);
	}
}

int getNoHateVertex(int conn, int ktoraSkladowa) {
	int cnt[n + 5];
	FOR(i, 1, n) {
		if (ostOdw[i] == conn && nalezyDo[i] == ktoraSkladowa && !indeg[i]) cnt[i] = 1;
		else cnt[i] = 0;
	}
	
	for (int i=0; i<(int)bad.size(); ++i) {
		int a = bad[i].e1, b = bad[i].e2; //a nie chce b jako przodka
		if (ostOdw[a] == ostOdw[b] && ostOdw[a] == conn && nalezyDo[a] == ktoraSkladowa && nalezyDo[b] == ktoraSkladowa) cnt[b] = 0;
	}
	FOR(i, 1, n)
	  if (cnt[i]) return i;
	return -1;
}
void FindAnswer(int rr) {
	gold[rr] = 1;
	for (int i=0; i<(int)outgoing[rr].size(); ++i) indeg[outgoing[rr][i]]--;
	vector <int> nowe; nowe.clear();
	int cc = 0;
	for (int i=0; i<(int)outgoing[rr].size(); ++i) {
		int u = outgoing[rr][i];
		if (!gold[u] && ostOdw[u] != rr) {
			dfs(u, rr, ++cc);
			int w = getNoHateVertex(rr, cc);
			if (w == -1) OUT("NIE");
			nowe.pb(w);
		}
	 }

	 for (int i=0; i<(int)nowe.size(); ++i) {
		 parent[nowe[i]] = rr;
		 FindAnswer(nowe[i]);
	 }
}
int main()
{
	scanf("%d%d", &n, &m);
	FOR(i, 1, m) {
		scanf("%d%d %c", &a, &b, &zn);
		assert(a != b);
		if (zn == 'T') 
		{
			good.pb(mp(b, a));
			cannotBeRoot[a] = 1;
		}
		else 
		{
			bad.pb(mp(a, b));
			cannotBeRoot[b] = 1;
		}
	}
	
	FOR(i, 1, n)
	  if (!cannotBeRoot[i]) {
		  root = i;
		  break;
	  }

	if (!root) OUT("NIE");
	FOR(i, 1, n)
	  if (i != root) v[root].pb(i), v[i].pb(root), outgoing[root].pb(i), indeg[i]++;
	  
	for (int i=0; i<(int)good.size(); ++i) {
		PII u = good[i];
		v[u.e1].pb(u.e2), v[u.e2].pb(u.e1);
		outgoing[u.e1].pb(u.e2);
		indeg[u.e2]++;
	}

	FindAnswer(root);
	//puts("TAK");
	if (!validate()) puts("Cos sie zepsulo. Ups.");
	else 
	{
		//puts("TAK");
		FOR(i, 1, n) printf("%d\n", parent[i]);
	}
}