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
#include <iostream>
#include <vector>
using namespace std;

vector <int> v[1001], dob[1001], zle[1001], st; 
int d[1001];
int ojc[1001];
int tmp[100001];
int star[1001];
int szef[1001][1001];


int dfs(int i, int k) {
    d[i] = k;
    st.push_back(i);
    int w;
    while(st.size()) {
        w = st.back();
        st.pop_back();
        for(int j = v[w].size() - 1; j >= 0; j --) {
            if(star[v[w][j]] == 1) {
				v[w][j] = v[w].back();
				v[w].pop_back();
		    } else if(d[v[w][j]] != k) {
		   		d[v[w][j]] = k;
		   		st.push_back(v[w][j]);
		   	}
        }
    }
}

int main() {
	ios_base::sync_with_stdio(0);
    int n, m, a, b, p = 1, licz = 0, sz;
    char c;
    cin >> n >> m;
    for(int i = 0; i < m; i ++) {
        cin >> a >> b >> c;
        if(c == 'T') {
            v[a].push_back(b);
            v[b].push_back(a);
            dob[a].push_back(b);
        } else {
            zle[a].push_back(b);
        }
    }
    
    while(licz < p) {
        for(int i = 1; i <= n; i ++) {
            if(d[i] == licz) {
                for(int j = dob[i].size() - 1; j >= 0; j --) {
                    if(star[dob[i][j]] == 1) {
                        dob[i][j] = dob[i].back();
                        dob[i].pop_back();
                    } else {
                        szef[licz][i] ++;
                    }
                }
                for(int j = zle[i].size() - 1; j >= 0; j --) {
                    if(star[zle[i][j]] == 1 || d[zle[i][j]] != licz) {
                        zle[i][j] = zle[i].back();
                        zle[i].pop_back();
                    }
                    else {
                        szef[licz][zle[i][j]] ++;
                    }
                }
            }
        }
        sz = -1;
        for(int i = 1; i <= n; i ++) {
            if(d[i] == licz && szef[licz][i] == 0) {
                sz = i;
                break;
            }
        }
        if(sz == -1) {
            cout << "NIE\n";
            return 0;
        }
        ojc[sz] = tmp[licz];
        star[sz] = 1;
        v[sz].clear();
        dob[sz].clear();
        zle[sz].clear();
        d[sz] = -1;
        for(int i = 1; i <=n; i ++) {
            if(d[i] == licz) {
                dfs(i, p);
                tmp[p] = sz;
                p ++;
            }
        }
        licz ++;
    }
	for(int i = 1; i <= n; i ++) {
		cout << ojc[i] << endl;
	}
	return 0;
}