結果
問題 | No.1914 Directed by Two Sequences |
ユーザー | HYEA LEE |
提出日時 | 2022-01-24 02:17:26 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,554 bytes |
コンパイル時間 | 6,313 ms |
コンパイル使用メモリ | 315,628 KB |
実行使用メモリ | 30,612 KB |
最終ジャッジ日時 | 2024-12-16 01:32:45 |
合計ジャッジ時間 | 74,287 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
22,128 KB |
testcase_01 | AC | 1 ms
13,636 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | TLE | - |
testcase_11 | TLE | - |
testcase_12 | TLE | - |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | TLE | - |
testcase_28 | TLE | - |
testcase_29 | TLE | - |
testcase_30 | TLE | - |
testcase_31 | WA | - |
testcase_32 | TLE | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
ソースコード
#pragma GCC optimize("Ofast") #pragma GCC target("avx2,bmi2,lzcnt,fma") #include <bits/stdc++.h> #include <atcoder/segtree> using namespace std; pair<int, int> op(pair<int, int> a, pair<int, int> b) { return {max(a.first, b.first), min(a.second, b.second)}; } pair<int, int> e() { return {INT_MIN, INT_MAX}; } using Seg = atcoder::segtree<pair<int, int>, op, e>; vector<pair<int, int>> solve(vector<int> A, vector<int> B, vector<pair<int, int>> E) { int N = A.size(); vector<vector<int>> conn(N); for(auto& [u, v]: E) { if(u > v) swap(u, v); conn[u].push_back(v), conn[v].push_back(u); } sort(E.begin(), E.end()); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q; vector<int> deg(N), color(N, -1); for(int i=0; i<N; ++i) deg[i] = conn[i].size(), Q.emplace(deg[i], i); while(!Q.empty()) { int u = Q.top().second; Q.pop(); if(color[u] != -1) continue; vector<bool> exist(conn[u].size()+1); for(int v: conn[u]) { Q.emplace(deg[v]--, v); if(color[v] != -1 && color[v] < (int)exist.size()) exist[color[v]] = 1; } for(int i=0; i<(int)exist.size(); ++i) if(!exist[i]) { color[u] = i; break; } assert(color[u] != -1); } int K = 1+*max_element(color.begin(), color.end()); vector<vector<int>> cliques(K); for(int i=0; i<N; ++i) cliques[color[i]].push_back(i); auto cmp = [&](int x, int y) { if(x == y) return false; if(x < y) return A[x] < B[y]; return B[x] < A[y]; }; function<void(vector<int>::iterator, vector<int>::iterator)> hamiltonian = [&](vector<int>::iterator st, vector<int>::iterator ed) { int N = distance(st, ed); if(N == 1) return; auto mi = st + N / 2; hamiltonian(st, mi); hamiltonian(mi, ed); vector<int> buffer; buffer.reserve(N); auto l = st, r = mi; while(l != mi || r != ed) { if(l == mi) buffer.push_back(*r++); else if(r == ed) buffer.push_back(*l++); else if(cmp(*l, *r)) buffer.push_back(*l++); else buffer.push_back(*r++); } copy(buffer.begin(), buffer.end(), st); return; }; vector<int> idxv(N); vector<pair<int, int>> ans; for(int i=0; i<K; ++i) { hamiltonian(cliques[i].begin(), cliques[i].end()); for(int j=0; j<(int)cliques[i].size()-1; ++j) ans.emplace_back(cliques[i][j], cliques[i][j+1]); for(int j=0; j<(int)cliques[i].size(); ++j) idxv[cliques[i][j]] = j; } for(int i=0; i<N; ++i) sort(conn[i].begin(), conn[i].end(), [&](int a, int b){ return color[a] < color[b]; }); vector<int> ptr(N); Seg seg(2*N); for(int i=0; i<K; ++i) { for(int j=i; j<K; ++j) { vector<pair<int, int>> io(cliques[j].size(), e()); vector<pair<bool, int>> V; for(int k=0; k<(int)cliques[i].size(); ++k) V.emplace_back(false, k); for(int k=0; k<(int)cliques[j].size(); ++k) V.emplace_back(true, k); sort(V.begin(), V.end(), [&](pair<int, int> a, pair<int, int> b) { int A = cliques[a.first?j:i][a.second]; int B = cliques[b.first?j:i][b.second]; if(A != B) return A < B; return a.first < b.first; }); for(auto [tp, idx]: V) { if(tp) { int u = cliques[j][idx]; for(int k=ptr[u]; k<(int)conn[u].size() && color[conn[u][k]] == i; ++k) if(conn[u][k] < u) seg.set(A[conn[u][k]], e()); int maxv = seg.prod(0, B[u]).first; int minv = seg.prod(B[u], 2*N).second; io[idx] = op(io[idx], {maxv, minv}); for(int k=ptr[u]; k<(int)conn[u].size() && color[conn[u][k]] == i; ++k) if(conn[u][k] < u) seg.set(A[conn[u][k]], {idxv[conn[u][k]], idxv[conn[u][k]]}); } else seg.set(A[cliques[i][idx]], {idx, idx}); } for(int x: cliques[i]) seg.set(A[x], e()); assert(seg.all_prod() == e()); sort(V.begin(), V.end(), [&](pair<int, int> a, pair<int, int> b) { int A = cliques[a.first?j:i][a.second]; int B = cliques[b.first?j:i][b.second]; if(A != B) return A > B; return a.first < b.first; }); for(auto [tp, idx]: V) { if(tp) { int u = cliques[j][idx]; for(int k=ptr[u]; k<(int)conn[u].size() && color[conn[u][k]] == i; ++k) if(conn[u][k] > u) seg.set(B[conn[u][k]], e()); int maxv = seg.prod(0, A[u]).first; int minv = seg.prod(A[u], 2*N).second; io[idx] = op(io[idx], {maxv, minv}); for(int k=ptr[u]; k<(int)conn[u].size() && color[conn[u][k]] == i; ++k) if(conn[u][k] > u) seg.set(B[conn[u][k]], {idxv[conn[u][k]], idxv[conn[u][k]]}); } else seg.set(B[cliques[i][idx]], {idx, idx}); } for(int x: cliques[i]) seg.set(B[x], e()); assert(seg.all_prod() == e()); for(int k=0; k<(int)cliques[j].size(); ++k) { auto [from, to] = io[k]; int v = cliques[j][k]; if(0 <= from && from < N && cliques[i][from] != v) ans.emplace_back(cliques[i][from], v); if(0 <= to && to < N && cliques[i][to] != v) ans.emplace_back(v, cliques[i][to]); } } for(int j=0; j<N; ++j) while(ptr[j] < (int)conn[j].size() && color[ptr[j]] == i) ++ptr[j]; } sort(ans.begin(), ans.end()); ans.erase(unique(ans.begin(), ans.end()), ans.end()); return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); int N, M; cin >> N >> M; vector<int> A(N), B(N); for(int&x: A) cin >> x, --x; for(int&x: B) cin >> x, --x; vector<pair<int, int>> E(M); for(auto& [u, v]: E) { cin >> u >> v; --u; --v; } auto ret = solve(A, B, E); cout << ret.size() << '\n'; for(auto [a, b]: ret) cout << a+1 << " " << b+1 << '\n'; }