結果
問題 | No.529 帰省ラッシュ |
ユーザー | ei1333333 |
提出日時 | 2016-11-01 18:45:49 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 372 ms / 4,500 ms |
コード長 | 7,332 bytes |
コンパイル時間 | 2,342 ms |
コンパイル使用メモリ | 194,436 KB |
実行使用メモリ | 54,052 KB |
最終ジャッジ日時 | 2024-05-09 13:01:58 |
合計ジャッジ時間 | 7,667 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 4 ms
5,376 KB |
testcase_05 | AC | 4 ms
5,376 KB |
testcase_06 | AC | 5 ms
5,376 KB |
testcase_07 | AC | 4 ms
5,376 KB |
testcase_08 | AC | 253 ms
29,768 KB |
testcase_09 | AC | 261 ms
29,644 KB |
testcase_10 | AC | 309 ms
33,632 KB |
testcase_11 | AC | 323 ms
33,752 KB |
testcase_12 | AC | 248 ms
30,500 KB |
testcase_13 | AC | 333 ms
54,052 KB |
testcase_14 | AC | 267 ms
36,816 KB |
testcase_15 | AC | 372 ms
34,136 KB |
testcase_16 | AC | 361 ms
34,272 KB |
testcase_17 | AC | 344 ms
45,732 KB |
testcase_18 | AC | 337 ms
46,236 KB |
testcase_19 | AC | 333 ms
41,632 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:300:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 300 | scanf("%d %d %d", &N, &M, &Q); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:305:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 305 | scanf("%d %d", &A, &B); | ~~~~~^~~~~~~~~~~~~~~~~ main.cpp:321:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 321 | scanf("%d %d %d", &T, &A, &B); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector< int > data; UnionFind(size_t sz) { data.assign(sz, -1); } void unite(int x, int y) { x = find(x); y = find(y); if(x != y) { if(data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; } } int find(int k) { if(data[k] < 0) return (k); return (data[k] = find(data[k])); } }; struct BiConnectedComponents { UnionFind uf; vector< vector< int > > g; vector< pair< int, int > > edges; vector< int > used, ord, low, comp; BiConnectedComponents(size_t v) : uf(v), g(v), used(v, 0), comp(v), ord(v), low(v) { } void add_edge(int x, int y) { g[x].push_back(y); g[y].push_back(x); edges.push_back(minmax(x, y)); } void dfs(int idx, int &k, int par = -1) { used[idx] = true; ord[idx] = k++; low[idx] = ord[idx]; for(auto &to : g[idx]) { if(!used[to]) { dfs(to, k, idx); low[idx] = min(low[idx], low[to]); if(ord[idx] >= low[to]) uf.unite(idx, to); } else if(to != par) { low[idx] = min(low[idx], ord[to]); } } } int operator[](int k) { return (comp[k]); } size_t size() { return (g.size()); } void build(vector< vector< int > > &t) { int kk = 0; dfs(0, kk); int ptr = 0; vector< int > cc(g.size()); for(int i = 0; i < g.size(); i++) { if(i == uf.find(i)) cc[i] = ptr++; } t.resize(ptr); for(int i = 0; i < g.size(); i++) { comp[i] = cc[uf.find(i)]; } for(auto &e : edges) { int x = comp[e.first], y = comp[e.second]; if(x == y) continue; t[x].push_back(y); t[y].push_back(x); } } }; vector< vector< int > > graph; struct CentroidPathDecomposition { struct Centroid { int ParIndex, ParDepth, Deep; vector< int > node; inline int size() { return (node.size()); } inline int &operator[](int k) { return (node[k]); } inline pair< int, int > Up() { return (make_pair(ParIndex, ParDepth)); } }; vector< int > SubTreeSize, NextPath; vector< int > TreeIndex, TreeDepth; vector< Centroid > Centroids; void BuildSubTreeSize() { stack< pair< int, int > > s; s.push({0, -1}); while(!s.empty()) { auto p = s.top(); s.pop(); if(~SubTreeSize[p.first]) { NextPath[p.first] = -1; for(auto &to : graph[p.first]) { if(p.second == to) continue; SubTreeSize[p.first] += SubTreeSize[to]; if(NextPath[p.first] == -1 || SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) { NextPath[p.first] = to; } } } else { s.push(p); SubTreeSize[p.first] = 1; for(auto &to : graph[p.first]) { if(p.second != to) s.push({to, p.first}); } } } } void BuildPath() { stack< pair< int, int > > s; Centroids.push_back((Centroid) {-1, -1, 0}); s.push({0, -1}); TreeIndex[0] = 0; while(!s.empty()) { auto p = s.top(); s.pop(); TreeDepth[p.first] = Centroids[TreeIndex[p.first]].size(); for(auto &to : graph[p.first]) { if(p.second != to) { if(to == NextPath[p.first]) { // Centroid-Path TreeIndex[to] = TreeIndex[p.first]; } else { // Not Centroid-Path TreeIndex[to] = Centroids.size(); Centroids.push_back((Centroid) {TreeIndex[p.first], TreeDepth[p.first], Centroids[TreeIndex[p.first]].Deep + 1}); } s.push({to, p.first}); } } Centroids[TreeIndex[p.first]].node.push_back(p.first); } } void Build() { BuildSubTreeSize(); BuildPath(); } inline int size() { return (Centroids.size()); } inline pair< int, int > Information(int idx) { return (make_pair(TreeIndex[idx], TreeDepth[idx])); } inline Centroid &operator[](int k) { return (Centroids[k]); } inline int LCA(int a, int b) // これを流用する { int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB; tie(TreeIdxA, TreeDepthA) = Information(a); tie(TreeIdxB, TreeDepthB) = Information(b); while(TreeIdxA != TreeIdxB) { if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) { tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up(); } else { tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up(); } } if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB); return (Centroids[TreeIdxA][TreeDepthA]); } CentroidPathDecomposition(int SZ) { SubTreeSize.assign(SZ, -1); NextPath.resize(SZ); TreeIndex.resize(SZ); TreeDepth.resize(SZ); } int getMax(int a, int b); }; struct SegmentTree { vector< int > seg; int sz; SegmentTree(int n) { sz = 1; while(sz < n) sz <<= 1; seg.assign(2 * sz - 1, -1); } int rmq(int a, int b, int k, int l, int r) { if(a >= r || b <= l) return (-1); if(a <= l && r <= b) return (seg[k]); return (max(rmq(a, b, 2 * k + 1, l, (l + r) >> 1), rmq(a, b, 2 * k + 2, (l + r) >> 1, r))); } int rmq(int a, int b) { return (rmq(a, b, 0, 0, sz)); } void update(int k, int x) { k += sz - 1; seg[k] = x; while(k > 0) { k = (k - 1) >> 1; seg[k] = max(seg[2 * k + 1], seg[2 * k + 2]); } } }; vector< SegmentTree > segs; CentroidPathDecomposition *press; void update(int idx, int v) { int treeIdx, treeDepth; tie(treeIdx, treeDepth) = press->Information(idx); segs[treeIdx].update(treeDepth, v); } int CentroidPathDecomposition::getMax(int a, int b) { int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB, ret = -1; tie(TreeIdxA, TreeDepthA) = Information(a); tie(TreeIdxB, TreeDepthB) = Information(b); while(TreeIdxA != TreeIdxB) { if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) { ret = max(ret, segs[TreeIdxA].rmq(0, TreeDepthA + 1)); tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up(); } else { ret = max(ret, segs[TreeIdxB].rmq(0, TreeDepthB + 1)); tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up(); } } if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB); ret = max(ret, segs[TreeIdxA].rmq(TreeDepthA, TreeDepthB + 1)); return (ret); } int main() { int N, M, Q; scanf("%d %d %d", &N, &M, &Q); BiConnectedComponents bc(N); for(int i = 0; i < M; i++) { int A, B; scanf("%d %d", &A, &B); bc.add_edge(--A, --B); } bc.build(graph); press = new CentroidPathDecomposition(graph.size()); press->Build(); for(int i = 0; i < press->size(); i++) { segs.push_back(SegmentTree((*press)[i].size())); } vector< priority_queue< int > > que(graph.size()); map< int, int > pos; for(int i = 0; i < Q; i++) { int T, A, B; scanf("%d %d %d", &T, &A, &B); if(T == 1) { A = bc[--A]; assert(pos.count(B) == 0); pos[B] = A; que[A].push(B); if(que[A].top() == B) update(A, que[A].top()); } else { int value = press->getMax(bc[--A], bc[--B]); printf("%d\n", value); if(value >= 1) { int idx = pos[value]; que[idx].pop(); update(idx, que[idx].empty() ? -1 : que[idx].top()); } } } }