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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <stdint.h>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>


static const uint32_t MAX = 1000;

static const uint32_t CONNECTED = 0;
static const uint32_t NOT_CONNECTED = 1;
static const uint32_t NEEDED_CONNECTION = 2;


static uint32_t N = 0;
static uint32_t M = 0;

static uint32_t gGraph[MAX][MAX];


void read_input(std::map<uint32_t, std::set<uint32_t>> &gDesires)
{
	std::cin >> N;
	std::cin >> M;

	for (uint32_t i = 0; i < M; ++i)
	{
		uint32_t a = 0;
		uint32_t b = 0;
		char c = 0;

		std::cin >> a;
		std::cin >> b;
		std::cin >> c;

		switch (c)
		{
			case 'T':
				// DEBUG
//				std::cout << a << " " <<  b << " " << c << std::endl;

				gDesires[a-1].insert(b-1);
				break;
			case 'N':
				gGraph[a-1][b-1] = NOT_CONNECTED;
				break;
			default:
				throw "Invalid char in input!!!";
		}
	}

	for (const auto &i : gDesires)
	{
		for (uint32_t j = 0; j < N; ++j)
		{
			if (i.second.find(j) == i.second.end())
			{
				gGraph[i.first][j] = NOT_CONNECTED;
			}
			else
			{
				gGraph[i.first][j] = NEEDED_CONNECTION;
			}
		}
	}
}


void setup_graph()
{
	for (uint32_t i = 0; i < N; ++i)
	{
		gGraph[i][i] = NOT_CONNECTED;
	}
}


void print_graph()
{
	for (uint32_t i = 0; i < N; ++i)
	{
		for (uint32_t j = 0; j < N; ++j)
		{
			std::cout << gGraph[i][j] << " ";
		}

		std::cout << std::endl;
	}
}


void scan_graph(std::set<uint32_t> &directors, bool &no_connections)
{
	for (uint32_t i = 0; i < N; ++i)
	{
		bool connections = false;
		bool director = true;

		for (uint32_t j = 0; j < N; ++j)
		{
			if (gGraph[i][j] == CONNECTED)
			{
				connections = true;
			}

			if (gGraph[i][j] == NEEDED_CONNECTION)
			{
				connections = true;
				director = false;
			}
		}

		if (director == true)
		{
			directors.insert(i);
		}
		else
		{
			if (connections == false)
			{
				// DEBUG
//				std::cout << "no connections, v: " << i << std::endl;

				no_connections = true;
			}
		}
	}
}


void remove_desire(uint32_t v, std::map<uint32_t, std::set<uint32_t>> &desires)
{
	// DEBUG
//	std::cout << "remove: " << v << std::endl;

	for (auto &i : desires)
	{
		i.second.erase(v);
	}

	for (auto iter = desires.begin(); iter != desires.end(); )
	{
		if (iter->second.empty())
		{
			auto temp = iter;
			++iter;
			desires.erase(temp);
		}
		else
		{
			++iter;
		}
	}
}


void traverse(uint32_t director, std::map<uint32_t, std::set<uint32_t>> &desires)
{
	std::vector<uint32_t> visited(N);
	std::vector<uint32_t> parent(N);
	uint32_t visited_count = 0;

	std::queue<uint32_t> queue;

	queue.push(director);
	visited[director] = 1;

	while (queue.empty() == false)
	{
		uint32_t v = queue.front();
		queue.pop();

		// DEBUG
//		std::cout << "visited: " << v << std::endl;

		visited_count++;
		remove_desire(v, desires);

		for (uint32_t i = 0; i < N; ++i)
		{
			if (gGraph[i][v] != NOT_CONNECTED && visited[i] == 0 && desires.find(i) == desires.end())
			{
				// DEBUG
//				std::cout << "push: " << i << std::endl;

				queue.push(i);
				visited[i] = 1;

				parent[i] = v + 1;
			}
		}
	}

	if (visited_count != N)
	{
		std::cout << "NIE" << std::endl;
	}
	else
	{
		for (uint32_t i = 0; i < N; ++i)
		{
			std::cout << parent[i] << std::endl;
		}
	}
}


int main()
{
	std::ios_base::sync_with_stdio(0);

	//
	std::map<uint32_t, std::set<uint32_t>> desires;

	read_input(desires);
	setup_graph();

	// DEBUG
//	print_graph();

	//
	std::set<uint32_t> directors;
	bool no_connections = false;

	scan_graph(directors, no_connections);

	//
	if (no_connections == true || directors.empty() == true)
	{
		// DEBUG
//		std::cout << "no connections: " << no_connections << std::endl;
//		std::cout << "directors: " << directors.empty() << std::endl;

		std::cout << "NIE" << std::endl;
		return 0;
	}

	// DEBUG
//	for (const auto &i : gDesires)
//	{
//		std::cout << "desire: " << i.first << "\n\t";
//		for (const auto &j : i.second)
//		{
//			std::cout << j << " ";
//		}
//		std::cout << std::endl;
//	}

	traverse(*(directors.begin()), desires);
	return 0;
}