結果

問題 No.470 Inverse S+T Problem
ユーザー hashiryohashiryo
提出日時 2019-06-08 11:59:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 2,332 bytes
コンパイル時間 2,002 ms
コンパイル使用メモリ 182,848 KB
実行使用メモリ 6,244 KB
最終ジャッジ日時 2023-08-24 01:35:26
合計ジャッジ時間 3,756 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 9 ms
6,100 KB
testcase_07 AC 8 ms
6,100 KB
testcase_08 AC 8 ms
6,244 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0