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
// tested by Hightail: https://github.com/dj3500/hightail
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
#include <cassert>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

template <typename T, typename R>
void wywal (vector<R> &v, const T &lambda) {
   v.erase(remove_if(ALL(v), lambda), v.end());
}

const int N = 20200;
vi out[N];
bool hasredin[N];
vector<pii> red;
bool seen[N];
int topnum[N], realoftop[N];
int biggesttop[N], res[N];

// verification
const int LOCAL = 1;
vector<pii> original[2];
bool verify () {
   FOR(bit,2) for (const pii &p : original[bit]) {
      bool seenSe = 0;
      int v = p.fi;
      while (v != 0) {
         if (v == p.se) seenSe = 1;
         v = res[v];
      }
      if (seenSe != bit) {
         //DBG(bit);
         //printf("FAILUJE");
         //exit(1);
         return 0;
      }
   }
   return 1;
}

void dfs (int v) {
   if (seen[v]++) return;
   for (int x : out[v]) dfs(x);
}

int main () {
   wez2(n,m);
   FOR(u,m) {
      int a, b;
      char c;
      scanf("%d %d %c", &a, &b, &c);
      if (c == 'T') {
         out[a].pb(b);
         original[1].pb(mp(a,b));
      } else {
         red.pb(mp(a,b));
         hasredin[b] = 1;
         original[0].pb(mp(a,b));
      }
   }
   int root = -1;
   FORI(i,n) {
      if (out[i].empty() && !hasredin[i]) root = i;
   }
   if (root == -1) {
      printf("NIE\n");
      return 0;
   }
   //in[root].clear();
   FORI(i,n) wywal(out[i], [root] (int x) { return x == root; });
   wywal(red, [root] (const pii &p) { return p.fi == root; });
   
   FORI(v,n) if (v != root) {
      FORI(i,n) seen[i] = 0;
      dfs(v);
      wywal(red, [] (const pii &p) {
         if (seen[p.fi] && seen[p.se]) {
            out[p.se].pb(p.fi); // w druga strone
            return 1;
         } else {
            return 0;
         }
      });
   }
   
   vi indeg, deg0;
   vi topnumsvisible;
   for (int iter = 1; ; ++iter) {
      indeg.assign(n+3, 0);
      deg0.clear();
      FORI(i,n) if (i != root) for (int j : out[i]) ++indeg[j];
      FORI(i,n) if (i != root) if (indeg[i] == 0) deg0.pb(i);
      topnum[root] = 1;
      realoftop[1] = root;
      int nxt = n;
      while (!deg0.empty()) {         
         int randIndex = rand() % SZ(deg0);
         if (randIndex != SZ(deg0)-1) swap(deg0[SZ(deg0)-1], deg0[randIndex]);
         
         int v = deg0.back();
         deg0.pop_back();
         
         topnum[v] = nxt;
         realoftop[nxt] = v;
         --nxt;
         for (int j : out[v]) {
            if (--indeg[j] == 0) {
               deg0.pb(j);
            }
         }
      }
   
      if (nxt != 1) {
         // black cycle
         printf("NIE\n");
         return 0;
      }
      //if (LOCAL) FORI(i,n) for (int j : out[i]) assert(topnum[i] > topnum[j]);
   
      // heura
      FORI(v,n) biggesttop[v] = 1;
      FORI(v,n) if (v != root) {
         topnumsvisible.clear();
         FORI(i,n) seen[i] = 0;
         dfs(v);
         FORI(i,n) if (seen[i]) topnumsvisible.pb(topnum[i]);
         sort(ALL(topnumsvisible));
         //if (LOCAL) assert(v == realoftop[topnumsvisible.back()]);
         REP(j,1,SZ(topnumsvisible)-1) {
            REMAX(biggesttop[realoftop[topnumsvisible[j]]], topnumsvisible[j-1]);
         }
      }
      res[root] = 0;
      FORI(v,n) if (v != root) res[v] = realoftop[biggesttop[v]];
      if (verify()) break;
      if (iter == 300) {
         // moze sie nie da...
         printf("NIE\n");
         return 0;
      } // else: iteruj jeszcze raz
   }
   if (0) {
      printf("TAK\n");
   } else {
      FORI(v,n) printf("%d\n", res[v]);
   }
}