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

using namespace std;

typedef vector<vector<int> > Edges;
typedef vector<int> Degrees;

const int MAXN = 1000;

Edges T(MAXN+1);
Edges T_rev(MAXN+1);
Edges N(MAXN+1);
Degrees out_T(MAXN+1);
Degrees in_N(MAXN+1);
vector<int> parents(MAXN+1);

int n, m;

void readData()
{
	cin >> n >> m;
	
	for (int i = 0; i < m; i++)
	{
		int a, b;
		string c;
		cin >> a >> b >> c;
		if (c == "T")
		{
			T[a].push_back(b);
			out_T[a]++;
			T_rev[b].push_back(a);
		}
		else
		{
			N[a].push_back(b);
			in_N[b]++;
		}
	}
}	

void removeNode(int v, Edges & e, Degrees & d)
{
	for (int i = 0; i < e[v].size(); i++)
	{
		int w = e[v][i];
		d[w]--;
	}
	e[v].clear();
}

int findRoot(vector<int> const & vs)
{
	for (int i = 0; i < vs.size(); i++)
	{
		int v = vs[i];
		if (out_T[v] == 0 && in_N[v] == 0)
			return v;
	}

	return 0;
}

void dfs(int v, int comp_idx, vector<int> & comp, vector<int> & comp_idxs)
{
	comp_idxs[v] = comp_idx;
	comp.push_back(v);
	for (int i = 0; i < T[v].size(); i++)
	{
		int w = T[v][i];
		if (comp_idxs[w] == 0)
			dfs(w, comp_idx, comp, comp_idxs);
	} 
	for (int i = 0; i < T_rev[v].size(); i++)
	{
		int w = T_rev[v][i];
		if (comp_idxs[w] == 0)
			dfs(w, comp_idx, comp, comp_idxs);
	} 
}

void removeEdgesAcrossComps(vector<int> const & vs, vector<int> & comp_idxs)
{
	for (int i = 0; i < vs.size(); i++)
	{
		int v = vs[i];
		int j = 0;
		while (j < N[v].size())
		{
			int w = N[v][j];
			if (comp_idxs[v] != comp_idxs[w])
			{
				N[v][j] = N[v].back();
				N[v].pop_back();
				in_N[w]--;
			}
			else
				j++;
		}
	}
}

int process(vector<int> const & vs)
{
	int root = findRoot(vs);
	if (root == 0)
		return 0;
	
	// "remove" T edges
	for (int i = 0; i < T_rev[root].size(); i++)
	{
		int w = T_rev[root][i];
		out_T[w]--;
	}

	// initialize comp_idxs	
	vector<int> comp_idxs(MAXN+1, -1);
	for (int i = 0; i < vs.size(); i++)
	{
		int v = vs[i];
		if (v != root)
			comp_idxs[v] = 0;
	}
	
	// compute connected components in T
	vector<vector<int> > comps;
	for (int i = 0; i < vs.size(); i++)
	{
		int v = vs[i];
		if (comp_idxs[v] == 0)
		{
			comps.push_back(vector<int>());
			dfs(v, comps.size(), comps.back(), comp_idxs);
		}
	}
	
	removeEdgesAcrossComps(vs, comp_idxs);

	// process each component separately
	for (int i = 0; i < comps.size(); i++)
	{
		int r = process(comps[i]);
		if (r == 0)
			return 0;
		parents[r] = root;
	}
	
	return root;
}

int main()
{
	readData();
	
	vector<int> vs(n);
	for (int i = 0; i < n; i++)
		vs[i] = i+1;
	
	int root = process(vs);
	if (root == 0)
		cout << "NIE\n";
	else
		for (int v = 1; v <= n; v++)
			cout << parents[v] << "\n";
	
	return 0;
}