結果
問題 | No.529 帰省ラッシュ |
ユーザー | mamekin |
提出日時 | 2017-06-11 01:51:08 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 10,629 bytes |
コンパイル時間 | 2,442 ms |
コンパイル使用メモリ | 154,420 KB |
実行使用メモリ | 47,292 KB |
最終ジャッジ日時 | 2024-09-24 15:48:05 |
合計ジャッジ時間 | 11,466 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,812 KB |
testcase_02 | RE | - |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | AC | 469 ms
46,364 KB |
testcase_14 | AC | 522 ms
12,860 KB |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
ソースコード
#define _USE_MATH_DEFINES #include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <complex> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> #include <iterator> using namespace std; void bridgeDecomposition( const vector<vector<int> >& edges, vector<int>& indexConvert, vector<vector<int> >& nodesOut) { const int n = edges.size(); vector<int> num(n, -1), low(n, -1); vector<bool> isStk(n, false); stack<int> stk; int cnt = -1; nodesOut.clear(); for(int i=0; i<n; ++i){ stack<tuple<int, int, unsigned> > arg; arg.push(make_tuple(i, -1, 0)); while(!arg.empty()){ int v = get<0>(arg.top()); int prev = get<1>(arg.top()); unsigned j = get<2>(arg.top()); arg.pop(); if(j == 0){ if(num[v] != -1) continue; num[v] = low[v] = ++ cnt; stk.push(v); isStk[v] = true; } else{ int w = edges[v][j-1]; if(w != prev && isStk[w]) low[v] = min(low[v], low[w]); } if(j < edges[v].size()){ arg.push(make_tuple(v, prev, j + 1)); int w = edges[v][j]; if(w != prev) arg.push(make_tuple(w, v, 0)); } else if(low[v] == num[v]){ nodesOut.push_back(vector<int>()); for(;;){ int w = stk.top(); stk.pop(); isStk[w] = false; nodesOut.back().push_back(w); if(v == w) break; } } } } const int m = nodesOut.size(); indexConvert.resize(n); for(int i=0; i<m; ++i){ for(unsigned j=0; j<nodesOut[i].size(); ++j) indexConvert[nodesOut[i][j]] = i; } } void bridgeDecomposition( const vector<vector<int> >& edges, vector<int>& indexConvert, vector<vector<int> >& nodesOut, vector<vector<int> >& edgesOut) { bridgeDecomposition(edges, indexConvert, nodesOut); const int n = edges.size(); const int m = nodesOut.size(); edgesOut.assign(m, vector<int>()); vector<set<int> > used(m); for(int i=0; i<n; ++i){ for(unsigned j=0; j<edges[i].size(); ++j){ int v = indexConvert[i]; int w = indexConvert[edges[i][j]]; if(v != w && used[v].find(w) == used[v].end()){ edgesOut[v].push_back(w); used[v].insert(w); } } } } class SegmentTree { public: typedef pair<int, int> T1; typedef pair<int, int> T2; // データの初期値、以下の条件を満たすこと // uniteData(v, INIT_DATA) == v static const T1 INIT_DATA; private: // 前回の値がprevである要素に対して、 // パラメータxを用いた更新処理を適用した後の計算結果を返す T1 updateData(T1 prev, T2 x){ if(x == INIT_DATA) return prev; else return x; } // 2つの区間の計算結果v1,v2に対して、 // その2つの区間を統合した区間における計算結果を返す T1 uniteData(T1 v1, T1 v2){ return max(v1, v2); } int n; vector<T1> data; void updateTree(int a, int k, int l, int r, T2 x){ if(a == l && a == r){ data[k] = updateData(data[k], x); } else if(l <= a && a <= r){ updateTree(a, k*2+1, l, (l+r)/2, x); updateTree(a, k*2+2, (l+r+1)/2, r, x); data[k] = uniteData(data[k*2+1], data[k*2+2]); } } T1 getValue(int a, int b, int k, int l, int r){ if(a <= l && r <= b){ return data[k]; } else if(a <= r && l <= b){ T1 v1 = getValue(a, b, k*2+1, l, (l+r)/2); T1 v2 = getValue(a, b, k*2+2, (l+r+1)/2, r); return uniteData(v1, v2); } else{ return INIT_DATA; } } public: SegmentTree(int n0){ n = 1; while(n < n0) n *= 2; data.assign(2*n-1, INIT_DATA); } SegmentTree(const vector<T1>& v) : SegmentTree((int)v.size()){ for(unsigned i=0; i<v.size(); ++i) data[n-1+i] = v[i]; for(int k=n-2; k>=0; --k) data[k] = uniteData(data[k*2+1], data[k*2+2]); } // a番目の要素にパラメータxによる更新処理を適用 void update(int a, T2 x){ updateTree(a, 0, 0, n-1, x); } // 区間[a,b]の計算結果を返す T1 get(int a, int b){ return getValue(a, b, 0, 0, n-1); } T1 get(int a, int b, T1 leftPrev, T1 rightPrev){ return uniteData(uniteData(leftPrev, get(a, b)), rightPrev); } }; const SegmentTree::T1 SegmentTree::INIT_DATA(-1, -1); class HeavyLightDecomposition { private: vector<SegmentTree> st; typedef SegmentTree::T1 T1; typedef SegmentTree::T2 T2; void addDataStructure(int n){ st.push_back(SegmentTree(n)); } void addDataStructure(const vector<T1>& init){ st.push_back(SegmentTree(init)); } void updateDataStructure(int i, int a, T2 x){ st[i].update(a, x); } void updateDataStructure(int i, int a, int b, T2 x){ st[i].update(a, x); } T1 getDataStructure(int i, int a, int b, T1 leftPrev, T1 rightPrev){ return st[i].get(a, b, leftPrev, rightPrev); } T1 reverseData(T1 x){ return x; } vector<int> depth; vector<int> parent; vector<vector<int> > node; vector<pair<int, int> > index; pair<int, int> dfs(const vector<vector<int> >& edges, int curr, int prev) { if(prev != -1){ depth[curr] = depth[prev] + 1; parent[curr] = prev; } int sum = 1; pair<int, int> p(-1, -1); for(int next : edges[curr]){ if(next == prev) continue; pair<int, int> p2 = dfs(edges, next, curr); sum += p2.first; p = max(p, p2); } if(p.first == -1){ p.second = node.size(); node.resize(p.second + 1); } node[p.second].push_back(curr); return make_pair(sum, p.second); } public: HeavyLightDecomposition(const vector<vector<int> >& edges, int root) { int n = edges.size(); depth.assign(n, 0); parent.assign(n, -1); dfs(edges, root, -1); index.resize(n); for(unsigned i=0; i<node.size(); ++i){ for(unsigned j=0; j<node[i].size(); ++j) index[node[i][j]] = make_pair(i, j); addDataStructure(node[i].size()); } } HeavyLightDecomposition(const vector<vector<int> >& edges, int root, const vector<T1>& init) { int n = edges.size(); depth.assign(n, 0); parent.assign(n, -1); dfs(edges, root, -1); index.resize(n); for(unsigned i=0; i<node.size(); ++i){ vector<T1> v(node[i].size()); for(unsigned j=0; j<node[i].size(); ++j){ index[node[i][j]] = make_pair(i, j); v[j] = init[node[i][j]]; } addDataStructure(v); } } void update(int a, T2 x){ updateDataStructure(index[a].first, index[a].second, x); } void update(int a, int b, T2 x){ while(index[a].first != index[b].first){ int a2 = node[index[a].first].back(); int b2 = node[index[b].first].back(); if(depth[a2] < depth[b2]){ swap(a, b); swap(a2, b2); } updateDataStructure(index[a].first, index[a].second, index[a2].second, x); a = parent[a2]; } if(depth[a] < depth[b]) swap(a, b); updateDataStructure(index[a].first, index[a].second, index[b].second, x); } T1 get(int a, int b){ T1 leftPrev = SegmentTree::INIT_DATA; T1 rightPrev = SegmentTree::INIT_DATA; while(index[a].first != index[b].first){ int a2 = node[index[a].first].back(); int b2 = node[index[b].first].back(); if(depth[a2] > depth[b2]){ leftPrev = getDataStructure(index[a].first, index[a].second, index[a2].second, leftPrev, SegmentTree::INIT_DATA); a = parent[a2]; } else{ rightPrev = getDataStructure(index[b].first, index[b].second, index[b2].second, rightPrev, SegmentTree::INIT_DATA); b = parent[b2]; } } if(depth[a] > depth[b]){ return getDataStructure(index[a].first, index[a].second, index[b].second, leftPrev, reverseData(rightPrev)); } else{ T1 ans = getDataStructure(index[a].first, index[b].second, index[a].second, rightPrev, reverseData(leftPrev)); return reverseData(ans); } } }; int main() { int n, m, q; cin >> n >> m >> q; vector<vector<int> > edges0(n); for(int i=0; i<m; ++i){ int a, b; cin >> a >> b; -- a; -- b; edges0[a].push_back(b); edges0[b].push_back(a); } vector<int> indexConvert; vector<vector<int> > nodes; vector<vector<int> > edges; bridgeDecomposition(edges0, indexConvert, nodes, edges); vector<priority_queue<int> > pq(edges.size()); HeavyLightDecomposition hl(edges, 0); while(--q >= 0){ int type, a, b; cin >> type >> a >> b; if(type == 1){ int i = indexConvert[a-1]; if(pq[i].empty() || pq[i].top() < b) hl.update(i, make_pair(b, i)); pq[i].push(b); } else{ int i = indexConvert[a-1]; int j = indexConvert[b-1]; pair<int, int> p = hl.get(i, j); cout << p.first << endl; if(p.first != -1){ int k = p.second; pq[k].pop(); if(pq[k].empty()) hl.update(k, make_pair(-1, i)); else hl.update(k, make_pair(pq[k].top(), i)); } } } return 0; }