結果
問題 | No.2296 Union Path Query (Hard) |
ユーザー | t98slider |
提出日時 | 2023-02-13 08:02:35 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,089 ms / 7,000 ms |
コード長 | 4,423 bytes |
コンパイル時間 | 2,036 ms |
コンパイル使用メモリ | 192,724 KB |
実行使用メモリ | 68,168 KB |
最終ジャッジ日時 | 2024-05-02 13:54:49 |
合計ジャッジ時間 | 31,615 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 46 ms
54,940 KB |
testcase_02 | AC | 39 ms
54,852 KB |
testcase_03 | AC | 38 ms
54,860 KB |
testcase_04 | AC | 299 ms
58,832 KB |
testcase_05 | AC | 292 ms
58,832 KB |
testcase_06 | AC | 408 ms
61,004 KB |
testcase_07 | AC | 404 ms
62,668 KB |
testcase_08 | AC | 388 ms
62,540 KB |
testcase_09 | AC | 303 ms
38,108 KB |
testcase_10 | AC | 297 ms
37,984 KB |
testcase_11 | AC | 299 ms
38,112 KB |
testcase_12 | AC | 283 ms
33,728 KB |
testcase_13 | AC | 100 ms
6,400 KB |
testcase_14 | AC | 86 ms
5,376 KB |
testcase_15 | AC | 448 ms
61,896 KB |
testcase_16 | AC | 397 ms
47,968 KB |
testcase_17 | AC | 193 ms
18,768 KB |
testcase_18 | AC | 583 ms
63,952 KB |
testcase_19 | AC | 362 ms
34,236 KB |
testcase_20 | AC | 580 ms
63,944 KB |
testcase_21 | AC | 512 ms
62,028 KB |
testcase_22 | AC | 506 ms
61,776 KB |
testcase_23 | AC | 271 ms
64,292 KB |
testcase_24 | AC | 206 ms
33,644 KB |
testcase_25 | AC | 339 ms
65,744 KB |
testcase_26 | AC | 331 ms
65,868 KB |
testcase_27 | AC | 878 ms
65,740 KB |
testcase_28 | AC | 893 ms
65,868 KB |
testcase_29 | AC | 1,013 ms
67,532 KB |
testcase_30 | AC | 1,013 ms
67,536 KB |
testcase_31 | AC | 1,089 ms
68,168 KB |
testcase_32 | AC | 1,040 ms
67,660 KB |
testcase_33 | AC | 882 ms
65,608 KB |
testcase_34 | AC | 649 ms
63,052 KB |
testcase_35 | AC | 616 ms
62,412 KB |
testcase_36 | AC | 416 ms
59,344 KB |
testcase_37 | AC | 961 ms
49,528 KB |
testcase_38 | AC | 994 ms
49,524 KB |
testcase_39 | AC | 991 ms
49,400 KB |
testcase_40 | AC | 954 ms
49,528 KB |
testcase_41 | AC | 1 ms
5,376 KB |
testcase_42 | AC | 2 ms
5,376 KB |
testcase_43 | AC | 2 ms
5,376 KB |
testcase_44 | AC | 521 ms
64,584 KB |
testcase_45 | AC | 542 ms
64,460 KB |
testcase_46 | AC | 577 ms
65,504 KB |
testcase_47 | AC | 579 ms
66,636 KB |
testcase_48 | AC | 573 ms
65,224 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(0); ll N, X, Q; cin >> N >> X >> Q; vector<vector<pair<int, ll>>> G(N); //重み付きグラフ vector<vector<int>> parent(18, vector<int>(N, -1)); //親の頂点のダブリング配列 vector<vector<ll>> dp(18, vector<ll>(N)); //辺の重みのダブリング配列 vector<int> depth(N); //根からの深さを入れる vector<int> parent_or_size(N, -1); //Union Find vector<tuple<ll, int, int>> diameter_and_vertex(N); //直径と頂点を入れる for(int i = 0; i < N; i++) diameter_and_vertex[i] = make_tuple(0, i, i); //leaderはUnion Find用の経路圧縮用関数 function<int(int)> leader = [&](int v){ if(parent_or_size[v] < 0) return v; return parent_or_size[v] = leader(parent_or_size[v]); }; //dfsでダブリングを更新する function<void(int)> dfs = [&](int v) { //ダブリングの更新ループ for(int i = 0; i + 1 < 18; i++){ if(parent[i][v] == -1) { parent[i + 1][v] = -1; dp[i + 1][v] = dp[i][v]; } else { parent[i + 1][v] = parent[i][parent[i][v]]; dp[i + 1][v] = dp[i][parent[i][v]] + dp[i][v]; } } int u; ll w; for(auto &&edge : G[v]){ tie(u, w) = edge; if(parent[0][v] == u) continue; depth[u] = depth[v] + 1; parent[0][u] = v; dp[0][u] = w; dfs(u); } }; //頂点u, 頂点v間の距離を求める関数 auto dist = [&](int u, int v){ if(leader(u) != leader(v)) return -1ll; if(depth[u] > depth[v]) swap(u, v); ll result = 0; //頂点 v を頂点 u と高さが同じになるようにする for(int i = 0; i < 18; i++){ if((depth[v] - depth[u]) >> i & 1){ result += dp[i][v]; v = parent[i][v]; } } if(u == v) return result; for(int i = 17; i >= 0; i--){ if(parent[i][u] != parent[i][v]){ result += dp[i][u]; result += dp[i][v]; u = parent[i][u]; v = parent[i][v]; } } result += dp[0][u]; result += dp[0][v]; return result; }; //頂点u, 頂点vを重みwの辺でマージ auto merge = [&](int u, int v, ll w){ int x = leader(u), y = leader(v); if(x == y) return true; assert(x != y); //非連結なことが保証されているはず if(-parent_or_size[x] < -parent_or_size[y]) { swap(x, y); swap(u, v); } //x - u 側の連結成分が親となるようにする //Union Findの更新 parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; //辺を追加してDFSでダブリングを更新 G[u].emplace_back(v, w); G[v].emplace_back(u, w); depth[v] = depth[u] + 1; parent[0][v] = u; dp[0][v] = w; dfs(v); //直径を更新する tuple<ll, int, int> mx = diameter_and_vertex[x]; ll temp; array<int, 4> candidate_vertex{}; tie(temp, candidate_vertex[0], candidate_vertex[1]) = diameter_and_vertex[x]; tie(temp, candidate_vertex[2], candidate_vertex[3]) = diameter_and_vertex[y]; for(int i = 0; i < 4; i++) { int v1 = candidate_vertex[i]; for(int j = 0; j < i; j++) { int v2 = candidate_vertex[j]; ll length = dist(v1, v2); mx = max(mx, make_tuple(length, v1, v2)); } } diameter_and_vertex[x] = mx; return false; }; ll type, u, v, w; int cnt = 1; while(Q--){ cin >> type; if(type == 1){ cin >> v >> w; merge(v, X, w); }else if(type == 2){ cin >> u >> v; ll ans = dist(u, v); cout << ans << '\n'; if(ans != -1) ( X += ans ) %= N; }else if(type == 3){ cin >> v; cout << get<0>( diameter_and_vertex[ leader(v) ]) << '\n'; }else{ cin >> v; (X += v) %= N; } } }