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
#include <bits/stdc++.h>
#define pb push_back
using namespace std;

const int MN = 1005;

int del[MN], PC[MN], in[MN], rep[MN], ile[MN], parent[MN], pajac[MN];

vector <int> S[MN], N[MN];

void wywal(int x)
{
	del[x] = 1;
	int s = S[x].size();
	for(int i = 0; i < s; ++i)
		--in[S[x][i]];
}

int Find(int a)
{
	if(a == rep[a])
		return a;
	rep[a] = Find(rep[a]);
	return rep[a];
}

void Union(int x, int y)
{
	int c = Find(x);
	int d = Find(y);
	if(c!=d)
	{
		if(ile[c] <= ile[d])
		{
			ile[d] += ile[c];
			rep[c] = d;
		}
		else
		{
			ile[c] += ile[d];
			rep[d] = c;
		}
	}
}

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 0; i < m; ++i)
	{
		int a, b;
		char typ[3];
		scanf("%d%d%s", &a, &b, typ);
		if(typ[0] == 'T')
		{
			S[b].pb(a); //kolesie którzy lubią b
			++in[a];
		}
		else
		{
			N[b].pb(a); //kolesie którzy nie lubią b
		}
	}
	int root = 0;
	for(int i = 1; i <= n; ++i)
		if(!N[i].size() && !in[i])
			root = i;
	if(!root)
	{
		printf("NIE");
		return 0;
	}
	wywal(root);
	for(int i = 1; i <= n; ++i)
	{
		ile[i] = 1;
		rep[i] = i;
	}
	for(int i = 1; i <= n; ++i)
		PC[i] = root;
	int xddd = 1;
	while(xddd)
	{
		xddd = 0;
		for(int i = 1; i <= n; ++i)
		{
			if(!del[i])
			{
				xddd = 1;
				int s = S[i].size();
				for(int j = 0; j < s; ++j)
					if(!del[S[i][j]])
						Union(i, S[i][j]);
			}
		}
		for(int i = 1; i <= n; ++i)
		{
			if(del[i])
				continue;
			int a = Find(i);
			int s = N[i].size();
			int ok = 1;
			for(int j = 0; j < s; ++j)
				if(Find(N[i][j]) == a)
					ok = 0;
			if(!in[i] && ok)
				pajac[a] = i;
		}
		for(int i = 1; i <= n; ++i)
		{
			if(del[i])
				continue;
			int a = Find(i);
			if(parent[pajac[a]])
				continue;
			if(!pajac[a])
			{
				printf("NIE");
				return 0;
			}
			parent[pajac[a]] = PC[a];
			wywal(pajac[a]);
		}
		for(int i = 1; i <= n; ++i)
			PC[i] = pajac[Find(i)];
		for(int i = 1; i <= n; ++i)
		{
			ile[i] = 1;
			rep[i] = i;
			pajac[i] = 0;
		}
	}
	for(int i = 1; i <= n; ++i)
		printf("%d\n", parent[i]);
}