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
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define VAR(v,w) __typeof(w) v=(w)
#define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it)
#define PB push_back

typedef vector<int> VI;
typedef vector<VI> VVI;

struct E {
	int a, b;
	char c;
};

char rel[1000][1000];
VVI g;
int need[1000], bad[1000];
int boss[1000];

void findTeam(int i, VI& team) {
	queue<int> q;
	boss[i] = -3;
	q.push(i);
	team.PB(i);
	while (!q.empty()) {
		int j = q.front();
		q.pop();
		FORE(k,g[j]) if (boss[*k] == -2) {
			boss[*k] = -3;
			q.push(*k);
			team.PB(*k);
		}
	}
}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	g.resize(n);
	vector<E> edges;
	edges.reserve(m);
	REP(j,m) {
		E e;
		scanf("%d%d %c", &e.a, &e.b, &e.c);
		--e.a;
		--e.b;
		rel[e.a][e.b] = e.c;
		if (e.c == 'T' && rel[e.b][e.a] != 'T') {
			g[e.a].PB(e.b);
			g[e.b].PB(e.a);
		}
		edges.PB(e);
	}
	FORE(e,edges) {
		if (e->c == 'T') ++need[e->a];
		else ++bad[e->b];
	}
	int d = -1;
	REP(i,n) if (!need[i] && !bad[i]) {
		d = i;
		break;
	}
	if (d < 0) {
		printf("NIE\n");
		return 0;
	}
	REP(i,n) boss[i] = d;
	boss[d] = -1;
	queue<int> q;
	q.push(d);
	while (!q.empty()) {
		int b = q.front();
		q.pop();
		REP(i,n) if (boss[i] == b) boss[i] = -2;
		REP(i,n) if (boss[i] == -2) {
			VI team;
			findTeam(i, team);
			FORE(t,team) need[*t] = bad[*t] = 0;
			FORE(e,edges) if (boss[e->a] == -3 && boss[e->b] == -3) {
				if (e->c == 'T') ++need[e->a];
				else ++bad[e->b];
			}
			int nb = -1;
			FORE(t,team) if (!need[*t] && !bad[*t]) {
				nb = *t;
				break;
			}
			if (nb < 0) {
				printf("NIE\n");
				return 0;
			}
			FORE(t,team) boss[*t] = nb;
			boss[nb] = b;
			q.push(nb);
		}
	}
	REP(i,n) printf("%d\n", boss[i] + 1);
}