結果
| 問題 |
No.470 Inverse S+T Problem
|
| コンテスト | |
| ユーザー |
hashiryo
|
| 提出日時 | 2019-06-08 11:59:08 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 7 ms / 2,000 ms |
| コード長 | 2,332 bytes |
| コンパイル時間 | 2,072 ms |
| コンパイル使用メモリ | 185,628 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-12-22 13:53:16 |
| 合計ジャッジ時間 | 3,293 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 27 |
ソースコード
#include<bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge] << '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
const ll INF = INT_MAX;
const ll MOD = 998244353;
struct Edge{
ll dst;
Edge(ll t):dst(t){}
};
using Edges=vector<Edge>;
using Graph=vector<Edges>;
vector<vector<int>> SCC(const Graph& g,vector<int> &I) {
ll n=g.size();
vector<vector<int>> scc;
vector<int> S, B;
I.assign(n,0);
function<void(int)> dfs = [&](int u) {
B.push_back(I[u] = S.size());
S.push_back(u);
for(Edge e: g[u]) {
int v=e.dst;
if (!I[v]) dfs(v);
else while (I[v] < B.back()) B.pop_back();
}
if (I[u] == B.back()) {
scc.push_back({});
B.pop_back();
for (; I[u] < (int)S.size(); S.pop_back()) {
scc.back().push_back(S.back());
I[S.back()] = n + scc.size();
}
}
};
for(int u=0;u<n;u++)if (!I[u]) dfs(u);
for(int u=0;u<n;u++)I[u] -= n+1;
return scc;
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll N;cin>>N;
vector<string> U(N);
for(ll i=0;i<N;i++)cin>>U[i];
if(N>52){
cout<<"Impossible"<<endl;
return 0;
}
Graph g(2*N);
for(ll i=0;i<N;i++){
for(ll j=i+1;j<N;j++){
if(U[i][0]==U[j][0]||U[i].substr(1,2)==U[j].substr(1,2))g[i].emplace_back(N+j),g[j].emplace_back(N+i);
if(U[i][0]==U[j][2]||U[i].substr(1,2)==U[j].substr(0,2))g[i].emplace_back(j),g[N+j].emplace_back(N+i);
if(U[i][2]==U[j][0]||U[i].substr(0,2)==U[j].substr(1,2))g[N+i].emplace_back(N+j),g[j].emplace_back(i);
if(U[i][2]==U[j][2]||U[i].substr(0,2)==U[j].substr(0,2))g[N+i].emplace_back(j),g[N+j].emplace_back(i);
}
}
vector<int> idx;
vector<vector<int>> scc=SCC(g,idx);
bool isok = true;
for(ll i=0;i<N;i++){
isok &= idx[i]!=idx[N+i];
}
if(!isok){
cout<<"Impossible"<<endl;
}else{
for(ll i=0;i<N;i++){
if(idx[i]<idx[i+N]){
cout<<U[i].substr(0,1)<<" "<<U[i].substr(1,2)<<endl;
}else{
cout<<U[i].substr(0,2)<<" "<<U[i].substr(2,1)<<endl;
}
}
}
return 0;
}
hashiryo