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
/* Copyright 2016 AcrossTheSky */
#include <iostream>
#include <cstdio>
#include <utility>
#include <cassert>
#include <map>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <sstream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define PER(i, a, b) for (int i = (a); i >= (b); --i)
#define RVC(i, c) fot (int i = 0; i < (c).size(); ++i)
#define RED(k, u) for (int k = head[(u)]; k; k = edge[k].next)
#define lowbit(x) ((x) & (-(x)))
#define CL(x, v) memset(x, v, sizeof x)
#define MP std::make_pair
#define PB push_back
#define FR first
#define SC second
#define rank rankk
#define next nextt
#define link linkk
#define index indexx
#define abs(x) ((x) > 0 ? (x) : (-(x)))
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

template<class T> inline
bool getmin(T *a, const T &b) {
    if (b < *a) {
        *a = b;
        return true;
    }
    return false;
}

template<class T> inline
bool getmax(T *a, const T &b) {
    if (b > *a) {
        *a = b;
        return true;
    }
    return false;
}

template<class T> inline
void read(T *a) {
    char c;
    while (isspace(c = getchar())) {}
    bool flag = 0;
    if (c == '-') flag = 1, *a = 0;
    else
        *a = c - 48;
    while (isdigit(c = getchar())) *a = *a * 10 + c - 48;
    if (flag) *a = -*a;
}
const int mo = 1000000007;
template<class T>
T pow(T a, T b, int c = mo) {
    T res = 1;
    for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c) if (b & i) res = 1LL * res * a % c;
    return res;
}
/*======================= TEMPLATE =======================*/
#include <bitset>
const int N = 1010;
bitset<N> vis[N];
bitset<N> flag[N];
int n, m, tot;
PII e[110000];
int head[N], cnt;
int f[N], pre[N];
struct node {
    int p, next;
}edge[110000];

void ae(int a, int b) {
    edge[++cnt].p = b;
    edge[cnt].next = head[a];
    head[a] = cnt;
}
int deg[N];
bool ideg[N];
bool isfather(int a, int b) {
    while (a) {
        a = f[a];
        if (a == b) return true;
    }
    return false;
}
int fa[N];
int get(int u) {
    if (u == fa[u]) return u;
    return fa[u] = get(fa[u]);
}
int join(int u, int v) {
    fa[get(u)] = get(v);
}
vector<int> v[N];
int main() {
    cin >> n >> m;
    REP(i, 1, n) deg[i] = 0, ideg[i] = 0;
    CL(head, 0); cnt = 0;
    tot = 0;
    while (m --) {
        int x, y; char c;
        read(&y); read(&x); while (isspace(c = getchar()));
        if (c == 'T') ae(x, y), ++deg[y];
        else e[++tot] = MP(x, y), ideg[x] = 1, v[x].PB(y);
    }
    CL(f, 0xff);
    int r = 0;
    REP(i, 1, n) if (!deg[i] && !ideg[i]) 
        r = i;
    if (!r) {
        puts("NIE");
        return 0;
    }
    f[r] = 0;
    REP(i, 1, n) pre[i] = r;
    RED(k, r) deg[edge[k].p]--;
    bitset<N> B;
    REP(tt, 1, n - 1) {
        B.reset();
        int p = 0;
        REP(i, 1, n) fa[i] = i;
        REP(i, 1, n) if (f[i] == -1) 
            RED(k, i) if (f[edge[k].p] == -1) join(i, edge[k].p);
        REP(i, 1, n) fa[i] = get(i);

        REP(i, 1, n) if (f[i] == -1 && !deg[i]){
            for (int j = 0; j < v[i].size(); ++j) {
                int k = v[i][j];
                if (f[k] == -1) 
                if (fa[k] == fa[i]) {
                    B[i] = 1; break;
                }
            }
        }


        REP(i, 1, n) if (f[i] == -1 && !B[i] && !deg[i]) p = i;
        if (!p) {
            puts("NIE");
            return 0;
        }
        f[p] = pre[p];
        int x = get(p);
        REP(i, 1, n) if (f[i] == -1 && x == get(i)) {
            pre[i] = p;
        }
        RED(k, p) deg[edge[k].p]--;
    }
    REP(i, 1, n) printf("%d\n", f[i]);
    return 0;
}