#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> #include<vector> #include<set> #include<map> #include<stack> #include<queue> #include<list> #include<string> #include<cstring> using namespace std; #define PB push_back #define MP make_pair #define F first #define S second #define ALL(x) (x).begin(),(x).end() #define SIZE(x) (int)(x).size() #define SORT(x) sort((x).begin(),(x).end()) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define REP(x, n) for(int x = 0; x < (n); x++) #define FOR(x, b, e) for(int x = (b); x <= (e); x++) #define FORD(x, b, e) for(int x = (b); x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) const int MAX_BUF_SIZE = 16384; char BUFOR[MAX_BUF_SIZE]; int BUF_SIZE, BUF_POS; char ZZZ; #define GET(ZZZ){ZZZ='a';if(BUF_POS<BUF_SIZE)ZZZ=BUFOR[BUF_POS++];\ else if(!feof(stdin)){BUF_SIZE=fread(BUFOR,1,MAX_BUF_SIZE,stdin);\ ZZZ=BUFOR[0];BUF_POS=1;}} #define GI(WW){do{GET(ZZZ);}while(!isdigit(ZZZ));WW=ZZZ-'0';\ while(1){GET(ZZZ);if(!isdigit(ZZZ))break;WW=WW*10+(ZZZ-'0');}} #define GC(WW){do{GET(ZZZ);}while(!isalpha(ZZZ));WW=ZZZ;} //FAST IO typedef long long int LL; typedef pair<int,int> PII; typedef vector<int> VI; const int MXN = 1010; const int C = 262144; const int INF = 1000000001; int vis[MXN][MXN]; int n, m; int isCycle; vector<PII> nedge; VI G[MXN], GT[MXN]; VI H[MXN]; int res[MXN]; int d = 1; int D[MXN][40]; int DT[MXN][40]; int HH[MXN][40]; int degg[MXN]; void setH(int a, int b, int val) { if(val == 1) HH[a][b / 32] |= 1 << (b % 32); else HH[a][b / 32] &= ~(1 << (b % 32) ); } void setD(int a, int b, int val) { if(val == 1) D[a][b / 32] |= 1 << (b % 32); else D[a][b / 32] &= ~(1 << (b % 32) ); } void setDT(int a, int b, int val) { if(val == 1) DT[a][b / 32] |= 1 << (b % 32); else DT[a][b / 32] &= ~(1 << (b % 32) ); } int degIn(int x) { int res = 0; FOR(i, 0, 31) res += __builtin_popcount(DT[x][i]); return res - 1; } int degOut(int x) { int res = 0; FOR(i, 0, 31) res += __builtin_popcount(D[x][i]); return res - 1; } int getValDT(int a, int b) { return (DT[a][b / 32] & (1 << (b % 32) ) ) > 0; } int getValD(int a, int b) { return (D[a][b / 32] & (1 << (b % 32) ) ) > 0; } int getValH(int a, int b) { return (HH[a][b / 32] & (1 << (b % 32) ) ) > 0; } int degInH(int x) { int res = 0; FOR(i, 1, n) res += getValH(i, x); return res; } void merguj(int a, int b) { //a nowy, b - usuwamy FOR(i, 0, 31) HH[a][i] |= HH[b][i]; FOR(i, 0, 31) D[a][i] |= D[b][i]; FOR(i, 1, n) if(res[i]) setD(a, i, 0); FOR(i, 1, n) if(getValDT(i, b)) setDT(i, a, 1); FOR(i, 1, n) setDT(i, b, 0); } int funcHD(int a, int b) { int cnt = 0; FOR(i, 0, 31) cnt += __builtin_popcount(D[b][i] & HH[a][i]); return cnt - getValH(a, b) > 0; } int funcGT(int a, int b) { int cnt = 0; FOR(i, 0, 31) cnt += __builtin_popcount(DT[a][i] & D[b][i]); //cout<<a<<" @@@@@@ "<<b<<" "<<cnt<<endl; return cnt > 2; } void dfs(int x, int s) { vis[s][x] = 1; FOREACH(it, G[x]) if(vis[s][*it] == 0) dfs(*it, s); } void test() { scanf("%d %d", &n, &m); FOR(i, 1, m) { int a, b; char zn; scanf("%d %d", &a, &b); while(1) { zn = getchar(); if(zn == 'N' || zn == 'T') break; } if(zn == 'T') { G[a].PB(b); GT[b].PB(a); } else { H[a].PB(b); setH(a, b, 1); nedge.PB(MP(a, b) ); degg[b]++; } } FOR(i, 1, n) dfs(i, i); FOR(i, 1, n) FOREACH(it, G[i]) if(vis[*it][i]) isCycle = 1; FOREACH(it, nedge) if(vis[it -> F][it -> S]) isCycle = 1; FOR(i, 1, n) FOR(j, 1, n) { setD(i, j, vis[i][j]); setDT(j, i, vis[i][j]); } if(isCycle) { printf("NIE\n"); return; } while(1) { int w = -1; FOR(i, 1, n) { //cout<<i<<" "<<degOut(i)<<" "<<degIn(i)<<" "<<degInH(i)<<endl; if(res[i] == 0 && degIn(i) == 0 && degOut(i) > 0) { w = i; break; } } if(w == -1) break; bool czy = 0; FOR(u, 1, n) { if(res[u] == 0 && u != w && getValD(w, u) && degg[u] > 0) { //cout<<"SPR "<<w<<" "<<u<<endl; bool czyOk = 1; if(funcHD(u, w) ) czyOk = 0; //cout<<"! "<<czyOk<<endl; if(funcGT(u, w) ) //czy nastepnik w jest osiagalny z u w G_T czyOk = 0; //cout<<"@ "<<czyOk<<endl; if(czyOk) { //cout<<"OOOOOOOOK"<<endl; res[w] = u; merguj(u, w); czy = 1; break; } } } if(!czy) FOR(u, 1, n) { if(res[u] == 0 && u != w && getValD(w, u) && degg[u] == 0) { //cout<<"SPR "<<w<<" "<<u<<endl; bool czyOk = 1; if(funcHD(u, w) ) czyOk = 0; //cout<<"! "<<czyOk<<endl; if(funcGT(u, w) ) //czy nastepnik w jest osiagalny z u w G_T czyOk = 0; //cout<<"@ "<<czyOk<<endl; if(czyOk) { //cout<<"OOOOOOOOK"<<endl; res[w] = u; merguj(u, w); czy = 1; break; } } } //cout<<czy<<endl; if(!czy) { printf("NIE\n"); return; } } int root = -1; // FOR(i, 1, n) // cout<<"RES "<<res[i]<<endl; FOR(i, 1, n) { if(res[i] == 0 && degInH(i) == 0) { root = i; break; } } FOR(i, 1, n) { if(res[i] == 0 && i != root) res[i] = root; } if(root == -1) printf("NIE\n"); else { //printf("TAK\n"); FOR(i, 1, n) printf("%d\n", res[i]); } } int main() { int te = 1; //scanf("%d", &te); while(te--) test(); return 0; }
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | #include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> #include<vector> #include<set> #include<map> #include<stack> #include<queue> #include<list> #include<string> #include<cstring> using namespace std; #define PB push_back #define MP make_pair #define F first #define S second #define ALL(x) (x).begin(),(x).end() #define SIZE(x) (int)(x).size() #define SORT(x) sort((x).begin(),(x).end()) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define REP(x, n) for(int x = 0; x < (n); x++) #define FOR(x, b, e) for(int x = (b); x <= (e); x++) #define FORD(x, b, e) for(int x = (b); x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) const int MAX_BUF_SIZE = 16384; char BUFOR[MAX_BUF_SIZE]; int BUF_SIZE, BUF_POS; char ZZZ; #define GET(ZZZ){ZZZ='a';if(BUF_POS<BUF_SIZE)ZZZ=BUFOR[BUF_POS++];\ else if(!feof(stdin)){BUF_SIZE=fread(BUFOR,1,MAX_BUF_SIZE,stdin);\ ZZZ=BUFOR[0];BUF_POS=1;}} #define GI(WW){do{GET(ZZZ);}while(!isdigit(ZZZ));WW=ZZZ-'0';\ while(1){GET(ZZZ);if(!isdigit(ZZZ))break;WW=WW*10+(ZZZ-'0');}} #define GC(WW){do{GET(ZZZ);}while(!isalpha(ZZZ));WW=ZZZ;} //FAST IO typedef long long int LL; typedef pair<int,int> PII; typedef vector<int> VI; const int MXN = 1010; const int C = 262144; const int INF = 1000000001; int vis[MXN][MXN]; int n, m; int isCycle; vector<PII> nedge; VI G[MXN], GT[MXN]; VI H[MXN]; int res[MXN]; int d = 1; int D[MXN][40]; int DT[MXN][40]; int HH[MXN][40]; int degg[MXN]; void setH(int a, int b, int val) { if(val == 1) HH[a][b / 32] |= 1 << (b % 32); else HH[a][b / 32] &= ~(1 << (b % 32) ); } void setD(int a, int b, int val) { if(val == 1) D[a][b / 32] |= 1 << (b % 32); else D[a][b / 32] &= ~(1 << (b % 32) ); } void setDT(int a, int b, int val) { if(val == 1) DT[a][b / 32] |= 1 << (b % 32); else DT[a][b / 32] &= ~(1 << (b % 32) ); } int degIn(int x) { int res = 0; FOR(i, 0, 31) res += __builtin_popcount(DT[x][i]); return res - 1; } int degOut(int x) { int res = 0; FOR(i, 0, 31) res += __builtin_popcount(D[x][i]); return res - 1; } int getValDT(int a, int b) { return (DT[a][b / 32] & (1 << (b % 32) ) ) > 0; } int getValD(int a, int b) { return (D[a][b / 32] & (1 << (b % 32) ) ) > 0; } int getValH(int a, int b) { return (HH[a][b / 32] & (1 << (b % 32) ) ) > 0; } int degInH(int x) { int res = 0; FOR(i, 1, n) res += getValH(i, x); return res; } void merguj(int a, int b) { //a nowy, b - usuwamy FOR(i, 0, 31) HH[a][i] |= HH[b][i]; FOR(i, 0, 31) D[a][i] |= D[b][i]; FOR(i, 1, n) if(res[i]) setD(a, i, 0); FOR(i, 1, n) if(getValDT(i, b)) setDT(i, a, 1); FOR(i, 1, n) setDT(i, b, 0); } int funcHD(int a, int b) { int cnt = 0; FOR(i, 0, 31) cnt += __builtin_popcount(D[b][i] & HH[a][i]); return cnt - getValH(a, b) > 0; } int funcGT(int a, int b) { int cnt = 0; FOR(i, 0, 31) cnt += __builtin_popcount(DT[a][i] & D[b][i]); //cout<<a<<" @@@@@@ "<<b<<" "<<cnt<<endl; return cnt > 2; } void dfs(int x, int s) { vis[s][x] = 1; FOREACH(it, G[x]) if(vis[s][*it] == 0) dfs(*it, s); } void test() { scanf("%d %d", &n, &m); FOR(i, 1, m) { int a, b; char zn; scanf("%d %d", &a, &b); while(1) { zn = getchar(); if(zn == 'N' || zn == 'T') break; } if(zn == 'T') { G[a].PB(b); GT[b].PB(a); } else { H[a].PB(b); setH(a, b, 1); nedge.PB(MP(a, b) ); degg[b]++; } } FOR(i, 1, n) dfs(i, i); FOR(i, 1, n) FOREACH(it, G[i]) if(vis[*it][i]) isCycle = 1; FOREACH(it, nedge) if(vis[it -> F][it -> S]) isCycle = 1; FOR(i, 1, n) FOR(j, 1, n) { setD(i, j, vis[i][j]); setDT(j, i, vis[i][j]); } if(isCycle) { printf("NIE\n"); return; } while(1) { int w = -1; FOR(i, 1, n) { //cout<<i<<" "<<degOut(i)<<" "<<degIn(i)<<" "<<degInH(i)<<endl; if(res[i] == 0 && degIn(i) == 0 && degOut(i) > 0) { w = i; break; } } if(w == -1) break; bool czy = 0; FOR(u, 1, n) { if(res[u] == 0 && u != w && getValD(w, u) && degg[u] > 0) { //cout<<"SPR "<<w<<" "<<u<<endl; bool czyOk = 1; if(funcHD(u, w) ) czyOk = 0; //cout<<"! "<<czyOk<<endl; if(funcGT(u, w) ) //czy nastepnik w jest osiagalny z u w G_T czyOk = 0; //cout<<"@ "<<czyOk<<endl; if(czyOk) { //cout<<"OOOOOOOOK"<<endl; res[w] = u; merguj(u, w); czy = 1; break; } } } if(!czy) FOR(u, 1, n) { if(res[u] == 0 && u != w && getValD(w, u) && degg[u] == 0) { //cout<<"SPR "<<w<<" "<<u<<endl; bool czyOk = 1; if(funcHD(u, w) ) czyOk = 0; //cout<<"! "<<czyOk<<endl; if(funcGT(u, w) ) //czy nastepnik w jest osiagalny z u w G_T czyOk = 0; //cout<<"@ "<<czyOk<<endl; if(czyOk) { //cout<<"OOOOOOOOK"<<endl; res[w] = u; merguj(u, w); czy = 1; break; } } } //cout<<czy<<endl; if(!czy) { printf("NIE\n"); return; } } int root = -1; // FOR(i, 1, n) // cout<<"RES "<<res[i]<<endl; FOR(i, 1, n) { if(res[i] == 0 && degInH(i) == 0) { root = i; break; } } FOR(i, 1, n) { if(res[i] == 0 && i != root) res[i] = root; } if(root == -1) printf("NIE\n"); else { //printf("TAK\n"); FOR(i, 1, n) printf("%d\n", res[i]); } } int main() { int te = 1; //scanf("%d", &te); while(te--) test(); return 0; } |