結果

問題 No.470 Inverse S+T Problem
ユーザー ir5ir5
提出日時 2024-06-16 23:18:05
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 4,537 bytes
コンパイル時間 3,118 ms
コンパイル使用メモリ 166,232 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-16 23:18:10
合計ジャッジ時間 5,090 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <functional>
#include <iomanip>
using namespace std;
using ll = long long;

class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x<lhs.x;}void operator++(){++x;}};I i,n;
public:range(int n_):i({0}),n({n_}){}range(int i_,int n_):i({i_}),n({n_}){}I& begin(){return i;}I& end(){return n;}};


template<class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p){ return os << "{" << p.first << ", " << p.second << "}"; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& obj) { os << "{"; for (const auto& e : obj) os << e << ", "; return os << "}"; }
template<typename T> ostream& operator<<(ostream& os, const set<T>& obj) { os << "set{"; for (const auto& e : obj) os << e << ", "; return os << "}"; }
template<typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& obj) { os << "map{"; for (const auto& e : obj) os << e << ", "; return os << "}"; }

#ifdef ONLINE_JUDGE
#define dump(expr) ;
#else
#define dump(expr) { cerr << "\033[33m#L" << __LINE__ << ": " << expr << "\033[39m" << endl; }
#endif

// Strongly Connected Components : k := the number of scc, comp_order[id] := its
// topological order [0,k). O(N+M)
struct E {
  int fr, to;
  E(int fr_, int to_) : fr(fr_), to(to_) {}
  friend ostream &operator<<(ostream &os, const E &e) { os << "(" << e.fr << " -> " << e.to << ")"; return os; }
};
typedef vector<E> V;
// typedef vector<V> G;

// note that each vertex will have both enterning edges & entered edges
void add_edge(vector<V> &vg, int fr, int to) {
  vg[fr].push_back(E(fr, to));
  vg[to].push_back(E(fr, to));
}

struct StronglyConnectedComponents {
  vector<char> visited;
  vector<int> t_order;
  vector<int> comp;
  int n_comps;

  StronglyConnectedComponents() {}

  // normal topological sort
  void dfs1(vector<V> &vg, int id, int &k) {
    visited[id] = true;
    for (auto it : vg[id]) if (it.fr == id && !visited[it.to]) dfs1(vg, it.to, k);
    t_order[--k] = id;
  }
  // dfs of reverse order
  void dfs2(vector<V> &vg, int id, int &k) {
    visited[id] = true;
    for (auto it : vg[id]) if (it.to == id && !visited[it.fr]) dfs2(vg, it.fr, k);
    comp[id] = k; // foreach vertex "id", return the topological order
  }

  void exec(vector<V> &vg) {
    int n = (int)vg.size(), k = n;
    // 1st dfs
    visited.assign(n, 0);
    t_order.assign(n, 0);
    comp.assign(n, 0);
    for (int j : range(n)) if (!visited[j]) dfs1(vg, j, k);
    // 2nd dfs
    visited.assign(n, 0);
    for (int i : range(n)) {
      int j = t_order[i];
      if (!visited[j])
        dfs2(vg, j, k), k++;
    }
    n_comps = k;

    dump(comp);
  }
};

struct TwoSAT {
  // negative clause ~x[i] should be represented as ~i (i=0,...,n-1)
  int n;
  vector<V> vg;
  StronglyConnectedComponents scc;
  vector<char> constructed;

  TwoSAT(int n_) : n(n_), vg(2 * n) {}

  void imply(int i, int j) {
    // x[i] => x[j]
    i = (i >= 0) ? (2 * i) : (2 * (~i) + 1);
    j = (j >= 0) ? (2 * j) : (2 * (~j) + 1);
    add_edge(vg, i, j);
  }

  void add_or(int i, int j) {
    // x[i] OR x[j]
    // <=>
    // ~x[i] => x[j] AND ~x[j] => x[i]
    imply(~i, j);
    imply(~j, i);
  }

  bool exec() {
    scc.exec(vg);
    for (int i : range(n)) if (scc.comp[2 * i] == scc.comp[2 * i + 1]) return false;
    constructed.assign(n, 0);
    for (int i : range(n)) constructed[i] = scc.comp[2 * i] > scc.comp[2 * i + 1];
    return true;
  }
};

void solve() {
  int n; cin >> n;
  vector<string> vs(n);
  for (int i : range(n)) cin >> vs[i];

  if (n > 100) {
    cout << "Impossible" << endl;
    return;
  }

  map<string, vector<int>> mp;

  for (int i : range(n)) {
    mp[vs[i].substr(0, 1)].push_back(i);
    mp[vs[i].substr(1, 2)].push_back(i);

    mp[vs[i].substr(0, 2)].push_back(~i);
    mp[vs[i].substr(2, 1)].push_back(~i);
  }
  dump(mp);

  TwoSAT tsat(n);
  for (auto p : mp) {
    for (int i : p.second) for (int j : p.second) {
      if (i == j) break;
      dump(i << " OR " << j)
      tsat.add_or(i, j);
    }
  }

  bool res = tsat.exec();
  if (!res) cout << "Impossible" << endl;
  else {
    for (int i : range(n)) {
      int k = tsat.constructed[i] + 1;
      cout << vs[i].substr(0, k) << " " << vs[i].substr(k) << endl;
    }
  }
}

int main() {
  cout << fixed << setprecision(12);
  solve();
}
0