#include<bits/stdc++.h> using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) int n, m, vis[1005], B[1005], globalnum=1; char C[10]; VI notson[1005], son[1005]; VPI V[1005]; int R[1005], par[1005]; //bool no[1005][1005], yes[1005][1005]; //vector<int> hehe[1005]; void remove(int ind) { REP(i, siz(son[ind])) par[son[ind][i]]--; son[ind].clear(); while(!V[ind].empty()) { int s=siz(V[ind])-1; int u=V[ind][s].F, i=V[ind][s].S; int w=V[u][siz(V[u])-1].F, ind2=V[u][siz(V[u])-1].S; V[w][ind2].S=i; swap(V[u][i], V[u][siz(V[u])-1]); V[u].pb(); V[ind].pb(); } } bool go(VI X, int prev, int num) { int root=-1, s=siz(X); //debug("lol %d %d %d %d %d\n",s, prev, num, root, X[0]); REP(i, s) vis[X[i]]=num; REP(i, s) { for(int j=siz(notson[X[i]])-1; j>=0; j--) { int u=notson[X[i]][j]; if(vis[u]!=num) { swap(notson[X[i]][j], notson[X[i]][siz(notson[X[i]])-1]); notson[X[i]].pb(); } } } REP(i, s) { if(notson[X[i]].empty() && par[X[i]]==0) { root=X[i]; break; } } if(root==-1) return false; //REP(i, s) // debug("aa %d\n", X[i]); //debug("%d %d %d %d\n",s, prev, num, root); R[root]=prev; remove(root); B[root]=1; REP(i, s) vis[X[i]]=0; vis[root]=num; REP(i, s) { if(B[X[i]]) continue; VI Y; Y.PB(X[i]); vis[X[i]]=1; REP(i, siz(Y)) { REP(j, siz(V[Y[i]])) { if(!B[V[Y[i]][j].F] && !vis[V[Y[i]][j].F]) { vis[V[Y[i]][j].F]=num; Y.PB(V[Y[i]][j].F); } } } globalnum++; if(!go(Y, root, globalnum)) return false; } return true; } int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); REP(i, m) { int a, b; scanf("%d%d%s", &a, &b, C); // debug("%d %d %c\n", a, b, C[0]); if(C[0]=='T') { // hehe[a].PB(b); son[b].PB(a); par[a]++; V[a].PB(MP(b, siz(V[b]))); V[b].PB(MP(a, siz(V[a])-1)); } else { notson[b].PB(a); // no[a][b]=1; } } VI X; RE(i, n) X.PB(i); if(go(X, 0, 1)) { //printf("TAK\n"); /*RE(i, n) { int v=i; while(v!=0) { if(no[i][v]) { printf("a\n"); return 0; } yes[i][v]=1; v=R[v]; } REP(j, siz(hehe[i])) { if(!yes[i][hehe[i][j]]) { printf("b\n"); return 0; } } }*/ RE(i, n) printf("%d\n", R[i]); } else printf("NIE\n"); 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 | #include<bits/stdc++.h> using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) int n, m, vis[1005], B[1005], globalnum=1; char C[10]; VI notson[1005], son[1005]; VPI V[1005]; int R[1005], par[1005]; //bool no[1005][1005], yes[1005][1005]; //vector<int> hehe[1005]; void remove(int ind) { REP(i, siz(son[ind])) par[son[ind][i]]--; son[ind].clear(); while(!V[ind].empty()) { int s=siz(V[ind])-1; int u=V[ind][s].F, i=V[ind][s].S; int w=V[u][siz(V[u])-1].F, ind2=V[u][siz(V[u])-1].S; V[w][ind2].S=i; swap(V[u][i], V[u][siz(V[u])-1]); V[u].pb(); V[ind].pb(); } } bool go(VI X, int prev, int num) { int root=-1, s=siz(X); //debug("lol %d %d %d %d %d\n",s, prev, num, root, X[0]); REP(i, s) vis[X[i]]=num; REP(i, s) { for(int j=siz(notson[X[i]])-1; j>=0; j--) { int u=notson[X[i]][j]; if(vis[u]!=num) { swap(notson[X[i]][j], notson[X[i]][siz(notson[X[i]])-1]); notson[X[i]].pb(); } } } REP(i, s) { if(notson[X[i]].empty() && par[X[i]]==0) { root=X[i]; break; } } if(root==-1) return false; //REP(i, s) // debug("aa %d\n", X[i]); //debug("%d %d %d %d\n",s, prev, num, root); R[root]=prev; remove(root); B[root]=1; REP(i, s) vis[X[i]]=0; vis[root]=num; REP(i, s) { if(B[X[i]]) continue; VI Y; Y.PB(X[i]); vis[X[i]]=1; REP(i, siz(Y)) { REP(j, siz(V[Y[i]])) { if(!B[V[Y[i]][j].F] && !vis[V[Y[i]][j].F]) { vis[V[Y[i]][j].F]=num; Y.PB(V[Y[i]][j].F); } } } globalnum++; if(!go(Y, root, globalnum)) return false; } return true; } int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); REP(i, m) { int a, b; scanf("%d%d%s", &a, &b, C); // debug("%d %d %c\n", a, b, C[0]); if(C[0]=='T') { // hehe[a].PB(b); son[b].PB(a); par[a]++; V[a].PB(MP(b, siz(V[b]))); V[b].PB(MP(a, siz(V[a])-1)); } else { notson[b].PB(a); // no[a][b]=1; } } VI X; RE(i, n) X.PB(i); if(go(X, 0, 1)) { //printf("TAK\n"); /*RE(i, n) { int v=i; while(v!=0) { if(no[i][v]) { printf("a\n"); return 0; } yes[i][v]=1; v=R[v]; } REP(j, siz(hehe[i])) { if(!yes[i][hehe[i][j]]) { printf("b\n"); return 0; } } }*/ RE(i, n) printf("%d\n", R[i]); } else printf("NIE\n"); return 0; } |