結果

問題 No.470 Inverse S+T Problem
ユーザー rickythetarickytheta
提出日時 2016-12-20 11:32:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,449 bytes
コンパイル時間 462 ms
コンパイル使用メモリ 51,688 KB
実行使用メモリ 7,008 KB
最終ジャッジ日時 2023-08-24 01:16:15
合計ジャッジ時間 2,152 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,008 KB
testcase_01 AC 4 ms
6,776 KB
testcase_02 AC 4 ms
6,964 KB
testcase_03 AC 4 ms
6,956 KB
testcase_04 AC 5 ms
6,904 KB
testcase_05 AC 5 ms
6,932 KB
testcase_06 AC 11 ms
6,820 KB
testcase_07 AC 11 ms
6,816 KB
testcase_08 AC 11 ms
6,828 KB
testcase_09 AC 4 ms
6,956 KB
testcase_10 AC 5 ms
6,908 KB
testcase_11 AC 6 ms
6,952 KB
testcase_12 AC 4 ms
6,924 KB
testcase_13 AC 5 ms
6,956 KB
testcase_14 AC 7 ms
6,940 KB
testcase_15 AC 6 ms
6,916 KB
testcase_16 AC 5 ms
6,996 KB
testcase_17 AC 5 ms
6,904 KB
testcase_18 AC 5 ms
6,860 KB
testcase_19 AC 5 ms
6,776 KB
testcase_20 AC 4 ms
6,924 KB
testcase_21 AC 6 ms
6,924 KB
testcase_22 AC 5 ms
6,996 KB
testcase_23 AC 6 ms
6,928 KB
testcase_24 AC 6 ms
6,872 KB
testcase_25 AC 6 ms
6,872 KB
testcase_26 AC 6 ms
6,900 KB
testcase_27 AC 6 ms
6,920 KB
testcase_28 AC 5 ms
6,816 KB
testcase_29 AC 4 ms
6,852 KB
testcase_30 AC 5 ms
6,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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