結果

問題 No.470 Inverse S+T Problem
ユーザー rickythetarickytheta
提出日時 2016-12-20 11:22:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 2,377 bytes
コンパイル時間 1,359 ms
コンパイル使用メモリ 151,104 KB
実行使用メモリ 7,560 KB
最終ジャッジ日時 2023-08-24 01:15:54
合計ジャッジ時間 3,042 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,560 KB
testcase_01 AC 5 ms
7,464 KB
testcase_02 AC 5 ms
7,404 KB
testcase_03 AC 4 ms
7,520 KB
testcase_04 AC 4 ms
7,476 KB
testcase_05 AC 5 ms
7,560 KB
testcase_06 AC 18 ms
7,380 KB
testcase_07 AC 19 ms
7,380 KB
testcase_08 AC 19 ms
7,336 KB
testcase_09 AC 4 ms
7,468 KB
testcase_10 AC 5 ms
7,524 KB
testcase_11 AC 7 ms
7,412 KB
testcase_12 AC 5 ms
7,408 KB
testcase_13 AC 5 ms
7,452 KB
testcase_14 AC 6 ms
7,520 KB
testcase_15 AC 6 ms
7,364 KB
testcase_16 AC 5 ms
7,412 KB
testcase_17 AC 5 ms
7,412 KB
testcase_18 AC 5 ms
7,408 KB
testcase_19 AC 5 ms
7,420 KB
testcase_20 AC 5 ms
7,476 KB
testcase_21 AC 6 ms
7,480 KB
testcase_22 AC 6 ms
7,424 KB
testcase_23 AC 7 ms
7,416 KB
testcase_24 AC 6 ms
7,420 KB
testcase_25 AC 6 ms
7,352 KB
testcase_26 AC 6 ms
7,352 KB
testcase_27 AC 6 ms
7,420 KB
testcase_28 AC 6 ms
7,336 KB
testcase_29 AC 5 ms
7,484 KB
testcase_30 AC 5 ms
7,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

int n;
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)cin>>u[i];
  if(n>=53){
    puts("Impossible");
    return 0;
  }
  // cut a/bc -> true
  // cut ab/c -> false
  bool flag = true;
  REP(i,n)used[i] = -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);
    bool tt = (a!=x)&&(b+c!=y+z);
    bool tf = (a!=z)&&(b+c!=x+y);
    bool ft = (a+b!=y+z)&&(c!=x);
    bool ff = (a+b!=x+y)&&(c!=z);
    // printf("%d %d -> %d %d %d %d\n",i,j,tt,tf,ft,ff);
    if(!tt){
      g[2*i][2*j+1] = true;
      g[2*j][2*i+1] = true;
    }
    if(!tf){
      g[2*i][2*j] = true;
      g[2*j+1][2*i+1] = true;
    }
    if(!ft){
      g[2*i+1][2*j+1] = true;
      g[2*j][2*i] = true;
    }
    if(!ff){
      g[2*i+1][2*j] = true;
      g[2*j+1][2*i] = true;
    }
  }
  // REP(i,2*n){
  //   REP(j,2*n)printf("%2d",g[i][j]);
  //   puts("");
  // }puts("");
  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,2*n){
  //   REP(j,2*n)printf("%2d",g[i][j]);
  //   puts("");
  // }
  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)if(used[i]==-1){
      if(!g[2*i][2*i+1])dfs(i,0);
      else dfs(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