結果

問題 No.2296 Union Path Query (Hard)
ユーザー t98slidert98slider
提出日時 2023-02-13 08:02:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,409 ms / 7,000 ms
コード長 4,423 bytes
コンパイル時間 2,889 ms
コンパイル使用メモリ 190,052 KB
実行使用メモリ 68,032 KB
最終ジャッジ日時 2023-08-15 01:27:44
合計ジャッジ時間 38,897 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 44 ms
54,540 KB
testcase_02 AC 43 ms
54,660 KB
testcase_03 AC 42 ms
54,616 KB
testcase_04 AC 383 ms
58,544 KB
testcase_05 AC 374 ms
58,552 KB
testcase_06 AC 535 ms
61,056 KB
testcase_07 AC 511 ms
62,752 KB
testcase_08 AC 481 ms
62,508 KB
testcase_09 AC 374 ms
37,956 KB
testcase_10 AC 372 ms
38,016 KB
testcase_11 AC 398 ms
37,988 KB
testcase_12 AC 365 ms
33,608 KB
testcase_13 AC 113 ms
6,300 KB
testcase_14 AC 103 ms
4,952 KB
testcase_15 AC 567 ms
61,960 KB
testcase_16 AC 502 ms
47,896 KB
testcase_17 AC 252 ms
18,728 KB
testcase_18 AC 719 ms
63,760 KB
testcase_19 AC 488 ms
34,184 KB
testcase_20 AC 692 ms
63,836 KB
testcase_21 AC 643 ms
62,052 KB
testcase_22 AC 631 ms
61,848 KB
testcase_23 AC 322 ms
64,336 KB
testcase_24 AC 256 ms
33,612 KB
testcase_25 AC 384 ms
65,564 KB
testcase_26 AC 378 ms
65,680 KB
testcase_27 AC 1,167 ms
65,680 KB
testcase_28 AC 1,173 ms
65,676 KB
testcase_29 AC 1,307 ms
67,212 KB
testcase_30 AC 1,340 ms
67,140 KB
testcase_31 AC 1,409 ms
68,032 KB
testcase_32 AC 1,393 ms
67,736 KB
testcase_33 AC 1,195 ms
65,492 KB
testcase_34 AC 874 ms
62,904 KB
testcase_35 AC 815 ms
62,316 KB
testcase_36 AC 593 ms
59,200 KB
testcase_37 AC 1,230 ms
49,328 KB
testcase_38 AC 1,242 ms
49,436 KB
testcase_39 AC 1,238 ms
49,260 KB
testcase_40 AC 1,254 ms
49,376 KB
testcase_41 AC 2 ms
4,388 KB
testcase_42 AC 1 ms
4,384 KB
testcase_43 AC 1 ms
4,384 KB
testcase_44 AC 625 ms
64,332 KB
testcase_45 AC 660 ms
64,100 KB
testcase_46 AC 683 ms
65,576 KB
testcase_47 AC 724 ms
66,448 KB
testcase_48 AC 683 ms
64,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
        }
    }
}
0