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
184
185
186
187
188
189
190
191
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define inf 1000000000
#define INF 1000000000000000000LL
#define VI vector<int>
#define VLL vector<ll>
#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
using namespace std;

#define NAX 1007

int n, m, a, b, curr, r, ile0;

char c;

vector<int> T[NAX];

int res[NAX];

bitset<NAX> N[NAX];

bitset<NAX> vis[NAX];

bitset<NAX> root;

bitset<NAX> leaf;

int na_ilu[NAX];

int numer[NAX][NAX];

vector<int> sciezki[NAX];

vector<int> sub[NAX];

bool comp(int a, int b)
{
	return numer[curr][a]+na_ilu[a]<numer[curr][b]+na_ilu[b];
}

bool dfs(int s, int v)
{
	if(numer[s][v]>m+7)
		return 1;

	if(!vis[s][v])
	{
		vis[s][v] = 1;
		sciezki[s].pb(v);
		++na_ilu[v];
	}

	for(int i = 0; i < T[v].size(); ++i)
	{
		if(numer[s][T[v][i]]<numer[s][v]+1)
		{
			numer[s][T[v][i]] = numer[s][v]+1;

			if(dfs(s, T[v][i]))
				return 1;
		}
	}

	return 0;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);

	cin >> n >> m;

	leaf.set();
	root.set();

	for(int i = 1; i <= m; ++i)
	{
		cin >> a >> b >> c;

		if(c=='T')
		{
			T[a].pb(b);
			root[a] = 0;
			leaf[b] = 0;
		}
		else
		{
			N[a][b] = 1;
			root[b] = 0;
		}
			
	}

	for(int i = 1; i <= n; ++i)
	{
		if(root[i])
		{
			r = i;
			leaf[r] = 0;
			break;
		}
	}

	for(int i = 1; i <= n; ++i)
	{
		if(i!=r)
		{
			T[i].pb(r);
		}
	}

	for(int i = 1; i <= n; ++i)
	{
		if(leaf[i])
		{
			curr = i;

			if(dfs(i, i))
			{
				cout << "NIE";
				return 0;
			}
		}
	}

	for(int i = 1; i <= n; ++i)
	{
		if(leaf[i])
		{
			curr = i;

			for(auto x:sciezki[curr])
			{
				for(auto y:sciezki[curr])
				{
					if(N[x][y])
					{
						T[y].pb(x);
					}
				}
			}

			sort(sciezki[curr].begin(), sciezki[curr].end(), comp);

			for(int j = 0; j < sciezki[curr].size()-1; ++j)
			{
				res[sciezki[curr][j]] = sciezki[curr][j+1];
			}

			for(int j = 0; j < sciezki[curr].size(); ++j)
			{
				for(int k = j+1; k < sciezki[curr].size(); ++k)
				{
					if(N[sciezki[curr][j]][sciezki[curr][k]])
					{
						cout << "NIE";
						return 0;
					}
				}
			}
		}
	}

	for(int i = 1; i <= n; ++i)
	{
		if(!res[i] && i!=r)
		{
			cout << "NIE";
			return 0;
		}
	}

	//cout << "TAK";
	//return 0;

	for(int i = 1; i <= n; ++i)
	{
		cout << res[i] << endl;
	}
}