#include <bits/stdc++.h> using namespace std; #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 FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 1010; vi g[N], gr[N], h[N], hr[N]; int reach[N][N], used[N][N]; int vis[N], depth[N], cnt[N], res[N]; void dfs(int u, int cc) { reach[u][cc] = 1; FOR(i,SZ(g[u])) if (!reach[g[u][i]][cc]) dfs(g[u][i], cc); } void recalc(int n) { FOR(i,n) FOR(j,n) reach[i][j]=0; FOR(i,n) dfs(i,i); } int main() { int n,m; scanf("%d%d", &n, &m); FOR(i,m) { int a,b; char ss[5]; scanf("%d%d%s", &a, &b, ss); a--; b--; if (ss[0] == 'T') { g[a].push_back(b); gr[b].push_back(a); } else { h[a].push_back(b); hr[b].push_back(a); } } int root = -1; FOR(i,n) { if (g[i].size() == 0 && hr[i].size() == 0) root = i; } if (root == -1) { printf("NIE\n"); return 0; } int ff=1; while (ff) { ff=0; recalc(n); FOR(i,n) FOR(j,SZ(h[i])) { int k = h[i][j], fnd = -1; if (used[i][k]) continue; FOR(l,n) if (reach[i][l] && reach[k][l]) { fnd = l; l=n; } if (fnd != -1) { g[k].push_back(i); gr[i].push_back(k); used[i][k]=1; ff=1; } //printf("%d %d -- %d\n", i+1, k+1, fnd+1); } } ff=1; while (ff) { ff=0; recalc(n); FOR(i,n) { if (ff) i=n; FOR(j,SZ(g[i])) FOR(k,j) { if (ff) break; int jj=g[i][j], kk=g[i][k]; if (reach[jj][kk]==0 && reach[kk][jj]==0) { g[jj].push_back(kk); gr[kk].push_back(jj); //printf("adding %d %d (%d)\n", jj+1, kk+1, i+1); ff=1; break; } } } } FOR(i,n) { if (g[i].size() == 0 && hr[i].size() == 0) root = i; } FOR(i,n) cnt[i] = g[i].size(); vis[root]=1; FOR(j,SZ(gr[root])) { cnt[gr[root][j]]--; } FOR(ii,n) FOR(i,n) if (!vis[i] && cnt[i]==0) { vis[i] = 1; int low = 0; FOR(j,SZ(g[i])) if (depth[g[i][low]] < depth[g[i][j]]) low = j; if (SZ(g[i])==0) { res[i] = root+1; depth[i] = 1; } else { res[i] = g[i][low]+1; depth[i] = depth[g[i][low]]+1; } FOR(j,SZ(gr[i])) { cnt[gr[i][j]]--; } } FOR(i,n) if (!vis[i]) { printf("NIE\n"); return 0; } FOR(i,n) { //printf("i=%d\n", i+1); fflush(stdout); set<int> to_be, not_to_be; FOR(j,SZ(g[i])) to_be.insert(g[i][j]); FOR(j,SZ(h[i])) not_to_be.insert(h[i][j]); int u=i; //printf("path %d", i+1); fflush(stdout); while (u!=-1) { if (not_to_be.find(u) != not_to_be.end()) { printf("NIE\n"); return 0; //printf("%d shouldn\'t be before %d\n", i+1, u+1); } if (to_be.find(u) != to_be.end()) { to_be.erase(u); } u = res[u]-1; //printf(" %d", u+1); fflush(stdout); } //printf("\n"); if (to_be.size() > 0) { printf("NIE\n"); return 0; //for (auto j=to_be.begin(); j!=to_be.end(); j++) // printf("%d ", *j+1); //printf("-- %d elements left for %d\n", to_be.size(), i+1); } } //printf("TAK\n"); //return 0; FOR(i,n) printf("%d\n", res[i]); 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 | #include <bits/stdc++.h> using namespace std; #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 FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 1010; vi g[N], gr[N], h[N], hr[N]; int reach[N][N], used[N][N]; int vis[N], depth[N], cnt[N], res[N]; void dfs(int u, int cc) { reach[u][cc] = 1; FOR(i,SZ(g[u])) if (!reach[g[u][i]][cc]) dfs(g[u][i], cc); } void recalc(int n) { FOR(i,n) FOR(j,n) reach[i][j]=0; FOR(i,n) dfs(i,i); } int main() { int n,m; scanf("%d%d", &n, &m); FOR(i,m) { int a,b; char ss[5]; scanf("%d%d%s", &a, &b, ss); a--; b--; if (ss[0] == 'T') { g[a].push_back(b); gr[b].push_back(a); } else { h[a].push_back(b); hr[b].push_back(a); } } int root = -1; FOR(i,n) { if (g[i].size() == 0 && hr[i].size() == 0) root = i; } if (root == -1) { printf("NIE\n"); return 0; } int ff=1; while (ff) { ff=0; recalc(n); FOR(i,n) FOR(j,SZ(h[i])) { int k = h[i][j], fnd = -1; if (used[i][k]) continue; FOR(l,n) if (reach[i][l] && reach[k][l]) { fnd = l; l=n; } if (fnd != -1) { g[k].push_back(i); gr[i].push_back(k); used[i][k]=1; ff=1; } //printf("%d %d -- %d\n", i+1, k+1, fnd+1); } } ff=1; while (ff) { ff=0; recalc(n); FOR(i,n) { if (ff) i=n; FOR(j,SZ(g[i])) FOR(k,j) { if (ff) break; int jj=g[i][j], kk=g[i][k]; if (reach[jj][kk]==0 && reach[kk][jj]==0) { g[jj].push_back(kk); gr[kk].push_back(jj); //printf("adding %d %d (%d)\n", jj+1, kk+1, i+1); ff=1; break; } } } } FOR(i,n) { if (g[i].size() == 0 && hr[i].size() == 0) root = i; } FOR(i,n) cnt[i] = g[i].size(); vis[root]=1; FOR(j,SZ(gr[root])) { cnt[gr[root][j]]--; } FOR(ii,n) FOR(i,n) if (!vis[i] && cnt[i]==0) { vis[i] = 1; int low = 0; FOR(j,SZ(g[i])) if (depth[g[i][low]] < depth[g[i][j]]) low = j; if (SZ(g[i])==0) { res[i] = root+1; depth[i] = 1; } else { res[i] = g[i][low]+1; depth[i] = depth[g[i][low]]+1; } FOR(j,SZ(gr[i])) { cnt[gr[i][j]]--; } } FOR(i,n) if (!vis[i]) { printf("NIE\n"); return 0; } FOR(i,n) { //printf("i=%d\n", i+1); fflush(stdout); set<int> to_be, not_to_be; FOR(j,SZ(g[i])) to_be.insert(g[i][j]); FOR(j,SZ(h[i])) not_to_be.insert(h[i][j]); int u=i; //printf("path %d", i+1); fflush(stdout); while (u!=-1) { if (not_to_be.find(u) != not_to_be.end()) { printf("NIE\n"); return 0; //printf("%d shouldn\'t be before %d\n", i+1, u+1); } if (to_be.find(u) != to_be.end()) { to_be.erase(u); } u = res[u]-1; //printf(" %d", u+1); fflush(stdout); } //printf("\n"); if (to_be.size() > 0) { printf("NIE\n"); return 0; //for (auto j=to_be.begin(); j!=to_be.end(); j++) // printf("%d ", *j+1); //printf("-- %d elements left for %d\n", to_be.size(), i+1); } } //printf("TAK\n"); //return 0; FOR(i,n) printf("%d\n", res[i]); return 0; } |