結果
問題 | No.470 Inverse S+T Problem |
ユーザー | SHIJOU |
提出日時 | 2020-10-27 18:03:39 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 7,012 bytes |
コンパイル時間 | 2,979 ms |
コンパイル使用メモリ | 233,536 KB |
実行使用メモリ | 814,516 KB |
最終ジャッジ日時 | 2024-06-01 23:05:41 |
合計ジャッジ時間 | 5,066 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | WA | - |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | MLE | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
ソースコード
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using namespace std; using ll = int64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template<class T> using PQ = priority_queue<T>; template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >; const int INF = 0xccccccc; const ll LINF = 0xcccccccccccccccLL; template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;} template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;} struct SCC { struct csr { vector<int> start, elist; csr(int n, const vector<pair<int, int>> &edges) : start(n+1), elist(edges.size()) { for(pair<int, int> e:edges) { start[e.first+1]++; } for(int i = 1; i <= n; i++) { start[i] += start[i-1]; } vector<int> counter = start; for(pair<int, int> e:edges) { elist[counter[e.first]++] = e.second; } } }; int n; vector<pair<int, int>> edges; SCC(int n_) : n(n_) {} void add(int from, int to) {edges.emplace_back(from, to);} pair<int, vector<int>> scc_ids() { csr g(n, edges); int now_ord = 0, group_num = 0; vector<int> used, low(n), ord(n, -1), ids(n); used.reserve(n); auto dfs = [&](auto self, int v)->void { low[v] = ord[v] = now_ord++; used.push_back(v); for(int i = g.start[v]; i < g.start[v+1]; i++) { int to = g.elist[i]; if(ord[to] == -1) { self(self, to); low[v] = min(low[v], low[to]); } else { low[v] = min(low[v], ord[to]); } } if(low[v] == ord[v]) { while(true) { int u = used.back(); used.pop_back(); ord[u] = n; ids[u] = group_num; if(u == v) break; } group_num++; } }; for(int i = 0; i < n; i++) { if(ord[i] == -1) dfs(dfs, i); } for(int &x:ids) { x = group_num - 1 - x; } return {group_num, ids}; } vector<vector<int>> scc() { pair<int, vector<int>> ids = scc_ids(); int group_num = ids.first; vector<int> counts(group_num); for(int x:ids.second) counts[x]++; vector<vector<int>> groups(group_num); for(int i = 0; i < group_num; i++) { groups[i].reserve(counts[i]); } for(int i = 0; i < n; i++) { groups[ids.second[i]].push_back(i); } return groups; } }; //SCC is needed struct two_sat { int n; vector<bool> boolean_val; SCC scc; two_sat():n(0), scc(0) {} two_sat(int n_):n(n_), boolean_val(n_), scc(n_<<1) {} void add(int i, bool ii, int j, bool jj) { scc.add(i + (ii?0:n), j + (jj?n:0)); scc.add(j + (jj?0:n), i + (ii?n:0)); } bool satisfiable() { vector<int> id = scc.scc_ids().second; for(int i = 0; i < n; i++) { if(id[i] == id[i+n]) return false; boolean_val[i] = id[i] < id[i+n]; } return true; } }; #undef _GLIBCXX_DEBUG string to_string(const string &s) {return '"' + s + '"';} string to_string(const char *s) {return to_string(string(s));} string to_string(bool b) {return b?"true":"false";} string to_string(vector<bool> v) { string res = "{"; for(int i = 0; i < int(v.size()); i++) { if(i) res += ", "; res += to_string(v[i]); } res += "}"; return res; } template<size_t N> string to_string(bitset<N> v) { string res; for(size_t i = 0; i < N; i++) res += char('0' + v[i]); return res; } template<class A, class B> string to_string(pair<A, B>); template<class A, class B, class C> string to_string(tuple<A, B, C>); template<class A, class B, class C, class D> string to_string(tuple<A, B, C, D>); template<class A> string to_string(A v) { bool first = true; string res = "{"; for(const auto &x:v) { if(!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; } template<class A, class B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template<class A, class B, class C> string to_string(tuple<A, B, C> t) { return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ")"; } template<class A, class B, class C, class D> string to_string(tuple<A, B, C, D> t) { return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ")"; } void debug_out() {cerr << endl;} template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 822 #endif //head void imp() { cout << "Impossible\n"; } int pl(vs::iterator itb, vs::iterator ite, string s) { auto itr = upper_bound(itb, ite, s); itr--; int res = itr-itb; (*itr).back()++; return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vs s(n), s_(n), s__(n); rep(i, n) { cin >> s[i]; s_[i] = s[i]; s__[i] = s[i]; reverse(all(s__[i])); } sort(all(s_)); sort(all(s__)); rep(i, n) { auto it = equal_range(all(s_), s_[i]); if(it.second-it.first > 2) { imp(); return 0; } } two_sat ts(n); vector<P> p(n); vi _(n), __(n); rep(i, n) { string x = s[i]; int u = pl(all(s_), x); reverse(all(x)); int v = pl(all(s__), x); p[i] = {u, v}; _[u] = i; __[v] = i; } debug(p); debug(_); debug(__); rep(i, n) { int j = p[i].first+1; while(j < n and s_[j][0] == s[i][0]) { ts.add(i, true, _[j++], true); } auto itr = lower_bound(all(s_), ""+s[i][2]); j = itr-s_.begin(); while(j < n and s_[j][0] == s[i][2]) { ts.add(i, false, _[j++], true); } j = p[i].second+1; while(j < n and s__[j][0] == s[i][2]) { ts.add(i, false, __[j++], false); } itr = lower_bound(all(s__), ""+s[i][0]); j = itr-s__.begin(); while(j < n and s__[j][0] == s[i][0]) { ts.add(i, true, __[j++], false); } string ss = s[i].substr(0, 2); j = p[i].first+1; while(j < n and s_[j].substr(0, 2) == ss) { ts.add(i, false, _[j++], false); } reverse(all(ss)); itr = lower_bound(all(s__), ss); j = itr-s__.begin(); while(j < n and s__[j].substr(0, 2) == ss) { ts.add(i, false, __[j++], true); } ss = s[i].substr(1); itr = lower_bound(all(s_), ss); j = itr-s_.begin(); while(j < n and s_[j].substr(0, 2) == ss) { ts.add(i, true, _[j++], false); } reverse(all(ss)); j = p[i].second+1; while(j < n and s__[j].substr(0, 2) == ss) { ts.add(i, true, __[j++], true); } } if(!ts.satisfiable()) { imp(); return 0; } rep(i, n) { if(ts.boolean_val[i]) { cout << s[i].substr(0, 2) << ' ' << s[i][2] << '\n'; } else { cout << s[i][0] << ' ' << s[i].substr(1) << '\n'; } } }