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
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <string.h>
#include <vector>

#define LAST 1003

int main() {
	std::vector<std::set<int>> ancestor;
	std::vector<std::set<int>> decestor;

	int ancestor_count[LAST + 1];
	int decestor_count[LAST + 1];

	memset(ancestor_count, 0, sizeof(int) * (LAST + 1));
	memset(decestor_count, 0, sizeof(int) * (LAST + 1));

	int position[LAST];
	memset(position, -1, sizeof(int) * (LAST));

	int n = 0;
	int m = 0;
	std::cin >> n >> m;

	int count = n;

	for (int i = 1; i < n + 3; i++) {
		std::set<int> temp1;
		std::set<int> temp2;
		ancestor.push_back(temp1);
		decestor.push_back(temp2);
	}

	for (int i = 0; i < m; i++) {
		int a = 0;
		int b = 0;
		char c = '0';
		std::cin >> a >> b >> c;
		if (c == 'T') {
			ancestor[a].insert(b); // b jest przodkiem a
			ancestor_count[a]++; // a ma jednego przodka więcej
		} else { // c == N
			decestor[b].insert(a); //b nie może być przodkiem a
			decestor_count[b]++; //b 
		}
	}

	// for (int i = 1; i < n + 1; i++) {
	// 	std::cout << i << "\nprzodki : " << ancestor_count[i] << "\n";
	// 	for (auto it : ancestor[i]) {
	// 		std::cout << it << " ";
	// 	}
	// 	std::cout << "nie może : " << decestor_count[i] << "\n";
	// 	for (auto it : decestor[i]) {
	// 		std::cout << it << " ";
	// 	}
	// }

	//ROOT
	//choose the one with no ancestors
	//check if it is accepted by everyone
	int root = -1;
	for (int i = 1; i < n + 1; i++) {
		if (ancestor_count[i] == 0 && decestor_count[i] == 0) {
			root = i;
		}
	}
	if (root > 0) {
		position[root] = 0;
		count--;
		for (int i = 1; i < n + 1; i++) {
			ancestor[i].erase(root); // juz jest przodkiem
			decestor_count[i] = decestor[i].size();
			decestor[i].erase(root); // juz nie bedzie synem
			ancestor_count[i] = ancestor[i].size();
		}
		ancestor[root].clear();
		ancestor_count[root] = -1;
		decestor_count[root] = -1;
		decestor[root].clear();
		if (count > 0) {
			int has_more = count;
			int placed = 0;
			int level = 0;
			while (has_more > 0) { //
				level++;
				// for (int i = 1; i < n + 1; i++) {
				// 	std::cout << i << "\nprzooodki : " << ancestor_count[i] << "\n";
				// 	for (auto it : ancestor[i]) {
				// 		std::cout << it << " ";
				// 	}
				// 	std::cout << "nie może : " << decestor_count[i] << "\n";
				// 	for (auto it : decestor[i]) {
				// 		std::cout << it << " ";
				// 	}
				// }
				placed = 0;
				root = -1;
				std::set<int> candidate_set;
				for (int j = 1; j < n + 1; j++) {
					if (ancestor_count[j] != -1 && decestor_count[j] != -1) {
						// 1. ustawiam ich w linii jeden pod drugim
						if (ancestor_count[j] == 0 && decestor_count[j] == 0) {
							root = j;
						}
						// bede probowac ustawic kolo siebie
						// ale oni nie moga byc przodkami co najmniej 1
						if (ancestor_count[j] == 0 && decestor_count[j] > 0) {
							candidate_set.insert(j);
						}
					}
				}
					if (root > 0) {
						// mogę ustawić to i candidates
						position[root] = level;
						placed++;
						for (int k = 1; k < n + 1; k++) {
							ancestor[k].erase(root); // juz jest przodkiem
							decestor_count[k] = decestor[k].size();
							decestor[k].erase(root); // juz nie bedzie synem
							ancestor_count[k] = ancestor[k].size();
						}
						ancestor[root].clear();
						decestor[root].clear();
						ancestor_count[root] = -1;
						decestor_count[root] = -1;
						for (auto it : candidate_set) {
							position[it] = level;
							placed++;
							for (int k = 1; k < n + 1; k++) {
								ancestor[k].erase(it); // juz jest przodkiem
								decestor_count[k] = decestor[k].size();
								decestor[k].erase(it); // juz nie bedzie synem
								ancestor_count[k] = ancestor[k].size();
								ancestor[k].clear();
								decestor[k].clear();

								ancestor_count[k] = -1;
								decestor_count[k] = -1;
							}
						}
					} else {
						for (auto it : candidate_set) {
							for (int k = 1; k < n + 1; k++) {
								ancestor[k].erase(it); // juz jest przodkiem
								decestor_count[k] = decestor[k].size();
								decestor[k].erase(it); // juz nie bedzie synem
								ancestor_count[k] = ancestor[k].size();
								ancestor[k].clear();
								decestor[k].clear();
								ancestor_count[k] = 0;
								decestor_count[k] = 0;
							}
						}
						for (auto it : candidate_set) {
							if (!(ancestor_count[it] == 0 && decestor_count[it] == 0)) {
								placed = 0;
							} else {
								position[it] = level;
								placed++;
								ancestor_count[it] = -1;
								decestor_count[it] = -1;
							}
						}
					}
				has_more -= placed;
				if (placed == 0) {
					//TODO ŹLE
					has_more = 0;
				}
			}
			if (placed == 0 && has_more == 0) {
				std::cout << "NIE\n";
			} else {
				std::string in_return = "";
				for (int i = 1; i < n + 1; i++) {
					in_return += std::to_string(position[i]) + "\n";
				}
				std::cout  << in_return;
			}
		} else {
			std::cout << "0\n";
		}
	} else {
		std::cout << "NIE\n";
	}
}