#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; void bridgeDecomposition( const vector >& edges, vector& indexConvert, vector >& nodesOut) { const int n = edges.size(); vector num(n, -1), low(n, -1); vector isStk(n, false); stack stk; int cnt = -1; nodesOut.clear(); for(int i=0; i > 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()); 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 >& edges, vector& indexConvert, vector >& nodesOut, vector >& edgesOut) { bridgeDecomposition(edges, indexConvert, nodesOut); const int n = edges.size(); const int m = nodesOut.size(); edgesOut.assign(m, vector()); vector > used(m); for(int i=0; i T1; typedef pair T2; // データの初期値、以下の条件を満たすこと // uniteData(v, INIT_DATA) == v static const T1 INIT_DATA; // 2つの区間の計算結果v1,v2に対して、 // その2つの区間を統合した区間における計算結果を返す static T1 uniteData(T1 v1, T1 v2){ return max(v1, v2); } private: // 前回の値がprevである要素に対して、 // パラメータxを用いた更新処理を適用した後の計算結果を返す T1 updateData(T1 prev, T2 x){ if(x == INIT_DATA) return prev; else return x; } int n; vector 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& v) : SegmentTree((int)v.size()){ for(unsigned i=0; i=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); } }; const SegmentTree::T1 SegmentTree::INIT_DATA(-1, -1); class HeavyLightDecomposition { private: typedef SegmentTree DataStructure; typedef DataStructure::T1 T1; typedef DataStructure::T2 T2; static const T1 INIT_DATA; // 長さnの配列に対するデータ構造を生成 DataStructure makeDataStructure(int n){ return DataStructure(n); } // 初期値initの配列に対するデータ構造を生成 DataStructure makeDataStructure(const vector& init){ return DataStructure(init); } // データ構造の更新処理 void updateDataStructure(DataStructure& ds, int a, T2 x){ ds.update(a, x); } void updateDataStructure(DataStructure& ds, int a, int b, T2 x){ } // データ構造のデータ取得 T1 getValFromDataStructure(DataStructure& ds, int a, int b, T1 prev){ return DataStructure::uniteData(ds.get(a, b), prev); } vector dsList; vector depth; vector parent; vector > node; vector > index; // 深さ優先探索によりHL分解を実行 pair dfs(const vector >& edges, int curr, int prev) { if(prev != -1){ depth[curr] = depth[prev] + 1; parent[curr] = prev; } int sum = 1; pair p(-1, -1); for(int next : edges[curr]){ if(next == prev) continue; pair 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 >& 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 >& edges, int root, const vector& 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 v(node[i].size()); for(unsigned j=0; j> n >> m >> q; vector > edges0(n); for(int i=0; i> a >> b; -- a; -- b; edges0[a].push_back(b); edges0[b].push_back(a); } vector indexConvert; vector > nodes; vector > edges; bridgeDecomposition(edges0, indexConvert, nodes, edges); vector > 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 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, k)); else hl.update(k, make_pair(pq[k].top(), k)); } } } return 0; }