結果
| 問題 |
No.470 Inverse S+T Problem
|
| コンテスト | |
| ユーザー |
btk
|
| 提出日時 | 2017-06-20 16:45:41 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 3,508 bytes |
| コンパイル時間 | 2,039 ms |
| コンパイル使用メモリ | 185,200 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-22 13:44:03 |
| 合計ジャッジ時間 | 3,285 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 27 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define CIN_ONLY if(1)
struct cww{cww(){
CIN_ONLY{
ios::sync_with_stdio(false);cin.tie(0);
}
}}star;
#define fin "\n"
#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define DEBUG if(0)
#define REC(ret, ...) std::function<ret (__VA_ARGS__)>
template <typename T>inline bool chmin(T &l,T r)
{bool a=l>r;if(a)l=r;return a;}
template <typename T>inline bool chmax(T &l,T r)
{bool a=l<r;if(a)l=r;return a;}
template <typename T>
istream& operator>>(istream &is,vector<T> &v){
for(auto &it:v)is>>it;
return is;
}
#define ITR(i,v) for(auto & i :(v))
typedef vector<int> V;
typedef vector<V> VV;
typedef VV Graph;
class SCC{
private:
Graph e,r_e;
V vs;
public:
int size;
V comp;
VV node;
Graph edge;
private:
void dfs(int v){
comp[v]=0;
ITR(u,e[v])if(comp[u]<0)dfs(u);
vs.pb(v);
}
void rdfs(int v,int k){
comp[v]=k;
ITR(u,r_e[v])if(comp[u]<0)rdfs(u,k);
}
void decomposition(){
comp.resize(size);
fill(ALL(comp),-1);
vs.reserve(size);
REP(v,size)if(comp[v]<0)dfs(v);
reverse(ALL(vs));
fill(ALL(comp),-1);
int k=0;
ITR(v,vs)if(comp[v]<0)rdfs(v,k++);
node.resize(k);
edge.resize(k);
REP(v,size){
node[comp[v]].pb(v);
for(auto& u : e[v]){
if(!isSame(v,u)){
edge[comp[v]].pb(comp[u]);
}
}
}
size=k;
}
public:
bool isSame(int a,int b){
return comp[a]==comp[b];
}
SCC(Graph& in){
size=in.size();
e=in;
r_e.resize(size);
REP(v,size)ITR(u,e[v])r_e[u].pb(v);
decomposition();
}
};
void add_edge(Graph &g,int u,int v){
g[u].pb(v);
}
void add_or(Graph& g,V &rev,int u,int v){
add_edge(g,rev[u],v);
add_edge(g,rev[v],u);
}
void add_xor(Graph& g,V &rev,int u,int v){
add_or(g,rev,u,v);
add_or(g,rev,rev[u],rev[v]);
}
int main(){
int N;
cin>>N;
if(N>26*26){
cout<<"Impossible"<<endl;
return 0;
}
vector<string> U(N);
cin>>U;
VV g;
V t(N),f(N),rev(2*N);
REP(i,N){
t[i]=g.size();g.pb({});
f[i]=g.size();g.pb({});
rev[t[i]]=f[i];
rev[f[i]]=t[i];
}
REP(i,N){
REP(j,i){
bool tt=false;
bool tf=false;
bool ft=false;
bool ff=false;
tt|=(U[i][0]==U[j][0]);
ff|=(U[i][2]==U[j][2]);
tf|=(U[i][0]==U[j][2]);
ft|=(U[i][2]==U[j][0]);
tt|=(U[i][1]==U[j][1]&&U[i][2]==U[j][2]);
tf|=(U[i][1]==U[j][0]&&U[i][2]==U[j][1]);
ft|=(U[i][0]==U[j][1]&&U[i][1]==U[j][2]);
ff|=(U[i][0]==U[j][0]&&U[i][1]==U[j][1]);
if(tt)add_or(g,rev,f[i],f[j]);
if(ft)add_or(g,rev,t[i],f[j]);
if(tf)add_or(g,rev,f[i],t[j]);
if(ff)add_or(g,rev,t[i],t[j]);
}
}
SCC scc(g);
REP(i,N){
if(scc.isSame(t[i],f[i])){
cout<<"Impossible"<<endl;
return 0;
}
}
REP(i,N){
cout<<U[i][0];
if(scc.comp[t[i]]<scc.comp[f[i]])cout<<U[i][1]<<" ";
else cout<<" "<<U[i][1];
cout<<U[i][2]<<endl;
}
return 0;
}
btk