#include //sort,二分探索,など #include //固定長bit集合 #include //pow,logなど #include //複素数 #include //両端アクセスのキュー #include //ファイルストリーム(標準入力変更用) #include //sortのgreater #include //setprecision(浮動小数点の出力の誤差) #include //入出力 #include //集合演算(積集合,和集合,差集合など) #include //map(辞書) #include //iota(整数列の生成),gcdとlcm(c++17) #include //キュー #include //集合 #include //スタック #include //文字列 #include //イテレータあるけど順序保持しないmap #include //イテレータあるけど順序保持しないset #include //pair #include //可変長配列 //名前 using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef map msi; typedef map msll; typedef pair pii; typedef pair pllll; typedef vector vi; typedef vector vll; typedef vector vs; typedef vector vb; typedef vector> vvi; typedef vector> vvll; typedef vector> vvs; typedef vector> vvb; //定数 const ll MOD = 1000000007; const ll INF = 1000000000000000000; const int MAXR = 100000; //10^5:配列の最大のrange //マクロ #define rep(i,n) for(int i=0;i=0;i--) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define in1(x1) cin >> x1 #define in2(x1, x2) cin >> x1 >> x2 #define in3(x1, x2, x3) cin >> x1 >> x2 >> x3 #define in4(x1, x2, x3, x4) cin >> x1 >> x2 >> x3 >> x4 #define in5(x1, x2, x3, x4, x5) cin >> x1 >> x2 >> x3 >> x4 >> x5 #define in6(x1, x2, x3, x4, x5, x6) cin >> x1 >> x2 >> x3 >> x4 >> x5 >> x6 #define inN(x, N) rep(i, N) in1(x[i]) #define outl(x) cout << x << endl #define out2l(x, y) cout << x << " " << y << endl //よく使う関数 template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } inline ll div_ceil(ll a, ll b) { return (a + (b - 1)) / b; } class TwoSAT { private: const int V; vector > G, rG; vector ps, cmp; void add_edge(int from, int to){ G[from].push_back(to), rG[to].push_back(from); } void dfs(int u){ cmp[u] = 0; for(int v : G[u]){ if(cmp[v] == -1) dfs(v); } ps.push_back(u); } void rdfs(int u, int k){ cmp[u] = k; for(int v : rG[u]){ if(cmp[v] == -1) rdfs(v, k); } } int scc(){ for(int i = 0; i < 2 * V; ++i){ if(cmp[i] == -1) dfs(i); } fill(cmp.begin(), cmp.end(), -1); int k = 0; for(int i = 2 * V - 1; i >= 0; --i){ if(cmp[ps[i]] == -1) rdfs(ps[i], k++); } return k; } public: vector ans; TwoSAT(const int literal_count) : V(literal_count), G(2 * V), rG(2 * V), cmp(2 * V, -1), ans(V){} void add_closure(int x, int y){ add_edge((x + V) % (2 * V), y), add_edge((y + V) % (2 * V), x); } void add_closure(int x, bool tx, int y, bool ty) { if(!tx) x += V; if(!ty) y += V; add_edge((x + V) % (2 * V), y), add_edge((y + V) % (2 * V), x); } // 充足可能性判定 // 真のものは 1,偽のものは 0 が ans に格納される(解の構成) bool satisfy(){ scc(); for(int i = 0; i < V; i++){ if(cmp[i] == cmp[V + i]) return false; ans[i] = (cmp[i] > cmp[V + i]); } return true; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); //標準入力をファイルに変更 //std::ifstream in("input.txt"); //std::cin.rdbuf(in.rdbuf()); int N; in1(N); vs U(N); inN(U, N); map>> m; rep(i, N) { m[U[i].substr(0, 1)].emplace_back(i, true); m[U[i].substr(1, 2)].emplace_back(i, true); m[U[i].substr(0, 2)].emplace_back(i, false); m[U[i].substr(2, 1)].emplace_back(i, false); } int M = 4 * N; TwoSAT ts(N + M); //追加した論理変数の個数 int pos = 0; for(auto [s, v] : m) { for(int i=0;i not v[i] or cnt+pos ts.add_closure(v[i].first, !v[i].second,N + pos, true); if(i){ //(cnt+pos-1が真ならばcnt+posが真) <=> not cnt+pos-1 or cnt+pos ts.add_closure(N + pos - 1, false, N + pos, true); //not(cnt+pos-1が真かつv[i]) <=> not v[i] or not cnt+pos-1 ts.add_closure(v[i].first, !v[i].second, N + pos-1, false); } pos++; } } if(ts.satisfy()) { rep(i, N) { if(ts.ans[i]) { out2l(U[i].substr(0, 1), U[i].substr(1, 2)); } else { out2l(U[i].substr(0, 2), U[i].substr(2, 1)); } } } else { outl("Impossible"); } return 0; }