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
//PA2016, runda 2, Reoorganizacja
// Andrzej Pezarski


#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

const int maxN=1001;

int N, M;

int P[maxN], R[maxN];
bool doneMark[maxN];
bool testMark[maxN];
int parent[maxN];
vector<pair<int, int> > edgeT;
vector<pair<int, int> > edgeF;


int find(int x) {
    if (P[x]!=x) P[x]=find(P[x]);
    return P[x];
}

int uni (int x, int y) {
    x=find(x);
    y=find(y);
    if (x==y) return x;
    if (R[x]>=R[y]) {
        if (R[x]==R[y]) R[x]++;
        return P[y]=x;
    } else return P[x]=y;
}


int countFirst() {
    for (int i=0; i<=N; i++) {
        testMark[i]=doneMark[i];
    }
    for (auto x:edgeT) {
        testMark[x.first] = true;
    }
    for (auto x:edgeF) {
        testMark[x.first] = true;
    }
    for (int i=1; i<=N; i++) {
        if (!testMark[i]) return i;
    }    
    return -1;
}

int countCC() {
    for (int i=0; i<=N; i++) {
        R[i]=0;
        P[i]=i;
        testMark[i]=doneMark[i];
    }
    for (auto x:edgeT) {
        if (doneMark[x.first] || doneMark[x.second]) continue;
        testMark[x.first]=true;
        uni(x.first, x.second);
    }
    for (int i=0; i<=N; i++) {
        find(i);
    }
    for (auto x:edgeF) {
        testMark[x.first] |= (P[x.first] == P[x.second]) ;
    }
    
    for (int i=1; i<=N; i++) {
        if (!testMark[i]) return i;
    }    
    return -1;
}


bool isOnPath(int x, int y) {
    while (x!=0 && x!=y) x=parent[x];
    return x!=0;
}

bool testDebug() {
    for (auto x:edgeT) {
        if(!isOnPath(x.first, x.second)) return false;
    }    
    for (auto x:edgeT) {
        if(isOnPath(x.second, x.first)) return false;
    }    
    return true;
}


int main() {
    scanf("%d%d", &N, &M);
    for(int i=0; i<=N; i++) doneMark[i]=false;
    while(M--) {
        int a, b;
        char c[4];
        scanf("%d%d%s", &a, &b, c);
        if (c[0]=='T') edgeT.push_back(make_pair(a, b));
        else edgeF.push_back(make_pair(b, a));
    }
    int v=countFirst();
    if (v==-1){
        printf("NIE\n");
        return 0;
    }
    
    for(int i=1; i<=N; i++) parent[i]=v;
    parent[v]=0;
    doneMark[v]=true;
    
    for(int n=1; n<N; n++) {
        int v=countCC();
            if (v==-1){
            printf("NIE\n");
            return 0;
        }
        for(int i=1; i<=N; i++) if (P[i]==P[v] && i!=v) parent[i]=v;
        doneMark[v]=true;
    }
//    if (!testDebug()) printf("DUPA\n");
//    printf("TAK\n");
    for(int i=1; i<=N; i++) printf("%d\n", parent[i]);
    return 0;
}