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
#include <bits/stdc++.h>
using namespace std;
using pi=pair<int, int>;
#define st first
#define nd second
const int N=1005, M=10005;
int n;
int id[N], p[N], belongs[N];
bool g[N][N];
vector<int> in[N], out[N], comp[N];
bool bad[N];
void dfs_id(int v, int src, int req_id)
{
    if(id[v]!=req_id) return;
    id[v]=src;
    comp[src].push_back(v);
    for(int u: in[v]) dfs_id(u, src, req_id);
    for(int u: out[v]) dfs_id(u, src, req_id);
}
inline bool compare_pi(pi a, pi b)
{
    return id[a.st]<id[b.st];
}
void print_vector(vector<int> v)
{
    printf("{");
    for(int i: v) printf("%d ", i);
    printf("}\n");
}
void print_vector_pi(vector<pi> v)
{
    printf("{");
    for(pi i: v) printf("(%d %d), ", i.st, i.nd);
    printf("}\n");
}
bool create_tree(int v, vector<pi> banned)
{
    //printf("v=%d:\n", v);
    id[v]=-v;
    //for(int i=1; i<=n; ++i) printf("id[%d]=%d\n", i, id[i]);
    for(int i=1; i<=n; ++i) if(id[i]==v) belongs[i]=v;
    for(int i=1; i<=n; ++i) dfs_id(i, i, v);
    //for(int i=1; i<=n; ++i) printf("id[%d]=%d\n", i, id[i]);
    vector<pi> tmp;
    for(pi i: banned) if(id[i.st]==id[i.nd]) tmp.push_back(i);
    sort(tmp.begin(), tmp.end(), compare_pi);
    //print_vector_pi(tmp);
    int j=0;
    for(int i=1; i<=n; ++i) if(belongs[i]==v&&id[i]==i&&comp[i].size()>0)
    {
        //printf("i=%d: ", i);
        //print_vector(comp[i]);
        for(int k: comp[i]) bad[k]=0;
        while(j<tmp.size()&&id[tmp[j].st]<i) ++j;
        vector<pi> tmp2;
        while(j<tmp.size()&&id[tmp[j].st]<=i)
        {
            bad[tmp[j].nd]=1;
            tmp2.push_back(tmp[j]);
            ++j;
        }
        for(int k: comp[i]) for(int l: out[k]) bad[l]=1;
        //for(int i=1; i<=n; ++i) printf("bad[%d]=%d\n", i, (int)bad[i]);
        int good=-1;
        for(int k: comp[i]) if(!bad[k]) good=k;
        if(good==-1) return 0;
        p[good]=v;
        for(int k: comp[i]) id[k]=good;
        comp[i].clear();
        if(!create_tree(good, tmp2)) return 0;
    }
    return 1;
}
vector<pi> q;
int main()
{
    //freopen("reo_new/reo_new144.in", "r", stdin);
    int m;
    scanf("%d%d", &n, &m);
    for(int i=0; i<m; ++i)
    {
        int a, b;
        char c;
        scanf("%d%d %c", &a, &b, &c);
        if(c=='T')
        {
            in[a].push_back(b);
            out[b].push_back(a);
        }
        else q.push_back(pi(a, b));
    }
    for(int i=1; i<=n; ++i) for(int j: out[i]) bad[j]=1;
    for(pi i: q) bad[i.nd]=1;
    int root=-1;
    for(int i=1; i<=n; ++i) if(!bad[i]) root=i;
    if(root==-1)
    {
        printf("NIE\n");
        return 0;
    }
    for(int i=1; i<=n; ++i) id[i]=root;
    if(!create_tree(root, q))
    {
        printf("NIE\n");
        return 0;
    }
    /*for(int i=1; i<=n; ++i)
    {
        int u=i;
        for(; u; u=p[u]) g[i][u]=1;
    }
    for(int i=1; i<=n; ++i) for(int j: in[i]) if(!g[i][j])
    {
        printf("NIE\n");
        return 0;
    }
    for(pi i: q) if(g[i.st][i.nd])
    {
        printf("NIE\n");
        return 0;
    }*/
    //printf("TAK\n");
    //for(int i=1; i<=n; ++i) if(p[i]==0) ++cnt0;
    for(int i=1; i<=n; ++i) printf("%d\n", p[i]);
}