#line 1 "base.hpp"//2 #ifndef __clang__ #pragma GCC optimize ("O3") #endif void solve( /* この関数に問題ごとの処理を書く */ #ifdef GCJ_CASE long long case_id #endif ); #if defined(EBUG) && !defined(ONLINE_JUDGE) #define debug true #else #define NDEBUG #define debug false #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using LL=long long; using ULL=unsigned long long; #define int LL #define CS const #define CX constexpr #define IL inline #define OP operator #define RT return #define TL template #define TN typename #define lambda [&] #define rep(f,t,i,c,u)for(int Rb_=(t),i=(f);i c Rb_;u(i)) #define upto(f,t,i)rep(f,t,i,<=,++) #define uptil(f,t,i)rep(f,t,i,<,++) #define downto(f,t,i)rep(f,t,i,>=,--) #define downtil(f,t,i)rep(f,t,i,>,--) #define times(n,i)uptil(0,n,i) #define rtimes(n,i)downto((n)-1,0,i) #define iter(v)begin(v),end(v) #define citer(v)cbegin(v),cend(v) #define riter(v)rbegin(v),rend(v) #define criter(v)crbegin(v),crend(v) #define IF(a,b,c)((a)?(b):(c)) #if debug #define _GLIBCXX_DEBUG #define _LIBCPP_DEBUG 2 #define _LIBCPP_DEBUG2 2 #define ln <,f1,f2,x)CMPOP(t,>=,f1,f2,x) #line 1 "mod.hpp"//2b #ifdef MOD #if !defined(FORCE_MOD)&&MOD!=1000000007&&MOD!=1000000009&&MOD!=998244353 #error unknown mod MOD and FORCE_MOD not defined. #endif #else #define MOD 1000000007 #endif #line 1 "power.hpp"//2bm using int128=__int128; TLT power(T x,int n){T rt(1);for(;n;n/=2){if(n%2)rt*=x;x*=x;}RT rt;} int pow_mod(int x,int n,int m){int rt=1;for(;n;n/=2){if(n%2)rt=rt*x%m;x=x*x%m;}RT rt;} int128 pow_mod_64(int128 x,int n,int m){int128 rt=1;for(;n;n/=2){if(n%2)rt=rt*x%m;x=x*x%m;}RT rt;} #line 3001 "mod.hpp"//2b IL CX int modulo(int a,int b){a%=b;RT a==0||(a>0)==(b>0)?a:a+b;} IL CX int divide(int a,int b){RT(a-modulo(a,b))/b;} TLclass MInt{ /*!https://ei1333.github.io/luzhiled/snippets/other/mod-int.html*/ public:int val; CX MInt():val(0){}explicit CX MInt(int v):val(modulo(v,mod)){} MInt&OP+=(CS MInt&m){val+=m.val;if(val>=mod)val-=mod;RT*this;} MInt&OP-=(CS MInt&m){val-=m.val;if(val<0)val+=mod;RT*this;}MInt&OP*=(CS MInt&m){val=val*m.val%mod;RT*this;} MInt&OP/=(CS MInt&m){val=val*m.inv().val%mod;RT*this;} BINOP_ASGN(MInt,MInt,+)BINOP_ASGN(MInt,MInt,-)BINOP_ASGN(MInt,MInt,*)BINOP_ASGN(MInt,MInt,/) MInt OP-()CS{MInt m;m.val=val?mod-val:0;RT m;}bool OP==(CS MInt&m)CS{RT val==m.val;} bool OP!=(CS MInt&m)CS{RT val!=m.val;} //MInt pow(int n)CS{MInt x(*this),rt(1);while(n){if(n%2)rt*=x;x*=x;n/=2;}RT rt;} MInt pow(int n)CS{RT power(*this,n);} MInt inv()CS{int a=val,b=mod,x=1,y=0,t;while(b){t=a/b;swap(b,a-=t*b);swap(y,x-=t*y);}RT(MInt)x;} friend ostream&OP<<(ostream&o,CS MInt&m){RT o<>(istream&i,MInt&m){int v;i>>v;m=MInt(v);RT i;} };using mint=MInt<>;CX mint OP"" _m(ULL n){RT mint(n);} CX MInt<998244353>OP"" _m998244353(ULL n){RT MInt<998244353>(n);} CX MInt<1000000007>OP"" _m1e9_7(ULL n){RT MInt<1000000007>(n);} CX MInt<1000000009>OP"" _m1e9_9(ULL n){RT MInt<1000000009>(n);} #line 1 "typedefs.hpp"//2b // struct unit{}; using unit = tuple<>; using int128=__int128; using LD=long double; TLusing vec=vector; TLusing vvec=vec>; TLusing vvvec=vec>; TLusing vvvvec=vec>; using WI=vvec;using VI=vec;using VB=vec;using PBI=pair;using PBVB=pair>;using WPBI=vvec>;using VS=vec; #line 1 "alias.hpp"//2b #define EB emplace_back #define PB push_back #define foldl accumulate #define scanl partial_sum #line 1 "util.hpp"//2b TLIL bool amax(T&v,CS T&a){RT vIL bool amin(T&v,CS T&a){RT v>a&&(v=a,true);} #ifndef __cpp_lib_exchange_function #define exchange exchange_RAB TLIL T exchange(T& t, U&& u){T x=move(t);t=forward(u);RT x;} #endif #ifndef __cpp_lib_clamp #define clamp clamp_RAB TLIL CX CS T&clamp(CS T&a,CS T&mn,CS T&mx){RT amx?mx:a;} #endif TLIL int size_RAB(T t){RT t.size();} #define size size_RAB TLIL void uniq_after_sort(V&v){v.erase(unique(iter(v)),v.end());} TLIL void sort_and_uniq(V&v){sort(iter(v));v.erase(unique(iter(v)),v.end());} TLIL auto leftmost_ge(CS V&v,CS K&k){RT v.lower_bound(k);} TLIL auto leftmost_gt(CS V&v,CS K&k){RT v.upper_bound(k);} namespace rab{ TLIL void append(V&v,CS W&w){copy(citer(w),back_inserter(v));} TLIL auto flatten(CS V&xss,int reserve_size=0)->TN V::value_type{ decltype(flatten(xss))ret; ret.reserve(reserve_size); for(CS auto&xs:xss)append(ret,xs); ret.shrink_to_fit(); RT move(ret); } TLIL bool is_in(I x,I l,I r){RT l<=x&&xIL T fetch(CS T&d,CS vec&v,int i){RT 0<=i&&iIL T fetch(CS T&d,CS vvec&v,int i,int j){ RT 0<=i&&iIL T fetch(CS T&d,CS vec>&v,int i,I...j){ // RT 0<=i&&istruct Compressed{int size;mapzip;vecunzip;}; TLIL Compressedcompressed(vecv){ sort_and_uniq(v);mapzip;times(size(v),i)zip[v[i]]=i;RT{size(v),zip,move(v)}; } TLstruct CompressedSrc{int size;mapzip;vecunzip;WI src;}; TLIL CompressedSrccompressed_src(CS vec&v){ auto c=compressed(v);VI src(c.size);times(size(v),i)src[c.zip[v[i]]].PB(i);RT{c.size,c.zip,c.unzip,src}; } struct identity{TLU OP()(U&&v)CS{RT v;}}; } #line 1 "debug.hpp"//2b TLIL istream&OP>>(istream&s,vec&v){for(auto&&p:v)s>>p;RT s;} TLIL ostream&OP<<(ostream&s,CS pair&p){RT s<<"("<Rdebug1(' ',vec)TLRdebug1(' ',map) TLRdebug1('\n',vvec)TLRdebug1('\n',vec>) #line 6001 "base.hpp"//2 signed main(){ if(debug)cerr<<"MOD: "<>T;times(T,t){cout<<"Case #"<,class Inverser=negate>class UnionFind{ /*! http://noshi91.hatenablog.com/entry/2018/05/30/191943 https://en.wikipedia.org/wiki/Disjoint-set_data_structure https://qiita.com/drken/items/cce6fc5c579051e64fab */ public:int n,*parents,*sizes;T*pot_diffs;bool to_delete; Adder adder;Inverser inverser;explicit UnionFind(int n,bool to_delete=false): n(n),parents(new int[n]),sizes(new int[n]),pot_diffs(new T[n]),to_delete(to_delete) {clear();}void clear(){ times(n,i)parents[i]=i;fill(sizes,sizes+n,1);fill(pot_diffs,pot_diffs+n,0);} ~UnionFind(){if(to_delete){delete[]parents;delete[]sizes;delete[]pot_diffs;}} int size(){RT n;}int root(int i){int p=parents[i];if(p==i)RT i; int r=root(p);pot_diffs[i]+=pot_diffs[p];RT parents[i]=r;}bool is_same(int i,int j){RT root(i)==root(j);} bool is_all_same(){int r=root(0);uptil(1,n,i)if(root(i)!=r)RT 0;RT 1;} bool merge(int i,int j,T pdiff=0){i=root(i);j=root(j);if(i==j)RT 0; if(sizes[i]>sizes[j]){swap(i,j);pdiff=inverser(pdiff);}parents[i]=j;sizes[j]+=sizes[i];pot_diffs[i]=pdiff;RT 1; }T diff(int i,int j){root(i);root(j); RT adder(pot_diffs[i],inverser(pot_diffs[j]));}};using unionfind=UnionFind<>; #line 2001 "graph.hpp"//2s TLstruct Edge{int from;int to;E weight; Edge(int from,int to,E weight):from(from),to(to),weight(weight){} CMPOPS(Edge,make_tuple(weight,from,to),make_tuple(e.weight,e.from,e.to),e) };TLclass Graph{protected:int nv_,nde_;unionfind uf;public: vvec>edges;Graph(int nv):nv_(nv),nde_(0),uf(nv),edges(nv){} IL int nv()CS{RT nv_;}IL int nde()CS{RT nde_;} IL int nue()CS{RT nde_/2;} IL void add_dedge(int i,int j,CS E&val){if(debug&&(i<0||nv_<=i||j<0||nv_<=j)){ cerr<<"invalid index:("<struct SCCer{int n;Graphscc;CS Graph&g;Graphgrev;VB visited;VI po,zip; WI unzip; SCCer(CS Graph&g):n(g.nv()),scc(0),g(g),grev(n),visited(n),zip(n,-1){ po.reserve(n);times(n,i)for(CS auto&e:g.edges[i]){grev.add_dedge(e.to,e.from,{});} }private:void po_dfs(int i){if(visited[i])RT; visited[i]=1;for(CS auto&e:g.edges[i])po_dfs(e.to);po.PB(i);} void zip_rdfs(int i,int z){if(~zip[i])RT;zip[i]=z;for(CS auto&e:grev.edges[i])zip_rdfs(e.to,z); }public:void exec(){times(n,i)po_dfs(i); int z=0;for(auto it=po.rbegin();it!=po.rend();++it)if(zip[*it]==-1)zip_rdfs(*it,z++);scc=decltype(scc)(z); unzip.resize(z);for(auto it=po.rbegin();it!=po.rend();++it){int x=zip[*it];unzip[x].PB(*it); for(CS auto&e:g.edges[*it]){int y=zip[e.to];if(x!=y)assert(xg(n*2);for(auto&ts:sat){switch(size(ts)){ case 0:RT{0,{}};case 1:g.add_dedge(sat2_y(n,ts[0]),sat2_x(n,ts[0]),{}); break;case 2: g.add_dedge(sat2_y(n,ts[0]),sat2_x(n,ts[1]),{});g.add_dedge(sat2_y(n,ts[1]),sat2_x(n,ts[0]),{}); break;default:cerr<<"invalid sat";exit(1);}}SCCersccer(g);sccer.exec();VB ans(n); times(n,i){int x=sccer.zip[i],y=sccer.zip[i+n];if(x==y)RT{0,{}};ans[i]=x>y; }RT{1,ans};} #line 3001 "2.cpp"// void solve() { // NN("U") int N;cin>>N;VS U(N);times(N,Ri_0){cin>>U[Ri_0];} if(N > 52 * 52 + 52) { cout << dict::Impossible ln; return; } WPBI sat; times(N, i) times(N, j) if(i != j) { const auto &a = U[i], &b = U[j]; if(i < j) { if(a[0] == b[0]) { // S[i]=S[j] if !vi && !vj sat.PB({{true, i}, {true, j}}); if(a[1] == b[1]) { // S[i]=S[j] if vi && vj sat.PB({{false, i}, {false, j}}); } } if(a[2] == b[2]) { // T[i]=T[j] if vi && vj sat.PB({{false, i}, {false, j}}); if(a[1] == b[1]) { // T[i]=T[j] if !vi && !vj sat.PB({{true, i}, {true, j}}); } } } if(a[0] == b[2]) { // S[i] == T[j] if !vi && vj sat.PB({{true, i}, {false, j}}); } if(a[0] == b[1] && a[1] == b[2]) { // S[i] == T[j] if vi && !vj sat.PB({{false, i}, {true, j}}); } } auto a = sat2(N, sat); if(!a.first) { cout << dict::Impossible ln; return; } times(N, i) { if(a.second[i]) { cout << U[i][0] << U[i][1] sp << U[i][2] ln; } else { cout << U[i][0] sp << U[i][1] << U[i][2] ln; } } }