#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; namespace { using Integer = long long; //__int128; template istream& operator >> (istream& is, pair& p){return is >> p.first >> p.second;} template istream& operator >> (istream& is, vector& vec){for(T& val: vec) is >> val; return is;} template istream& operator , (istream& is, T& val){ return is >> val;} template ostream& operator << (ostream& os, const pair& p){return os << p.first << " " << p.second;} template ostream& operator << (ostream& os, const vector& vec){for(size_t i=0; i ostream& operator , (ostream& os, const T& val){ return os << " " << val;} template void print(const H& head){ cout << head; } template void print(const H& head, const T& ... tail){ cout << head << " "; print(tail...); } template void println(const T& ... values){ print(values...); cout << endl; } template void eprint(const H& head){ cerr << head; } template void eprint(const H& head, const T& ... tail){ cerr << head << " "; eprint(tail...); } template void eprintln(const T& ... values){ eprint(values...); cerr << endl; } class range{ Integer start_, end_, step_; public: struct range_iterator{ Integer val, step_; range_iterator(Integer v, Integer step) : val(v), step_(step) {} Integer operator * (){return val;} void operator ++ (){val += step_;} bool operator != (range_iterator& x){return step_ > 0 ? val < x.val : val > x.val;} }; range(Integer len) : start_(0), end_(len), step_(1) {} range(Integer start, Integer end) : start_(start), end_(end), step_(1) {} range(Integer start, Integer end, Integer step) : start_(start), end_(end), step_(step) {} range_iterator begin(){ return range_iterator(start_, step_); } range_iterator end(){ return range_iterator( end_, step_); } }; inline string operator "" _s (const char* str, size_t size){ return move(string(str)); } constexpr Integer my_pow(Integer x, Integer k, Integer z=1){return k==0 ? z : k==1 ? z*x : (k&1) ? my_pow(x*x,k>>1,z*x) : my_pow(x*x,k>>1,z);} constexpr Integer my_pow_mod(Integer x, Integer k, Integer M, Integer z=1){return k==0 ? z%M : k==1 ? z*x%M : (k&1) ? my_pow_mod(x*x%M,k>>1,M,z*x%M) : my_pow_mod(x*x%M,k>>1,M,z);} constexpr unsigned long long operator "" _ten (unsigned long long value){ return my_pow(10,value); } inline int k_bit(Integer x, int k){return (x>>k)&1;} //0-indexed mt19937 mt(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()); template string join(const vector& v, const string& sep){ stringstream ss; for(size_t i=0; i0) ss << sep; ss << v[i]; } return ss.str(); } inline string operator * (string s, int k){ string ret; while(k){ if(k&1) ret += s; s += s; k >>= 1; } return ret; } } constexpr long long mod = 9_ten + 7; void TopologicalSort_dfs(vector > &G, vector &res, int node, vector &visit){ if(visit[node] == true) return; visit[node] = true; for(auto itr = G[node].rbegin(); itr != G[node].rend(); itr++){ TopologicalSort_dfs(G, res, *itr, visit); } /* for(int i=0; i TopologicalSort(vector> &G){ int n = G.size(); vector ret; vector visit(n,false); for(int i=0; i > G; vector > rG; void dfs(vector &visit, int pos, vector &result){ visit[pos] = true; for(int i=0; i &visit, int pos, int k){ visit[pos] = true; component[pos] = k; for(int i=0; i > &G){ for(int i=0; i component; vector< vector > scc_graph; SCC(int n){ this->n = n; G.resize(n); rG.resize(n); } SCC(vector > &G){ this->n = G.size(); this->G = G; rG.resize(n); make_rev(G); } void strongly_connected_components(){ vector visit(n, false); vector result; for(int i=0; i=0; i--){ if(visit[ result[i] ]) continue; rdfs(visit, result[i], k); k++; } num_components = k; scc_graph.resize( num_components ); for(int i=0; i> f; vector> G; public: vector ans; bool has_solution; TwoSAT(int num_of_variables) : n(num_of_variables), G(num_of_variables*2), ans(num_of_variables), has_solution(false){ } // 1<=x,y<=num_of_variables // (x OR !y) => add(x, -y) // (x||y) <=> ( !y=>x && !x=>y ) void add(int x, int y){ assert( 1 <= abs(x) && abs(x) <= n); assert( 1 <= abs(y) && abs(y) <= n); x = (x>0)?x-1:n-x-1; y = (y>0)?y-1:n-y-1; int not_x = (x+n)%(n+n); int not_y = (y+n)%(n+n); G[not_x].push_back(y); G[not_y].push_back(x); } // has solution ? true : false bool solve(){ SCC scc(G); scc.strongly_connected_components(); auto d = TopologicalSort(scc.scc_graph); auto c = scc.component; for(int i=0; i c_t( d.size() ); // topological order of component x for(int i=0; i> n; vector s(n); cin >> s; if(n > 26*2){ println("Impossible"); return 0; } map> v; for(int i : range(n)){ v[ s[i].substr(0, 1) ].push_back( i+1 ); v[ s[i].substr(1, 2) ].push_back( i+1 ); v[ s[i].substr(0, 2) ].push_back( -(i+1) ); v[ s[i].substr(2, 1) ].push_back( -(i+1) ); } TwoSAT sat(n); for(auto p : v){ if( p.second.size() < 2 ){ continue; } for(int i=0; i