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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <cstdio>
#include <vector>
#include <stack>
#define ANY (-1)

using namespace std;
int n, m;
vector<vector<int> > T(1001), N(1001);
stack<int> S;
vector<int> top;
vector<int> position(1001);
vector<int> visited(1001, 0);
vector<int> index(1001, 0);
vector<int> parent(1001, 0);
vector<int> over;

vector<vector<int> > required(1001, vector<int>(1001, 0));

int main()
{
    scanf( "%d%d", &n, &m);


    int a, b;
    char c[10];
    for( int i=0 ; i<m ; i++ )
    {
        scanf("%d%d%s", &a, &b, c);
        if( c[0]=='T')
        {
            T[a].push_back(b);
            required[a][b]=1;
        }
        else
        {
            N[a].push_back(b);
            required[a][b]=-1;
        }
    }


    for( int k=1 ; k<=n ; k++ )
    {
        if( visited[k]>0 )
            continue;
        S.push(k);
        visited[k]++;
        while( !S.empty() )
        {
            int x=S.top();
            if( index[x]==T[x].size() )
            {
                top.push_back(x);
                position[x] = top.size();
                S.pop();
                continue;
            }
            while( index[x] < T[x].size() )
            {
                if( visited[ T[x][ index[x] ] ]>0 )
                {
                    index[x]++;
                    continue;
                }
                S.push( T[x][ index[x] ] );
                visited[ T[x][ index[x] ] ]++;
                index[x]++;
                break;
            }
        }
    }

    for( int k=0 ; k<n-1 ; k++ )
    {
        for( int i=0 ; i<T[top[k]].size() ; i++ )
            if( T[top[k]][i]==top[k+1] )
            {
                printf("NIE\n");
                return 0;
            }
    }



    for( int k=n-1 ; k>=0 ; k-- )
    {
        if( T[top[k]].size()==0 )
        {
            parent[top[k]]=ANY;
            continue;
        }

        bool flag=false;
        int chosen=-1;

        for( int j=k-1 ; j>=0 ; j-- )
        {
            if( required[ top[k] ][ top[j] ]==1 )
            {
                for( int i=T[ top[k] ].size()-1 ; i>=0 && flag==false ; i-- )
                {
                    //printf( "top[k], top[j]: %d  %d\n  ", top[k], top[j]);
                    //printf("? from to %d %d\n", top[j], T[top[k]][i]);
                    if( required[ top[j] ][ T[top[k]][i] ] == -1)
                    {
                        flag=true;
                    }
                    else if( i==0 )
                        chosen = j;
                }
                if( flag==true )
                    flag=false;
                else
                    break;
            }
        }
        if( chosen==-1 )
        {
            printf("NIE\n");
            return 0;
        }
        parent[top[k]] = top[chosen];
        for( int i=0 ; i<T[top[k]].size() ; i++ )
        {
            if( required[ top[chosen] ][ T[top[k]][i] ]==0  && top[chosen]!=T[top[k]][i] )
            {
                required[ top[chosen] ][ T[top[k]][i] ]=1;
                T[top[chosen]].push_back( T[top[k]][i] );
            }
        }
        for( int i=0 ; i<N[top[k]].size() ; i++ )
        {
            if( required[ top[chosen] ][ N[top[k]][i] ]==0 )
            {
                required[ top[chosen] ][ N[top[k]][i] ]=-1;
                N[top[chosen]].push_back( N[top[k]][i] );
            }
        }
    }

    int root=-1;
    for( int k=1 ; k<=n ; k++ )
    {
        if( T[k].size()>0 ) continue;

        bool flag=false;
        for( int j=1 ; j<=n && flag==false ; j++ )
            if( required[j][k]==-1 )
                flag=true;
        if( flag==false )
        {
            root=k;
            break;
        }
    }
    if( root==-1 )
    {
        printf("NIE\n");
        return 0;
    }

    for( int k=1 ; k<=n ; k++ )
        if( parent[k]==ANY && k!=root )
            parent[k]=root;
    parent[root]=0;


    for( int k=1 ; k<=n ; k++ )
    {
        int p=parent[k];
        while( p>0 )
        {
            if( required[k][p]==-1 )
            {
                printf("NIE\n");
                return 0;
            }

            p=parent[p];
        }
    }


    for( int i=1 ; i<=n ; i++ )
        printf("%d\n", parent[i] ) ;

    return 0;
}