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

using namespace std;

stringstream out;

int n, m;

set<int> prefersTColumn[1000];
set<int> prefersTRow[1000];
set<int> prefersNColumn[1000];
set<int> prefersNRow[1000];

char allPrefers[1000][1000];
int roots[1000];
vector<int> toRemove;
set<int> leftNumbers;

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

	cin >> n >> m;

	for (int i = 0; i < n; ++i)
	{
		leftNumbers.insert(i);
		roots[i] = -1;
		for (int j = 0; j < n; ++j)
			allPrefers[i][j] = 'o';
	}

	for (int i = 0; i < m; ++i)
	{
		int a, b;
		char c;
		cin >> a >> b >> c;
		a--;
		b--;
		allPrefers[a][b] = c;
		if (c == 'T')
		{
			prefersTColumn[b].insert(a);
			prefersTRow[a].insert(b);
		}
		else
		{
			prefersNColumn[b].insert(a);
			prefersNRow[a].insert(b);		
		}
	}
	
	bool settingRoot = true;
	int currentRoot = 0;
	while (!toRemove.empty() || settingRoot)
	{
		if (!settingRoot)
		{
			int valueToRemove = toRemove.back();
			toRemove.pop_back();
			currentRoot = valueToRemove + 1;
			for (auto it = prefersNColumn[valueToRemove].begin(); it != prefersNColumn[valueToRemove].end(); ++it)
			{
				prefersNRow[*it].erase(valueToRemove);
			}
			prefersNColumn[valueToRemove].clear();

			for (auto it = prefersTColumn[valueToRemove].begin(); it != prefersTColumn[valueToRemove].end(); ++it)
			{
				prefersTRow[*it].erase(valueToRemove);
			}
			prefersTColumn[valueToRemove].clear();

			for (auto it = prefersNRow[valueToRemove].begin(); it != prefersNRow[valueToRemove].end(); ++it)
			{
				prefersNColumn[*it].erase(valueToRemove);
			}
			prefersNRow[valueToRemove].clear();

			for (auto it = prefersTRow[valueToRemove].begin(); it != prefersTRow[valueToRemove].end(); ++it)
			{
				prefersTColumn[*it].erase(valueToRemove);
			}
			prefersTRow[valueToRemove].clear();
		}

		for (auto numberIterator = leftNumbers.begin(); numberIterator != leftNumbers.end(); ++numberIterator)
		{
			int currentNumber = *numberIterator;
			if (prefersTRow[currentNumber].empty())
			{
				if (prefersNColumn[currentNumber].empty())
				{
					toRemove.push_back(currentNumber);
					roots[currentNumber] = currentRoot;
					leftNumbers.erase(numberIterator);
					break;
				}
				else if (!settingRoot)
				{
					bool possible = true;
					for (auto it = prefersNColumn[currentNumber].begin(); it != prefersNColumn[currentNumber].end(); ++it)
					{
						//if (prefersTRow[*it].size() == 1 && prefersNColumn[*(prefersTRow[*it].begin())].find
						if (!prefersTRow[*it].empty())
						{
							possible = false;
							break;
						}
					}

					for (auto it = prefersTColumn[currentNumber].begin(); it != prefersTColumn[currentNumber].end() && possible; ++it)
					{
						for (auto it2 = prefersNColumn[currentNumber].begin(); it2 != prefersNColumn[currentNumber].end(); ++it2)
						{
							if (allPrefers[*it][*it2] == 'T')
							{
								possible = false;
								break;
							}
						}
					}

					if (possible)
					{
						toRemove.push_back(currentNumber);
						numberIterator = leftNumbers.erase(numberIterator);
						roots[currentNumber] = currentRoot;
						for (auto it = prefersNColumn[currentNumber].begin(); it != prefersNColumn[currentNumber].end(); ++it)
						{
							toRemove.push_back(*it);
							roots[*it] = currentRoot;
							leftNumbers.erase(*it);
						}
						break;
					}
				}
			}
		}
		settingRoot = false;
	}
	bool possible = true;
	for (int i = 0; i < n; ++i)
	{
		if (roots[i] == -1)
		{
			possible = false;
			break;
		}
		out << roots[i] << endl;
	}

	if (!possible)
		cout << "NIE\n";
	else
		cout << out.str();
	return 0;
}