結果

問題 No.2296 Union Path Query (Hard)
ユーザー t98slidert98slider
提出日時 2023-03-08 21:57:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 288 ms / 7,000 ms
コード長 5,485 bytes
コンパイル時間 1,901 ms
コンパイル使用メモリ 185,772 KB
実行使用メモリ 37,844 KB
最終ジャッジ日時 2024-05-02 14:10:52
合計ジャッジ時間 14,791 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 20 ms
29,008 KB
testcase_02 AC 20 ms
29,012 KB
testcase_03 AC 21 ms
29,008 KB
testcase_04 AC 153 ms
32,080 KB
testcase_05 AC 197 ms
32,080 KB
testcase_06 AC 184 ms
33,616 KB
testcase_07 AC 208 ms
35,024 KB
testcase_08 AC 177 ms
34,896 KB
testcase_09 AC 148 ms
21,404 KB
testcase_10 AC 151 ms
21,532 KB
testcase_11 AC 158 ms
21,536 KB
testcase_12 AC 146 ms
19,348 KB
testcase_13 AC 83 ms
5,376 KB
testcase_14 AC 82 ms
5,376 KB
testcase_15 AC 206 ms
34,384 KB
testcase_16 AC 184 ms
27,124 KB
testcase_17 AC 123 ms
11,008 KB
testcase_18 AC 250 ms
35,928 KB
testcase_19 AC 189 ms
19,600 KB
testcase_20 AC 254 ms
35,924 KB
testcase_21 AC 232 ms
34,392 KB
testcase_22 AC 235 ms
34,260 KB
testcase_23 AC 135 ms
36,896 KB
testcase_24 AC 115 ms
19,804 KB
testcase_25 AC 121 ms
35,536 KB
testcase_26 AC 122 ms
35,412 KB
testcase_27 AC 181 ms
35,408 KB
testcase_28 AC 176 ms
35,540 KB
testcase_29 AC 199 ms
36,432 KB
testcase_30 AC 194 ms
36,420 KB
testcase_31 AC 286 ms
33,752 KB
testcase_32 AC 288 ms
33,492 KB
testcase_33 AC 257 ms
32,852 KB
testcase_34 AC 200 ms
32,212 KB
testcase_35 AC 187 ms
31,952 KB
testcase_36 AC 206 ms
30,800 KB
testcase_37 AC 261 ms
24,368 KB
testcase_38 AC 259 ms
23,848 KB
testcase_39 AC 265 ms
24,368 KB
testcase_40 AC 271 ms
24,364 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 177 ms
37,072 KB
testcase_45 AC 187 ms
36,864 KB
testcase_46 AC 191 ms
37,844 KB
testcase_47 AC 201 ms
37,844 KB
testcase_48 AC 189 ms
37,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct problem_H {
    int MAX_LOGV, N;// lg(頂点数), 頂点数
    vector<vector<pair<int, int>>> G; //重み付きグラフ
    vector<vector<int>> parent; //2^i個先の親の頂点番号
    vector<ll> dp; //連結成分の代表元からの距離
    vector<int> depth; //連結成分の代表元からの深さ
    vector<int> log_table; //[lg x]のテーブル
    vector<int> parent_or_size; //いつものUnion Find
    vector<ll> diameter; //直径の長さを入れる
    vector<array<int, 2>> diameter_vertex; //直径の端点
    queue<int> que; //BFS用のqueue

    problem_H(int n) : N(n), MAX_LOGV(__lg(n) + 1), parent(MAX_LOGV, vector<int>(n, -1)),
                       parent_or_size(n, -1), 
                       G(n), dp(n), depth(n), diameter(n), diameter_vertex(n), log_table(n) {
        for(int i = 0; i < N; i++) {
            diameter_vertex[i] = {i, i}; //直径の端点を{i, i}で初期化
        }
        for(int i = 2; i < N; i++) {
            log_table[i] = log_table[i / 2] + 1; //[lg x]のテーブルを初期化
        }
    }

    //頂点vが所属する連結成分の代表元を返す関数
    int leader(int &v){ return parent_or_size[v] < 0 ? v : parent_or_size[v]; }

    //頂点uと頂点vを重みwの辺で結ぶ
    void merge(int u, int v, int w){
        int x = leader(u), y = leader(v);

        if(-parent_or_size[x] < -parent_or_size[y]) {
            swap(x, y);
            swap(u, v); //通常のUnion Findと違って u-vもswapしなけれればならないことに注意
        }

        //x - u 側が親の連結成分, y - v 側が子の連結成分となる

        parent_or_size[x] += parent_or_size[y];

        G[u].emplace_back(v, w);
        G[v].emplace_back(u, w);

        update_parent(v, u, w); // 頂点v の親を 頂点u に更新

        //ここからBFS
        //v - u はもう使わないので書き換えてしまっても問題ない
        que.emplace(v);
        while(!que.empty()){
            v = que.front();
            que.pop();

            parent_or_size[v] = x; //訪問すると同時に連結成分の代表元を登録しておく
            
            for(auto &edge : G[v]){
                tie(u, w) = edge;
                if(parent[0][v] == u) continue; //親への訪問をはじく
                update_parent(u, v, w); // 頂点u の親を 頂点v に更新
                que.emplace(u);
            }
        }

        update_diameter(x, y); //連結成分の代表元元同士を見て直径を更新
    }

    //親を更新する関数
    //頂点 v の親を par とする。頂点 v から par への距離は w
    void update_parent(const int &v, const int &par, const int &w){
        int depth_log = log_table[max(depth[v], depth[par] + 1)]; //頂点 v から2 ^ depth 先の親までの情報を書き換える
        
        depth[v] = depth[par] + 1; //代表元からの深さを更新
        dp[v] = dp[par] + w; //代表元からの距離を更新
        parent[0][v] = par; // 2 ^ 0 個先の親にparを登録
        
        for(int i = 0; i < depth_log; i++){ //2 ^ (i + 1) 個先の親の情報を更新
            parent[i + 1][v] = parent[i][v] == -1 ? -1 : parent[i][parent[i][v]];
        }
    }

    //連結成分の代表元 x, y をマージして直径の情報を更新する
    void update_diameter(const int &x, const int &y){
        ll mx;
        int u, v;
        if(diameter[x] >= diameter[y]) {
            mx = diameter[x];
            u = diameter_vertex[x][0], v = diameter_vertex[x][1];
        } else {
            mx = diameter[y];
            u = diameter_vertex[y][0], v = diameter_vertex[y][1];
        }

        for(int i = 0; i < 2; i++){
            int v1 = diameter_vertex[x][i];
            for(int j = 0; j < 2; j++){
                int v2 = diameter_vertex[y][j];
                ll d = dist(v1, v2);
                if(d > mx){
                    mx = d;
                    u = v1, v = v2;
                }
            }
        }

        diameter[x] = mx;
        diameter_vertex[x] = {u, v};
    }

    //頂点 u - 頂点 v 間の距離を求める関数
    long long dist(int u, int v){
        if(leader(u) != leader(v)) return -1;
        if(depth[v] < depth[u]) swap(v, u);
        long long res = dp[u] + dp[v];
        int d = depth[v] - depth[u];
        while(d){
            v = parent[log_table[d & -d]][v];
            d -= d & -d;
        }
        if(u == v) return res - 2 * dp[v];
        for(int i = log_table[depth[v]]; i >= 0; i--){
            if(parent[i][v] != parent[i][u]){
                u = parent[i][u];
                v = parent[i][v];
            }
        }
        return res - 2 * dp[parent[0][v]];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N, Q;
    ll X;
    cin >> N >> X >> Q;

    problem_H uf(N);
    
    int type, u, v, w;
    while(Q--){
        cin >> type;
        if(type == 1) {
            cin >> v >> w;
            uf.merge(v, X, w);
        } else if(type == 2) {
            cin >> u >> v;
            ll d = uf.dist(u, v);
            cout << d << '\n';
            if(d != -1) (X += d) %= N;
        } else if(type == 3) {
            cin >> v;
            cout << uf.diameter[uf.leader(v)] << '\n';
        } else {
            cin >> v;
            X += v;
            if(X >= N) X -= N;
        }
    }
}
0