結果
問題 | No.470 Inverse S+T Problem |
ユーザー | Haar |
提出日時 | 2019-09-25 20:02:02 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 38 ms / 2,000 ms |
コード長 | 4,686 bytes |
コンパイル時間 | 3,016 ms |
コンパイル使用メモリ | 232,348 KB |
実行使用メモリ | 28,288 KB |
最終ジャッジ日時 | 2024-06-01 22:59:06 |
合計ジャッジ時間 | 4,485 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 38 ms
28,160 KB |
testcase_07 | AC | 37 ms
28,216 KB |
testcase_08 | AC | 37 ms
28,288 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 3 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
testcase_23 | AC | 2 ms
5,376 KB |
testcase_24 | AC | 2 ms
5,376 KB |
testcase_25 | AC | 2 ms
5,376 KB |
testcase_26 | AC | 2 ms
5,376 KB |
testcase_27 | AC | 2 ms
5,376 KB |
testcase_28 | AC | 7 ms
6,528 KB |
testcase_29 | AC | 3 ms
5,376 KB |
testcase_30 | AC | 4 ms
5,376 KB |
ソースコード
#include <bits/stdc++.h> #define FOR(v, a, b) for(int v = (a); v < (b); ++v) #define FORE(v, a, b) for(int v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define REPE(v, n) FORE(v, 0, n) #define REV(v, a, b) for(int v = (a); v >= (b); --v) #define ALL(x) (x).begin(), (x).end() #define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it) #define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(c,x) ((c).find(x) != (c).end()) #define LLI long long int #define fst first #define snd second #ifdef DEBUG #include <misc/C++/Debug.cpp> #else #define dump(x) ((void)0) #endif #define gcd __gcd using namespace std; template <class T> constexpr T lcm(T m, T n){return m/gcd(m,n)*n;} template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;} template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;} template <typename T, typename U> istream& operator>>(istream &is, pair<T,U> &p){is >> p.first >> p.second; return is;} template <typename T, typename U> bool chmin(T &a, const U &b){return (a>b ? a=b, true : false);} template <typename T, typename U> bool chmax(T &a, const U &b){return (a<b ? a=b, true : false);} template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);} class TwoSat{ int n; vector<vector<int>> g, rg; int f(int i){ assert(i != 0); assert(abs(i) <= n); if(i > 0) return i-1; else return abs(i)-1 + n; } public: TwoSat(int n): n(n), g(2*n), rg(2*n){} /** * @brief a→bを導入する */ void add_if(int a, int b){ g[f(a)].push_back(f(b)); rg[f(b)].push_back(f(a)); } /** * @brief a∨bを導入する * @note a ∨ b <=> (!a => b) ∧ (!b => a) */ void add_or(int a, int b){ add_if(-a, b); add_if(-b, a); } /** * @brief ¬(a∧b)を導入する * @note !(A ∧ B) <=> (!A ∨ !B) */ void not_coexist(int a, int b){ add_or(-a, -b); } private: // scc用 vector<bool> visit; vector<int> check; vector<int> scc; void dfs(int cur){ visit[cur] = true; for(auto e : g[cur]){ if(not visit[e]) dfs(e); } check.push_back(cur); } void rdfs(int cur, int i){ scc[cur] = i; for(auto e : rg[cur]){ if(scc[e] == -1) rdfs(e,i); } } vector<int> tsort(const vector<vector<int>> &graph){ int n = graph.size(); vector<int> indeg(n); REP(i,n){ for(auto e : graph[i]){ ++indeg[e]; } } stack<int> st; REV(i,n-1,0){ if(indeg[i] == 0) st.push(i); } vector<int> ret; while(!st.empty()){ int cur = st.top(); st.pop(); ret.push_back(cur); for(auto e : graph[cur]){ --indeg[e]; if(indeg[e]==0){ st.push(e); } } } return ret; } public: bool solve(vector<bool> &ret){ visit.assign(2*n, false); scc.assign(2*n, -1); REP(i,2*n) if(!visit[i]) dfs(i); reverse(ALL(check)); int m = 0; for(auto c : check) if(scc[c] == -1) {rdfs(c,m); ++m;} REP(i,n) if(scc[i] == scc[i+n]) return false; vector<vector<int>> g2(m); REP(i,n*2){ for(auto j : g[i]){ if(scc[i] != scc[j]) g2[scc[i]].push_back(scc[j]); } } auto ts = tsort(g2); vector<int> r(m); REP(i,m) r[ts[i]] = i; ret = vector<bool>(n); REP(i,n) ret[i] = r[scc[i]] > r[scc[i+n]]; return true; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; while(cin >> N){ vector<string> U(N); cin >> U; vector<string> S1(N), T1(N), S2(N), T2(N); REP(i,N){ S1[i] = {U[i][0], U[i][1]}; T1[i] = {U[i][2]}; S2[i] = {U[i][0]}; T2[i] = {U[i][1], U[i][2]}; } TwoSat sat(N); set<char> cnt; for(auto &s : U) for(auto &c : s) cnt.insert(c); if((int)cnt.size() < N){ cout << "Impossible" << endl; continue; } FORE(i,1,N){ FORE(j,i+1,N){ if(S1[i-1] == S1[j-1] or T1[i-1] == T1[j-1]) sat.not_coexist(i, j); if(S1[i-1] == T2[j-1] or T1[i-1] == S2[j-1]) sat.not_coexist(i, -j); if(S2[i-1] == T1[j-1] or T2[i-1] == S1[j-1]) sat.not_coexist(-i, j); if(S2[i-1] == S2[j-1] or T2[i-1] == T2[j-1]) sat.not_coexist(-i, -j); } } vector<bool> ret; bool ans = sat.solve(ret); if(ans){ REP(i,N){ if(ret[i]) cout << S1[i] << " " << T1[i] << endl; else cout << S2[i] << " " << T2[i] << endl; } }else{ cout << "Impossible" << endl; } } return 0; }