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 <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
//#define REP(i,n) FOR(i,1,(n)+1)
typedef vector<int> vi;
#define pb push_back
#define sz size()
typedef pair<int,int> pii;
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
#define INF 1000000001
//#define VAR(n,v) typeof(v) n=(v)
#define ALL(t) t.begin(),t.end()
#define SC(a) scanf("%d", &a)
#define GET(a) int a; SC(a)

#define ISDEBUG 0
#define dprintf(...) if(ISDEBUG) \
{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class It> void dptab(It b, It e, const char* f="%d ") {
	if(ISDEBUG) {
		for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n");
}}

enum EdgeDirection { INCOME, OUTCOME };
struct Edge {
	int v;
	EdgeDirection direction;
	explicit Edge(int v, EdgeDirection d) : v(v), direction(d) {}
	bool operator<(const Edge &e) const {
		if(v != e.v) return v < e.v;
		else return direction < e.direction;
	}
};

set<Edge> graph_positives[1000];
set<Edge> graph_negatives[1000];
int parent[1000];
int income_negatives[1000];
int outcome_positives[1000];
int n;

void dfs(int vertex, const set<int> &vertices, vector<bool> &visited, set<int> &subtree) {
	visited[vertex] = true;
	subtree.insert(vertex);
	for(const auto &edge : graph_positives[vertex]) {
		if(!visited[edge.v] && vertices.count(edge.v)) {
			dfs(edge.v, vertices, visited, subtree);
		}
	}
}

bool crawl_graph(int director, const set<int> &vertices) {
	if(vertices.empty()) {
		return true;
	}

	dprintf("\nvertices: ");
	dptab(ALL(vertices));
	int new_director = -1;
	for(auto const &vertex : vertices) {
		if(0 == income_negatives[vertex]
				&& 0 == outcome_positives[vertex]) {
			new_director = vertex;
			break;
		}
	}
	if(-1 == new_director) {
		dprintf("FALSE!\n");
		for(auto const &vertex : vertices) {
			dprintf("  income_neg[%d] = %d  ->  ", vertex, income_negatives[vertex]);
			dptab(ALL(graph_negatives[vertex]));
			dprintf("  outcome_pos[%d] = %d\n", vertex, outcome_positives[vertex]);
		}
		return false;
	}
	parent[new_director] = director;

	dprintf("new director: %d\n", new_director);
	for(auto const &edge : graph_positives[new_director]) {
		dprintf("erasing: %d\n", edge.v);
		assert(edge.direction == INCOME);
		graph_positives[edge.v].erase(Edge(new_director, OUTCOME));
		--outcome_positives[edge.v];

	}
	//graph_positives[new_director].clear(); //TODO: remove

	vector<bool> visited(n, 0);
	for(auto const &vertex : vertices) {
		if(!visited[vertex] && vertex != new_director) {
			set<int> subtree;
			dprintf("dfs:");
			dfs(vertex, vertices, visited, subtree);
			dprintf("\n");
			dprintf("subtree: ");
			dptab(ALL(subtree));

			for(auto const &subtree_vertex : subtree) {
				set<Edge> updated_negatives;
				for(auto const &edge : graph_negatives[subtree_vertex]) {
					if(subtree.count(edge.v)) {
						updated_negatives.insert(edge);
					} else {
						if(edge.direction == INCOME) {
							graph_negatives[edge.v].erase(Edge(subtree_vertex, OUTCOME));
							--income_negatives[subtree_vertex];
						} else {
							graph_negatives[edge.v].erase(Edge(subtree_vertex, INCOME));
							--income_negatives[edge.v];
						}
					}
				}
				graph_negatives[subtree_vertex] = updated_negatives;
			}

			bool result = crawl_graph(new_director, subtree);
			if(!result) {
				return false;
			}
		}
	}

	return true;
}

int main() {
	SC(n);
	GET(m);
	FOR(i,0,n) {
		parent[i] = -1;
	}
	while(m--) {
		int u, v;
		char c;
		scanf("%d %d %c", &u, &v, &c);
		--u, --v;
		dprintf("%d %d %c\n", u, v, c);
		if ('T' == c) {
			graph_positives[u].insert(Edge(v, OUTCOME));
			graph_positives[v].insert(Edge(u, INCOME));
			++outcome_positives[u];
		} else {
			graph_negatives[u].insert(Edge(v, OUTCOME));
			graph_negatives[v].insert(Edge(u, INCOME));
			++income_negatives[v];
		}
	}
	set<int> vertices;
	FOR(i,0,n) {
		vertices.insert(i);
	}
	bool result = crawl_graph(-1, vertices);
	if(!result) {
		printf("NIE\n");
	} else {
		if(ISDEBUG) {
			printf("TAK\n");
		} else {
			FOR(i,0,n) {
				printf("%d\n", parent[i] + 1);
			}
		}
	}
	return 0;
}