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
#include <cstdio>
#define MAXN 1001

int n,m;
int ts[MAXN][MAXN];
int head = 0;
const int prz = 1;
const int pod = 2;
const int noone = 3;

int main() {
    scanf("%d%d",&n,&m);
    for(int k = 0;k < m;k++) {
        int a,b;
        char c;
        scanf("%d%d %c",&a,&b,&c);
        if(c == 'T') {
                ts[a][b] = prz;
        }
        else {
                ts[b][a] = pod;
        }
    }

    for(int a = 1;a <= n;a++) {
        for(int b = 0;b <= n;b++) {
            if(ts[a][b] == pod) break;
            if(b == n) {
                head = a;
                a = n;
            }
        }
    }

    if(head == 0) {
        printf("NIE\n");
        return 0;
    }

    for(int k = 1;k <= n;k++) {
        if(k == head) printf("0\n");
        else {
            int h = 0;
            for(int l = 1;l <= n;l++) {
                if(ts[k][l] == prz) {
                    h = l;
                }
            }
            printf("%d\n",h == 0 ? head : h);
        }
    }
}