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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

public class reo {

	public static final int SUPERVISOR_NOT_YET_DETERMINED = -2;
	public static final int SUPERVISOR_OF_MAIN_DIRECTOR = -1;
	public static final int NO_DIRECTOR_CANDIDATE = -100;
	public static final int DIRECTOR_NOT_YET_DETERMINED = -101;

	private final int workersCount;
	private final int relationsCount;

	private int[] supervisors;
	private GraphVertex[] workerVertices;

	private class GraphVertex {

		final int workerNumber;

		List<Integer> greenArrowsTo;
		List<Integer> greenArrowsFrom;

		List<Integer> redArrowsFrom;
		int countOfRedArrowsTo;

		GraphVertex(int workerNumber) {
			this.workerNumber = workerNumber;
			this.greenArrowsFrom = new LinkedList<Integer>();
			this.greenArrowsTo = new LinkedList<Integer>();
			this.redArrowsFrom = new LinkedList<Integer>();
			this.countOfRedArrowsTo = 0;
		}
	}

	private class Graph {

		private Set<Integer> vertices;
		private int supervisor;
		private int director;;

		Graph(Collection<Integer> vertices, int supervisor) {
			this.vertices = new LinkedHashSet<Integer>(2 * vertices.size());
			this.vertices.addAll(vertices);
			this.supervisor = supervisor;
			this.director = DIRECTOR_NOT_YET_DETERMINED;
		}

		List<Graph> processGraph() {
			int director = this.getDirector();
			if (director == NO_DIRECTOR_CANDIDATE)
				return null;
			supervisors[director] = this.supervisor;
			removeAllGreenArrowsTo(director);
			vertices.remove(director);
			if (!vertices.isEmpty())
				return this.componentsAfterRemovingDirector(director);
			else
				return new LinkedList<Graph>();
		}

		int getDirector() {
			if (this.director == DIRECTOR_NOT_YET_DETERMINED) {
				return (this.director = this.findDirectorCandidate());
			} else {
				return this.director;
			}
		}

		private int findDirectorCandidate() {
			for (Integer i : this.vertices) {
				GraphVertex vertex = workerVertices[i];
				if (vertex.countOfRedArrowsTo == 0 && vertex.greenArrowsFrom.size() == 0) {
					return vertex.workerNumber;
				}
			}
			return NO_DIRECTOR_CANDIDATE;
		}

		private List<Graph> componentsAfterRemovingDirector(int newDirector) {
			List<Graph> result = new LinkedList<Graph>();
			int[] visited = new int[workersCount];
			int componentsCounter = 0;
			for (Integer k : this.vertices) {
				GraphVertex v = workerVertices[k];
				if (visited[v.workerNumber] == 0) {
					++componentsCounter;
					List<Integer> component = new LinkedList<Integer>();
					List<GraphVertex> queue = new LinkedList<GraphVertex>();
					queue.add(v);
					while (!queue.isEmpty()) {
						GraphVertex head = queue.remove(0);
						if (visited[head.workerNumber] == 0) {
							component.add(head.workerNumber);
							for (Integer i : head.greenArrowsFrom) {
								if (visited[i] == 0)
									queue.add(workerVertices[i]);
							}
							for (Integer i : head.greenArrowsTo) {
								if (visited[i] == 0)
									queue.add(workerVertices[i]);
							}
							visited[head.workerNumber] = componentsCounter;
						}
					}
					for (Integer i : component) {
						workerVertices[i].countOfRedArrowsTo = 0;
					}
					for (Integer i : component) {
						Iterator<Integer> ite = workerVertices[i].redArrowsFrom.iterator();
						int worker;
						while (ite.hasNext()) {
							worker = ite.next();
							if (visited[worker] == componentsCounter)
								++workerVertices[worker].countOfRedArrowsTo;
							else
								ite.remove();
						}
					}
					result.add(new Graph(component, this.getDirector()));
				}
			}
			return result;
		}
	}

	public static void main(String[] args) throws IOException {

		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		reo r = new reo(reader);
		boolean succes = r.createTree();
		if (succes) {
			r.printSupervisors();
		} else {
			System.out.println("NIE");
		}
	}

	private boolean createTree() {
		List<Integer> allWorkers = new ArrayList<Integer>(workersCount);
		for (int j = 0; j < workersCount; ++j)
			allWorkers.add(j);
		Graph wholeGraph = new Graph(allWorkers, SUPERVISOR_OF_MAIN_DIRECTOR);
		List<Graph> graphsToProcess = new LinkedList<Graph>();

		int countOfDeterminedSupervisors = 0;
		graphsToProcess.add(wholeGraph);
		while (!graphsToProcess.isEmpty()) {
			Graph nextGraph = graphsToProcess.remove(0);
			List<Graph> components = nextGraph.processGraph();
			++countOfDeterminedSupervisors;
			if (components == null) {
				return false;
			}
			graphsToProcess.addAll(components);
		}
		if (countOfDeterminedSupervisors == workersCount) {
			return true;
		} else {
			return false;
		}
	}

	private void readRelations(BufferedReader reader, int relationsCount) throws IOException {
		int workerA, workerB;
		char relation;
		StringTokenizer st = null;
		for (int j = 0; j < relationsCount; ++j) {
			st = new StringTokenizer(reader.readLine());
			workerA = Integer.parseInt(st.nextToken()) - 1;
			workerB = Integer.parseInt(st.nextToken()) - 1;
			relation = st.nextToken().charAt(0);

			if (relation == 'T') {
				workerVertices[workerB].greenArrowsTo.add(workerA);
				workerVertices[workerA].greenArrowsFrom.add(workerB);
			} else {
				workerVertices[workerA].redArrowsFrom.add(workerB);
				++workerVertices[workerB].countOfRedArrowsTo;
			}
		}
	}

	private void removeAllGreenArrowsTo(int worker) {
		for (Integer i : workerVertices[worker].greenArrowsTo) {
			workerVertices[i].greenArrowsFrom.remove(new Integer(worker));
		}
		workerVertices[worker].greenArrowsTo.clear();
	}

	private reo(BufferedReader reader) throws IOException {
		StringTokenizer st = new StringTokenizer(reader.readLine());
		workersCount = Integer.parseInt(st.nextToken());
		relationsCount = Integer.parseInt(st.nextToken());
		workerVertices = new GraphVertex[workersCount];
		supervisors = new int[workersCount];
		for (int j = 0; j < workersCount; ++j) {
			supervisors[j] = SUPERVISOR_NOT_YET_DETERMINED;
			workerVertices[j] = new GraphVertex(j);
		}
		readRelations(reader, relationsCount);
	}

	private void printSupervisors() {
		for (int j = 0; j < workersCount; ++j)
			System.out.println(supervisors[j] + 1);
	}

}