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
// if you don't like "internet" being used as a non-singleton, then oh boy are you cheezed after reading THIS code huh??
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cstring>
#include <cassert>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define rep(i,a,n) for (int i=a;i<(n);i++)
#define per(i,a,n) for (int i=(n)-1;i>=(a);i--)
template<typename T> ostream& operator<<(ostream& s, vector<T> t) {rep(i, 0, t.size()) s << (i ? " " : "") << t[i]; return s;}
template<typename T> istream& operator>>(istream& s, vector<T> &t) {rep(i, 0, t.size()) s >> t[i]; return s;}
typedef long long ll;

const int N = 1234;
int n;

vector<int> above[N], below[N]; // must be above, cannot be below
vector<pair<int, int>> nie;
int res[N];

int root[N];
int getRoot(int x) {return (root[x] == x) ? x : (root[x] = getRoot(root[x]));}
void merge(int x, int y) {root[getRoot(x)] = root[getRoot(y)];}

void solve(unordered_set<int> &v, int father) {
    for (auto x : v) root[x] = x;
    //cout << "V: "; for (auto x : v) cout << x << " "; cout << endl;
    int r = -1;
    for (auto x : v) {
        bool good = true;
        rep(i, 0, below[x].size()) {
            if (v.count(below[x][i])) {
                good = false;
            } else {
                swap(below[x][i], below[x].back());
                below[x].pop_back();
                i--;
            }
        }
        if (good && !above[x].size()) {
            r = x;
            break;
        }
    }
    if (r == -1) cout << "NIE" << endl, exit(0);
    res[r] = father;
    for (auto x : v) {
        if (x == r) continue;
        rep(i, 0, above[x].size()) {
            if (above[x][i] == r) {
                swap(above[x][i], above[x].back());
                above[x].pop_back();
                i--;
            } else {
                merge(x, above[x][i]);
            }
        }
    }
    unordered_map<int, unordered_set<int>> m;
    for (auto x : v) {
        if (x == r) continue;
        m[getRoot(x)].insert(x);
    }
    for (auto &group : m) {
        solve(group.second, r);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    int m;
    cin >> n >> m;
    rep(i, 0, m) {
        int a, b;
        char c;
        cin >> a >> b >> c;
        a--, b--;
        if (c == 'T') above[a].push_back(b);
        else          below[b].push_back(a);
    }
    unordered_set<int> v;
    rep(i, 0, n) v.insert(i);
    solve(v, -1);
    rep(i, 0, n) cout << res[i] + 1 << endl;
}