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
#include <cstdio>
#include <list>
#include <map>
using namespace std;

unsigned constexpr N = 1e3 + 10;

unsigned n, parent[N];
signed char matrix[N][N];


unsigned char scanfInput() {
    unsigned short m;
    scanf("%u%hu", &n, &m);
    while(m--) {
        unsigned short a, b;
        char c;
        scanf("%hu%hu %c", &a, &b, &c);
        matrix[b][a] = (c == 'T' ? 1 : -1);
        if(c == 'T') {
            if(matrix[a][b] == 1)
                return 1;
            matrix[a][b] = -1;
        }
    }
    return 0;
}


bool filled_matrix[N];
bool fill_matrix_odw[N];

unsigned char fillMatrix(unsigned const v) {
    if(fill_matrix_odw[v])
        return 1;
    fill_matrix_odw[v] = true;
    if(filled_matrix[v])
        return 0;
    filled_matrix[v] = true;

    list<unsigned> ls;
    for(unsigned u = 1 ; u <= n ; ++u)
        if(matrix[v][u] == 1)
            ls.emplace_back(v);

    for(auto const & u : ls) {
        fillMatrix(u);
        for(unsigned i = 1 ; i <= n ; ++i)
            if(matrix[u][i] == 1) {
                if(matrix[v][i] == -1 || matrix[i][v] == 1)
                    return 1;
                matrix[v][i] = 1;
                matrix[i][v] = -1;
            }
    }

    return 0;
}

unsigned char fillMatrix() {
    for(unsigned v = 1 ; v <= n ; ++v) {
        fill(fill_matrix_odw, fill_matrix_odw+n+1, false);
        if(fillMatrix(v) == 1)
            return 1;
    }
    return 0;
}


struct Cmp {
    bool operator () (pair<unsigned, unsigned> const & a, pair<unsigned, unsigned> const & b) const {
        if(a.first != b.first)
            return a.first < b.first;
        return a.second > b.second;
    }
};
void constructTree() {
    unsigned root;

    multimap<pair<unsigned, unsigned>, unsigned, Cmp> mm;
    for(unsigned v = 1 ; v <= n ; ++v) {
        unsigned sum_plus = 0, sum_minus = 0;
        for(unsigned u = 1 ; u <= n ; ++u)
            if(matrix[v][u] == 1)
                ++sum_plus;
            else if(matrix[v][u] == -1)
                ++sum_minus;
        mm.emplace(pair<unsigned, unsigned>(sum_plus, sum_minus), v);
    }

    for(auto const & p : mm) {
        auto const & v = p.second;
        root = v;
        if(p.first.first == 0)
            continue;

        for(unsigned u = 1 ; u <= n ; ++u) {
            if(parent[u] == 0 && matrix[v][u] == 1)
                parent[u] = v;
        }
    }

    for(unsigned v = 1 ; v <= n ; ++v)
        if(v != root && parent[v] == 0)
            parent[v] = root;
}


bool isAncestor(unsigned const v, unsigned const u) {
    if(parent[u] == v)
        return true;
    if(parent[u] != 0)
        return isAncestor(v, parent[u]);
    return false;
}

unsigned char validateVertex(unsigned const v) {
    for(unsigned u = 1 ; u <= n ; ++u)
        if(v != u && matrix[v][u] == -1)
            if(isAncestor(v, u))
                return 1;
    return 0;
}

unsigned char validateTree() {
    for(unsigned v = 1 ; v <= n ; ++v)
        if(validateVertex(v) == 1)
            return 1;
    return 0;
}


void printfTree() {
    for(unsigned v = 1 ; v <= n ; ++v)
        printf("%u\n", parent[v]);
}


int main() {
    if(scanfInput() == 1) {
        printf("NIE");
        return 0;
    }
    if(fillMatrix() == 1) {
        printf("NIE");
        return 0;
    }
    constructTree();
    if(validateTree() == 1) {
        printf("NIE");
        return 0;
    }
    printfTree();
}