結果

問題 No.470 Inverse S+T Problem
コンテスト
ユーザー rickytheta
提出日時 2016-12-20 11:32:17
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,449 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 412 ms
コンパイル使用メモリ 66,324 KB
実行使用メモリ 7,808 KB
最終ジャッジ日時 2026-05-28 19:34:45
合計ジャッジ時間 1,877 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cstdio>
#include <string>

using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);++i)

int n;
char buf[25];
string u[125252];
string s[53],t[53];

bool g[125][125];

int used[53];
void dfs(int p,int v){
  used[p] = v;
  int from = 2*p+v;
  REP(to,2*n){
    if(!g[from][to])continue;
    if(used[to/2]!=-1)continue;
    dfs(to/2,to%2);
  }
}

int main(){
  scanf("%d",&n);
  REP(i,n){
    scanf("%s",buf);
    u[i] = string(buf);
  }
  if(n>=53){
    puts("Impossible");
    return 0;
  }
  // cut a/bc -> true(0)
  // cut ab/c -> false(1)
  REP(i,n)REP(j,i){
    string a = u[i].substr(0,1);
    string b = u[i].substr(1,1);
    string c = u[i].substr(2,1);
    string x = u[j].substr(0,1);
    string y = u[j].substr(1,1);
    string z = u[j].substr(2,1);
    REP(o,2)REP(p,2){
      bool ok = ((o==0?a:c)!=(p==0?x:z)) && ((o==0?b+c:a+b)!=(p==0?y+z:x+y));
      if(!ok)g[2*i+o][2*j+p^1] = g[2*j+p][2*i+o^1] = true;
    }
  }
  bool flag = true;
  REP(k,2*n)REP(i,2*n)REP(j,2*n)g[i][j]=g[i][j]||(g[i][k]&&g[k][j]);
  REP(i,n)if(g[2*i][2*i+1] && g[2*i+1][2*i])flag = false;
  if(!flag){
    puts("Impossible");
  }else{
    REP(i,n)used[i] = -1;
    REP(i,n)if(used[i]==-1)dfs(i,g[2*i][2*i+1]);
    REP(i,n){
      if(used[i]==0){
        printf("%s %s\n",u[i].substr(0,1).c_str(), u[i].substr(1,2).c_str());
      }else{
        printf("%s %s\n",u[i].substr(0,2).c_str(), u[i].substr(2,1).c_str());
      }
    }
  }
  return 0;
}
0