#include using namespace std; typedef unsigned int uint; typedef long long int ll; typedef unsigned long long int ull; #define debugv(v) printf("L%d %s => ",__LINE__,#v);for(auto e:v){cout< ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<>=1,k++)s=(s<<1)|(u&1);for(;0>=1)cout<<(s&1);}} #define TIME chrono::system_clock::now() #define MILLISEC(t) (chrono::duration_cast(t).count()) template ostream& operator <<(ostream &o,const pair p){o<<"("<> vertex_to; // vector>でも良いかもしれない? vector> vertex_from; Graph(size_t n):n(n),vertex_to(n),vertex_from(n){} void connect(int from, int to){ vertex_to[from].emplace_back(to); vertex_from[to].emplace_back(from); } vector& operator[](int v){ return vertex_to[v]; } void resize(size_t n){ vertex_to.resize(n); vertex_from.resize(n); } size_t degree(int v){ return vertex_to[v].size() + vertex_from[v].size(); } size_t degree_in(int v){ return vertex_from[v].size(); } size_t degree_out(int v){ return vertex_to[v].size(); } void reverse_direction(){ vertex_from.swap(vertex_to); } }; class unionfind { public: vector data; unionfind(int size) : data(size, -1) { } bool union_set(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool find_set(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; // http://www.prefield.com/algorithm/graph/strongly_connected_components.html template void strongly_connected_components_dfs(const Graph &g, int v, U_FIND& result, stack &s, vector &flg, vector &low, vector &num, int& count) { low[v] = num[v] = count++; s.push(v); flg[v] = true; for (int w : g.vertex_to[v]){ if (num[w] == 0) { strongly_connected_components_dfs(g, w, result, s, flg, low, num, count); low[v] = min(low[v], low[w]); } else if (flg[w]){ // ? low[v] = min(low[v], num[w]); } } if (low[v] == num[v]) { while (!s.empty()) { int w = s.top(); s.pop(); flg[w] = false; if (v == w) break; result.union_set(v,w); } } } template void strongly_connected_components(const Graph& graph, U_FIND& result) { int size = graph.n; vector num(size), low(size); stack s; vector flg(size); int count = 1; for (int i=0; i& ord, vector &num, int k){ if (0 <= num[v]) return; num[v] = k; for (int to : graph.vertex_to[v]) _scc_dfs(to, ord, num, k); ord.push_back(v); } public: // 1 <= a <= n OR -1 >= a >= -n // 正ならばx_a,負ならばNOT x_aを表現. void emplace(int a,int b){ // assert(a!=0 && b!=0); graph.connect(_cv(-a),_cv(b)); graph.connect(_cv(-b),_cv(a)); } // TODO:殆どコピペなので読み直し bool solve(vector& result){ int i; vector num(graph.n, -1), ord, dro; for (i=0; i> n; if (52 < n) halt(); cin.ignore(); for (i=1;i<=n;i++){ cin >> data[i]; } sat_2 sat(n); for (i=1;i<=n-1;i++){ for (j=i+1;j<=n;j++){ if (!check(i,j,0b00)) sat.emplace(i,j); if (!check(i,j,0b01)) sat.emplace(i,-j); if (!check(i,j,0b10)) sat.emplace(-i,j); if (!check(i,j,0b11)) sat.emplace(-i,-j); } } vector result; if (!sat.solve(result)) halt(); for (i=1;i<=n;i++){ //cout << result[i]; if (result[i]){ printf("%c %c%c\n",data[i][0],data[i][1],data[i][2]); }else{ printf("%c%c %c\n",data[i][0],data[i][1],data[i][2]); } } return 0; }