#include <bits/stdc++.h> #define scanf(arg...) (scanf(arg)?:0) #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 REP(x, b) for(int x = 0; x < (b); ++x) #define SIZE(c) ((int)c.size()) #define ALL(c) (c).begin(), (c).end() #define PB push_back #define MP make_pair #define ST first #define ND second using namespace std; typedef long double LD; typedef long long LL; typedef unsigned long long ULL; typedef vector <int> VI; typedef pair <int, int> PII; typedef vector <PII> VPII; const int MAXN = 1e4 + 9; VI tab[MAXN]; VI tab2[MAXN]; VI tab3[MAXN]; VI vec; int parent[MAXN]; bitset <MAXN> odw; bitset <MAXN> mozna; void usun(vector <int>& v, int poz) { swap(v[poz], v.back()); v.pop_back(); } void dfs(int poz, vector <int>& v) { odw[poz] = true; v.PB(poz); REP(i, SIZE(tab3[poz])) { if(!odw[tab3[poz][i]]) dfs(tab3[poz][i], v); } } void eliminacja(vector <int>& v) { REP(i, SIZE(v)) { for(int j = 0; j < SIZE(tab[v[i]]); j++) { if(odw[tab[v[i]][j]] != odw[v[i]]) usun(tab[v[i]], j), j--; } for(int j = 0; j < SIZE(tab2[v[i]]); j++) { if(odw[tab2[v[i]][j]] != odw[v[i]]) usun(tab2[v[i]], j), j--; } for(int j = 0; j < SIZE(tab3[v[i]]); j++) { if(odw[tab3[v[i]][j]] != odw[v[i]]) usun(tab3[v[i]], j), j--; } } } int korzen(vector <int>& v) { mozna.reset(); REP(i, SIZE(v)) { if(SIZE(tab[v[i]]) == 0) mozna[v[i]] = true; } REP(i, SIZE(v)) { REP(j, SIZE(tab2[v[i]])) { mozna[tab2[v[i]][j]] = false; } } REP(i, SIZE(v)) { if(mozna[v[i]]) return v[i]; } return -1; } void usun_korzen(vector <int>& v, int root) { odw[root] = false; eliminacja(v); } int rek(int poz) { vector <int> tmp; odw.reset(); dfs(poz, tmp); eliminacja(tmp); int root = korzen(tmp), temp; if(root == -1) return -1; usun_korzen(tmp, root); REP(i, SIZE(tmp)) { if(tmp[i] != root) { if(parent[tmp[i]] == -2) { temp = rek(tmp[i]); if(temp == -1) return -1; parent[temp] = root; } } } return root; } int main() { int n, m, a, b, temp; char ch; cin>>n>>m; REP(i, m) { scanf("%d %d %c", &a, &b, &ch); a--; b--; if(ch == 'T') { tab[a].PB(b); tab3[a].PB(b); tab3[b].PB(a); } else { tab2[a].PB(b); } } REP(i, n) vec.PB(i), parent[i] = -2, odw[i] = true; int root = korzen(vec); if(root == -1) { puts("NIE"); return 0; } parent[root] = -1; usun_korzen(vec, root); REP(i, n) { if(i != root) { if(parent[i] == -2) { temp = rek(i); if(temp == -1) { puts("NIE"); return 0; } parent[temp] = root; } } } REP(i, n) { printf("%d\n", parent[i] + 1); } 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 | #include <bits/stdc++.h> #define scanf(arg...) (scanf(arg)?:0) #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 REP(x, b) for(int x = 0; x < (b); ++x) #define SIZE(c) ((int)c.size()) #define ALL(c) (c).begin(), (c).end() #define PB push_back #define MP make_pair #define ST first #define ND second using namespace std; typedef long double LD; typedef long long LL; typedef unsigned long long ULL; typedef vector <int> VI; typedef pair <int, int> PII; typedef vector <PII> VPII; const int MAXN = 1e4 + 9; VI tab[MAXN]; VI tab2[MAXN]; VI tab3[MAXN]; VI vec; int parent[MAXN]; bitset <MAXN> odw; bitset <MAXN> mozna; void usun(vector <int>& v, int poz) { swap(v[poz], v.back()); v.pop_back(); } void dfs(int poz, vector <int>& v) { odw[poz] = true; v.PB(poz); REP(i, SIZE(tab3[poz])) { if(!odw[tab3[poz][i]]) dfs(tab3[poz][i], v); } } void eliminacja(vector <int>& v) { REP(i, SIZE(v)) { for(int j = 0; j < SIZE(tab[v[i]]); j++) { if(odw[tab[v[i]][j]] != odw[v[i]]) usun(tab[v[i]], j), j--; } for(int j = 0; j < SIZE(tab2[v[i]]); j++) { if(odw[tab2[v[i]][j]] != odw[v[i]]) usun(tab2[v[i]], j), j--; } for(int j = 0; j < SIZE(tab3[v[i]]); j++) { if(odw[tab3[v[i]][j]] != odw[v[i]]) usun(tab3[v[i]], j), j--; } } } int korzen(vector <int>& v) { mozna.reset(); REP(i, SIZE(v)) { if(SIZE(tab[v[i]]) == 0) mozna[v[i]] = true; } REP(i, SIZE(v)) { REP(j, SIZE(tab2[v[i]])) { mozna[tab2[v[i]][j]] = false; } } REP(i, SIZE(v)) { if(mozna[v[i]]) return v[i]; } return -1; } void usun_korzen(vector <int>& v, int root) { odw[root] = false; eliminacja(v); } int rek(int poz) { vector <int> tmp; odw.reset(); dfs(poz, tmp); eliminacja(tmp); int root = korzen(tmp), temp; if(root == -1) return -1; usun_korzen(tmp, root); REP(i, SIZE(tmp)) { if(tmp[i] != root) { if(parent[tmp[i]] == -2) { temp = rek(tmp[i]); if(temp == -1) return -1; parent[temp] = root; } } } return root; } int main() { int n, m, a, b, temp; char ch; cin>>n>>m; REP(i, m) { scanf("%d %d %c", &a, &b, &ch); a--; b--; if(ch == 'T') { tab[a].PB(b); tab3[a].PB(b); tab3[b].PB(a); } else { tab2[a].PB(b); } } REP(i, n) vec.PB(i), parent[i] = -2, odw[i] = true; int root = korzen(vec); if(root == -1) { puts("NIE"); return 0; } parent[root] = -1; usun_korzen(vec, root); REP(i, n) { if(i != root) { if(parent[i] == -2) { temp = rek(i); if(temp == -1) { puts("NIE"); return 0; } parent[temp] = root; } } } REP(i, n) { printf("%d\n", parent[i] + 1); } return 0; } |