結果

問題 No.470 Inverse S+T Problem
ユーザー たこしたこし
提出日時 2017-05-16 12:32:34
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 3,844 bytes
コンパイル時間 1,742 ms
コンパイル使用メモリ 167,836 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 01:28:49
合計ジャッジ時間 3,980 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 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 2 ms
4,380 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 RE -
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

using namespace std;

//変数は0-index
//trueなx_iはi , falseなx_iはi+XSizeのインデックスで管理する
class SAT_2{
public:
  SAT_2(int aN)
  {
    init(aN);
  }

  //変数の数
  void init(int aN)
  {
    XSize = aN;
    VecSize = 2 * XSize;
    Edge.resize(VecSize);
    rEdge.resize(VecSize);
    used.resize(VecSize);
    vs.clear();
    cmp.resize(VecSize);
  }

  int revTF(int a)
  {
    return (a + XSize) % (2 * XSize);
  }

  //節を登録
  //(¬x_i v x_j)ならreg(i + XSize, j)を呼び出す
  void reg(int l, int r)
  {
    int tmpL = l, tmpR = r;

    tmpL = revTF(tmpL);
    Edge[tmpL].push_back(tmpR);
    rEdge[tmpR].push_back(tmpL);

    tmpL = r, tmpR = l;
    tmpL = revTF(tmpL);
    Edge[tmpL].push_back(tmpR);
    rEdge[tmpR].push_back(tmpL);
  }

  void dfs(int v)
  {
    used[v] = true;
    for (int i = 0; i < Edge[v].size(); i++) {
      if(!used[Edge[v][i]]){
        dfs(Edge[v][i]);
      }
    }
    vs.push_back(v);
  }

  void rdfs(int v, int k){
    used[v] = true;
    cmp[v] = k;
    for (int i = 0; i < rEdge[v].size(); i++) {
      if(!used[rEdge[v][i]]){
        rdfs(rEdge[v][i], k);
      }
    }
  }

  int scc()
  {
    for (int i = 0; i < used.size(); i++) {
      used[i] = false;
    }
    vs.clear();
    for (int i = 0; i < VecSize; i++) {
      if(!used[i]) dfs(i);
    }

    for (int i = 0; i < used.size(); i++) {
      used[i] = false;
    }

    int k = 0;
    for (int i = vs.size() - 1; 0 <= i; i--) {
      if(!used[vs[i]]){
        rdfs(vs[i], k++);
      }
    }

    return k;
  }

  bool checkSatisfy()
  {
    scc();

    for (int i = 0; i < XSize; i++) {
      if(cmp[i] == cmp[i+XSize]){
        return false;
      }
    }

    return true;
  }

  //x_lの真偽値を取得 たぶんまだ出来てない
  bool getTF(int l)
  {
    assert(0 <= l && l < XSize);
    return cmp[l] > cmp[l+XSize];
  }

  void debug()
  {
    for (int i = 0; i < VecSize; i++) {
      cerr << cmp[i] << endl;
    }
  }

private:
  vector<vector<int>> Edge;
  vector<vector<int>> rEdge;
  vector<bool> used;
  vector<int> vs; //帰りがけ順の並び
  vector<int> cmp; //属する連結成分のトポロジカル順序

  int XSize;
  int VecSize;
};

#define MAX_N 10005

int N;
string U[MAX_N];

int main()
{

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> U[i];
  }

  if(N > 52){
    cout << "Impossible" << endl;
    return 0;
  }

  SAT_2 sat(N);
  for (int i = 0; i < N; i++) {
    for (int j = i+1; j < N; j++) {
      if(i == j){
        continue;
      }
      string A1, A2, B1, B2;

      A1 = U[i].substr(0,1), A2 = U[i].substr(1, 2), B1 = U[j].substr(0,1), B2 = U[j].substr(1,2);
      if(A1 == B1 || A2 == B2){
        sat.reg(i+N, j+N);
      }

      A1 = U[i].substr(0,1), A2 = U[i].substr(1, 2), B1 = U[j].substr(0,2), B2 = U[j].substr(2,1);
      if(A1 == B2 || A2 == B1){
        sat.reg(i+N, j);
      }

      A1 = U[i].substr(0,2), A2 = U[i].substr(2, 1), B1 = U[j].substr(0,1), B2 = U[j].substr(1,2);
      if(A1 == B2 || A2 == B1){
        sat.reg(i, j+N);
      }

      A1 = U[i].substr(0,2), A2 = U[i].substr(2, 1), B1 = U[j].substr(0,2), B2 = U[j].substr(2,1);
      if(A1 == B1 || A2 == B2){
        sat.reg(i, j);
      }

    }
  }

  if(!sat.checkSatisfy()){
    cout << "Impossible" << endl;
  }
  else{
    for (int i = 0; i < N; i++) {
      if(sat.getTF(i)){
        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