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
169
170
171
172
173
#include<cstdio>
#include<algorithm>
#include<utility>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>

// macros

#define FORE(c, a, b) for(int c=a; c<= int(b); ++c)
#define FORD(c, a, b) for(int c=a; c>= int(b); --c)
#define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define ALL(a) a.begin(), a.end() 
#define PR(n) printf("%d ", (int) (n)) 
#define PRL(n) printf("%lld ", (ll) (n)) 
#define SC(n) scanf("%d", &n);
#define SC2(n, m) scanf("%d%d", &n, &m);

#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define itr iterator

#define dbg if(0)
#define prd dbg printf

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;

typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mi;
typedef pair<int, int> pi;
typedef vector<pi> vii;
typedef vector<vi> vvi;

const int NMAX = 1<<10;

int n, m, k, x, y;
int parent[NMAX], active[NMAX], spoil[NMAX], fu[NMAX], siz[NMAX];
vi tak[NMAX], nie[NMAX];
char cc;

int findBoss(vi const &vec) {
  REP(i, n+1)
    active[i] = spoil[i] = 0;
  REP(i, vec.size())
    active[vec[i]] = 1;
  REP(i, vec.size()) {
    int v = vec[i];
    FORIT(it, tak[v], vi)
      if(active[*it]) spoil[v] = 1;
    FORIT(it, nie[v], vi)  
      if(active[*it]) spoil[*it] = 1;
  }
  int out = -1;
  REP(i, vec.size())
    if(spoil[vec[i]] == 0) out = i;
  return out;
}

void update(int x, int xx) {
  if(x == xx) return;
  update(fu[x], xx);
  fu[x] = xx;
}

int fuu(int x) {
  while(x != fu[x])
    x = fu[x];
  return x;
}

void connect(int x, int y) {
  prd("connect %d %d\n", x, y);
  int xx = fuu(x), yy = fuu(y);
  if(siz[xx] < siz[yy]) {
    int tmp = xx; xx = yy; yy = tmp;
  }
  fu[yy] = xx;
  update(x, xx);
  update(y, xx);
  if(xx != yy)
    siz[xx] += siz[yy];
}

vvi divide(vi const &vec) {
  prd("divide\n");
  REP(i, n+1) {
    active[i] = 0; siz[i] = 1; fu[i] = i;
  }
  REP(i, vec.size())
    active[vec[i]] = 1;
  REP(i, vec.size()) {
    int v = vec[i];
    FORIT(it, tak[v], vi)
      if(active[*it]) connect(v, *it);
  }
  
  mi repr;
  REP(i, vec.size()) {
    int v = vec[i];
    if(fuu(v) == v) repr[v] = repr.size() - 1;
  }
  prd("divide2\n");
  
  vvi groups;
  REP(i, repr.size()) {
    vi ve;
    groups.pb(ve);
  }
  REP(i, vec.size()) {
    int v = vec[i];
    prd("add %d %d %d\n", v, fuu(v), repr[fuu(v)]);
    groups[repr[fuu(v)]].pb(v);
  }
  return groups;
}   

void solve(vi &vec, int boss) {
  dbg {
    printf("solve ");
    FORIT(it, vec, vi) PR(*it);
    printf("(%d)\n", boss);
  }
  if(vec.size() == 1) {
    parent[vec[0]] = boss;
    return;
  }
  int candIndex = findBoss(vec);
  if(candIndex != -1) {
    int cand = vec[candIndex];
    vec.erase(vec.begin() + candIndex);
    parent[cand] = boss;
    solve(vec, cand);
    return;
  }
  vvi groups = divide(vec);
  if(groups.size() == 1 || boss == 0) {
    printf("NIE");
    exit(0);
  }
  REP(ig, groups.size())
    solve(groups[ig], boss);
}
int main() {
  SC2(n,m);
  REP(i, m) {
    scanf("%d%d%c%c", &x, &y, &cc, &cc);
    if(cc == 'T')
      tak[x].pb(y);
    else
      nie[x].pb(y);
  }
  vi ver;
  REP(i, n)
    ver.pb(i+1);
  solve(ver, 0);
  //printf("TAK");
  REP(i, n)
    printf("%d\n", parent[i+1]);
  return 0;
}