結果

問題 No.470 Inverse S+T Problem
ユーザー DrafearDrafear
提出日時 2018-11-03 14:48:31
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 5,193 bytes
コンパイル時間 2,179 ms
コンパイル使用メモリ 187,960 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2023-08-24 01:34:17
合計ジャッジ時間 4,695 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 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 14 ms
7,552 KB
testcase_30 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:22:17: warning: overflow in conversion from ‘double’ to ‘ll’ {aka ‘short int’} changes value from ‘1.0e+18’ to ‘32767’ [-Woverflow]
 const ll linf = 1e18;
                 ^~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef short ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
  each(x,vec) is >> x;
  return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
  rep(i,vec.size()) {
    if (i) os << " ";
    os << vec[i];
  }
  return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
  rep(i,vec.size()) {
    if (i) os << endl;
    os << vec[i];
  }
  return os;
}

void scc_dfs(ll v, vector<bool>& used, vector<ll>& vs, const vector<vector<ll>>& G) {
  each(to, G[v]) {
    if (used[to]) continue;
    used[to] = true;
    scc_dfs(to, used, vs, G);
  }
  vs.pb(v);
}
// cmpが返る
// 同じcmpは強連結成分
// cmp[i] < cmp[j] なら j から i に行けない
vector<ll> scc(const vector<vector<ll>>& G) {
  const ll n = G.size();
  vector<bool> used(n, false);
  vector<ll> vs;
  rep(i, n) {
    if (used[i]) continue;
    used[i] = true;
    scc_dfs(i, used, vs, G);
  }
  used.assign(n, false);
  vector<vector<ll>> rG(n);
  rep(i, n) {
    each(to, G[i]) {
      rG[to].pb(i);
    }
  }
  vector<ll> res(n);
  ll k = 0;
  rrep(i, vs.size()) {
    if (used[vs[i]]) continue;
    used[vs[i]] = true;
    stack<ll> S;
    S.push(vs[i]);
    while ( !S.empty() ) {
      ll v = S.top(); S.pop();
      res[v] = k;
      each(to, rG[v]) {
        if (!used[to]) {
          used[to] = true;
          S.push(to);
        }
      }
    }
    ++k;
  }
  return res;
}
vector<vector<ll>> get_scc_graph(const vector<ll>& cmp, const vector<vector<ll>>& G) {
  vector<vector<ll>> res(*max_element(all(cmp))+1);
  rep(i, G.size()) {
    each(to, G[i]) {
      if (cmp[i] != cmp[to]) {
        res[cmp[i]].pb(cmp[to]);
      }
    }
  }
  rep(i, res.size()) {
    sort(all(res[i]));
    res[i].erase(unique(all(res[i])), res[i].end());
  }
  return res;
}
class Sat {
  ll n;
  vector<vector<ll>> G;
  ll node_id(ll id, bool b) const {
    return id * 2 + b;
  }
public:
  Sat(ll size) : n(size), G(size*2) {}
  // (id1, b1) => (id2, b2)
  void add(ll id1, bool b1, ll id2, bool b2) {
    G[node_id(id1, b1)].pb(node_id(id2, b2));
    G[node_id(id2, !b2)].pb(node_id(id1, !b1));
  }
  bool check() const {
    vector<ll> cmp = scc(G);
    rep(i, n) {
      if (cmp[node_id(i, true)] == cmp[node_id(i, false)]) {
        return false;
      }
    }
    return true;
  }
  vector<bool> solve() const {
    assert( check() );
    vector<ll> cmp = scc(G);
    vector<bool> used(n, false), res(n);
    vector<P> v;
    rep(i, 2*n) v.eb(cmp[i], i);
    sort(all(v), greater<P>());
    stack<ll> S;
    each(p, v) {
      ll i = p.second;
      if (used[i/2]) continue;
      used[i/2] = true;
      S.push(i);
      while ( !S.empty() ) {
        ll v = S.top(); S.pop();
        res[v/2] = v & 1;
        each(to, G[v]) {
          if (used[to/2]) continue;
          used[to/2] = true;
          S.push(to);
        }
      }
    }
    return res;
  }
};

int main() {
  // {
  //   Sat sat(3);
  //   sat.add(0, true, 1, false);
  //   sat.add(1, false, 1, true);
  //   sat.add(2, true, 0, true);
  //   cout << sat.solve() << endl;
  //   return 0;
  // }
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n; cin >> n;
  vector<string> s(n); cin >> s;
  Sat sat(9*n);
  ll idx = n;
  map<string, vector<pair<ll, bool>>> m;
  rep(i, n) {
    m[s[i].substr(0, 1)].eb(i, false);
    m[s[i].substr(1, 2)].eb(i, false);
    m[s[i].substr(0, 2)].eb(i, true);
    m[s[i].substr(2, 1)].eb(i, true);
  }
  each(p, m) {
    // left
    rep(i, 1, p.second.size()) {
      sat.add(idx+i, true, idx+i-1, true);
    }
    rep(i, p.second.size()) {
      bool b = p.second[i].second;
      if (i > 0) sat.add(p.second[i].first, b, idx+i-1, true);
      sat.add(idx+i, true, p.second[i].first, !b);
    }
    idx += p.second.size();
    // right
    rep(i, 1, p.second.size()) {
      sat.add(idx+i-1, true, idx+i, true);
    }
    rep(i, p.second.size()) {
      bool b = p.second[i].second;
      if (i+1 < p.second.size()) sat.add(p.second[i].first, b, idx+i+1, true);
      sat.add(idx+i, true, p.second[i].first, !b);
    }
    idx += p.second.size();
  }
  if (!sat.check()) {
    cout << "Impossible" << endl;
  }
  else {
    vector<bool> ans = sat.solve();
    rep(i, n) {
      if (!ans[i]) {
        cout << s[i].substr(0, 1) << " " << s[i].substr(1, 2) << '\n';
      }
      else {
        cout << s[i].substr(0, 2) << " " << s[i].substr(2, 1) << '\n';
      }
    }
    cout << flush;
  }
}
0