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<queue>
#include<list>
#include<stdio.h>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define ull unsigned long long int
#define FOR(i,n) for(int i=0;i<n;++i)
#define FORD(i,n) for(int i=(n)-1;i>=0;--i)
#define znakow 26
#define dcout 0 && cout
#define INF 9999999
int inline min(int a,int b){return a>b?b:a;}
int inline max(int a,int b){return a<b?b:a;}
#define RED 1
#define GREEN 2
#define REMOVED -553434
class E{
	public: int other, color, points;
	E(int other, int color, int points){
		this->other=other;
		this->color=color;
		this->points=points;
	}
};

void crackTree(int*wantsThatManyAsBoss, int*isRejectedByThatMany, set<int>*graph, vector<E*>*edges, int root, int n, int*parents);
int chooseRoot(set<int>*graph, int*wantsThatManyAsBoss, int*isRejectedByThatMany){
	dcout<<"wants [";FOR(i,10)dcout<<wantsThatManyAsBoss[i]<<", ";dcout<<"]\n";
	dcout<<"isRej [";FOR(i,10)dcout<<isRejectedByThatMany[i]<<", ";dcout<<"]\n";
	for (set<int>::iterator it = graph->begin() ; it != graph->end(); ++it){
		if(wantsThatManyAsBoss[*it]==0 && isRejectedByThatMany[*it]==0){
			dcout<<*it<<" chosen as Root\n";
			return *it;
		}
	}
	dcout<<"NO ROOT!"<<endl;
	return -1;
}
int NIE=0;
void removeRoot(int*wantsThatManyAsBoss, int*isRejectedByThatMany, int root, vector<E*>*edges, int*parents, int lastParent, set<int>*graph){
	FOR(i,edges[root].size()){//for(iterator<E*> it=edges[root].begin();it!=edges[root].end();++it){
		if(graph->count(edges[root][i]->other)==0)continue;
		dcout<<"Thinking about removing an edge from "<<root<<" to "<<edges[root][i]->other<<", color "<<edges[root][i]->color<<", points="<<edges[root][i]->points<<endl;
		if(edges[root][i]->color==RED){
			--isRejectedByThatMany[edges[root][i]->other];
		}else{//pointed at
			if(edges[root][i]->points==1){
				--wantsThatManyAsBoss[edges[root][i]->other];
				dcout<<"removing green edge to root="<<root<<" from "<<edges[root][i]->other<<endl;
			}
		}
	}
	isRejectedByThatMany[root]=REMOVED;
	wantsThatManyAsBoss[root]=REMOVED;
	dcout<<"removing root "<<root<<" from graph "<<graph->count(root)<<"\n";
	graph->erase(root);//graph->find(root));
	parents[root]=lastParent;
}

void partition(int*wantsThatManyAsBoss, int*isRejectedByThatMany, set<int>*graph, vector<E*>*edges, int root, int n, int*parents){
	int taken[n];
	FOR(i,n)taken[i]=0;

	vector<set<int>*> parts;
	for (set<int>::iterator it = graph->begin() ; it != graph->end(); ++it){
		int i=*it;
		if(taken[i]==0){
			set<int>*subGraph=new set<int>();
			subGraph->insert(i);
			vector<E*> redEdges;
			queue<int> q;
			q.push(i);
			taken[i]=1;
			while(!q.empty()){
				int node=q.front();
				q.pop();
				if(1){
					FOR(h,edges[node].size()){
					//for(vector<E*>::iterator e=edges[node].begin();e!=edges[node].end();++e){
						if(edges[node][h]->color==GREEN){dcout<<"Evaluating green edge of node "<<node<<": "<<h<<endl;
							int other = edges[node][h]->other;
							if(!taken[other] && graph->count(other)){
								q.push(other);
								taken[other]=1;
								subGraph->insert(other);
							}
						}else{
							redEdges.push_back(edges[node][h]);
						}
					}
				}
			}
			//remove red edges
			//for (vector<E*>::iterator re = redEdges.begin() ; re != redEdges.end(); ++re){
			FOR(j,redEdges.size()){
				if(subGraph->count(redEdges[j]->other)==0 && graph->count(redEdges[j]->other)!=0){
					--isRejectedByThatMany[redEdges[j]->other];
				}
			}
			dcout<<"Found a set [";for(set<int>::iterator itt = subGraph->begin() ; itt != subGraph->end(); ++itt)dcout<<*itt<<", ";dcout<<"]\n";
			parts.push_back(subGraph);
		}
	}
	dcout<<"Found "<<parts.size()<<" disjoint sets"<<endl;
	FOR(i,parts.size()){
		crackTree(wantsThatManyAsBoss, isRejectedByThatMany, parts[i], edges, root, n, parents);
		delete parts[i];
		if(NIE)break;
	}
}

void crackTree(int*wantsThatManyAsBoss, int*isRejectedByThatMany, set<int>*graph, vector<E*>*edges, int root, int n, int*parents){
	if(graph->size()==1){
		int newRoot=*(graph->begin());
		removeRoot(wantsThatManyAsBoss,isRejectedByThatMany,newRoot, edges, parents, root, graph );
		dcout<<newRoot<<" was alone in the set thus chosen as a root\n";
		return;
	}
	int newRoot = chooseRoot(graph, wantsThatManyAsBoss, isRejectedByThatMany);
	if(newRoot==-1){
		NIE=1;
		return;
	}
	removeRoot(wantsThatManyAsBoss, isRejectedByThatMany, newRoot, edges, parents, root, graph);
	partition(wantsThatManyAsBoss, isRejectedByThatMany, graph, edges, newRoot, n, parents);
}
int main(){
	std::ios::sync_with_stdio(false);
	int n,m;
	cin>>n>>m;
	vector<E*>*edges=new vector<E*>[n];
	int*parents=new int[n];
	int*wantsThatManyAsBoss=new int[n];
	int*isRejectedByThatMany=new int[n];
	FOR(i,n)wantsThatManyAsBoss[i]=isRejectedByThatMany[i]=0;
	FOR(i,m){
		int a,b;
		char t,t2;
		cin>>a>>b>>t;
		--a;--b;
		if(t=='T'){
			edges[a].push_back(new E(b,GREEN,0));
			edges[b].push_back(new E(a,GREEN,1));
			++wantsThatManyAsBoss[a];
		}else{
			edges[a].push_back(new E(b,RED,0));
			++isRejectedByThatMany[b];
		}
	}
	int lastParent=-1;
	int previousRoot=-1;
	set<int>*graph=new set<int>;
	FOR(i,n)graph->insert(i);
	crackTree(wantsThatManyAsBoss, isRejectedByThatMany, graph, edges, previousRoot, n, parents);

	if(NIE){
		cout<<"NIE"<<endl;
	}else{
		//cout<<"TAK"<<endl;return 0;
		FOR(i,n)cout<<(parents[i]+1)<<" ";
		cout<<endl;
	}
	
    return 0;
}