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
#include <algorithm>
#include <iostream>
#include <vector>

class DisjointSetStructure {
	struct Element {
		int parent;
		int depth;
	};
	Element* elements;
public:
	DisjointSetStructure(size_t n) {
		elements = new Element[n];
		while (n--) {
			elements[n].parent = n;
			elements[n].depth = 0;
		}
	}
	~DisjointSetStructure() {
		delete[] elements;
	}
	int Find(int index) {
		int& parent = elements[index].parent;
		if (parent != index)
			parent = Find(parent);
		return parent;
	}
	void Union(int index1, int index2) {
		int root1index = Find(index1);
		int root2index = Find(index2);
		if (root1index != root2index) {
			Element& root1 = elements[root1index];
			Element& root2 = elements[root2index];
			if (root1.depth < root2.depth) {
				root1.parent = root2index;
			} else {
				root1.depth += root1.depth == root2.depth;
				root2.parent = root1index;
			}
		}
	}
};

class Solver {
	std::vector<std::vector<int>> relT, relN;
	std::vector<int> parents;
	int total;
	bool fail;
public:
	Solver(int n) : relT(n), relN(n), parents(n), total(n), fail(false) {}
	void Load(int m) {
		while (m--) {
			int a, b;
			char c;
			std::cin >> a >> b >> c;
			a--;
			b--;
			if (c == 'T')
				relT[a].push_back(b);
			else
				relN[b].push_back(a);
		}
	}
	void Solve(const std::vector<int>& nodes, int parent) {
		std::vector<int>::const_iterator root;
		{
			std::vector<int8_t> inNodes(total);
			for (const auto& node : nodes) {
				inNodes[node] = true;
			}
			auto inNodesFunc = [&](int other) {
				return inNodes[other];
			};
			root = std::find_if(nodes.begin(), nodes.end(), [&](int node) {
				return std::none_of(relT[node].begin(), relT[node].end(), inNodesFunc)
					&& std::none_of(relN[node].begin(), relN[node].end(), inNodesFunc);
			});
		}
		if (root == nodes.end()) {
			fail = true;
		} else {
			int rootNode = *root;
			parents[rootNode] = parent;
			std::vector<std::vector<int>> sets(total);
			{
				DisjointSetStructure dss(total);
				for (const auto& node : nodes) {
					if (node != rootNode) {
						for (const auto& other : relT[node]) {
							dss.Union(node, other);
						}
					}
				}
				for (const auto& node : nodes) {
					if (node != rootNode)
						sets[dss.Find(node)].push_back(node);
				}
			}
			for (const auto& s : sets) {
				if (!s.empty()) {
					Solve(s, rootNode);
					if (fail)
						break;
				}
			}
		}
	}
	void Write() const {
		if (fail) {
			std::cout << "NIE\n";
		} else {
			for (const auto& parent : parents) {
				std::cout << (parent + 1) << '\n';
			}
		}
	}
};

int main() {
	std::ios_base::sync_with_stdio(false);
	int n, m;
	std::cin >> n >> m;
	Solver solver(n);
	solver.Load(m);
	std::vector<int> allNodes(n);
	for (int i = n; i--;) {
		allNodes[i] = i;
	}
	solver.Solve(allNodes, -1);
	solver.Write();
    return 0;
}