結果

問題 No.1054 Union add query
ユーザー milanis48663220milanis48663220
提出日時 2020-06-20 01:27:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,188 bytes
コンパイル時間 672 ms
コンパイル使用メモリ 82,156 KB
実行使用メモリ 814,176 KB
最終ジャッジ日時 2023-09-16 16:27:42
合計ジャッジ時間 6,834 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int root(int)’ 内:
main.cpp:18:1: 警告: 非 void を戻す関数内に return 文がありません [-Wreturn-type]
   18 | }
      | ^

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

int N, Q;
int par[500000];
int dat[500000];

int root(int v){
    par[v] == v ? v : root(par[v]);
}

int size(int v){
    int ans = 1;
    while(v != par[v]){
        ans++; v = par[v];
    }
    return ans;
}

int calc(int v){
    int ans = dat[v];
    while(v != par[v]){
        v = par[v]; ans += dat[v];
    }
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N >> Q;
    for(int i = 0; i < N; i++){
        par[i] = i;
    }
    for(int i = 0; i < Q; i++){
        int t, a, b;
        cin >> t >> a >> b;
        if(t == 1){
            a--; b--;
            int ra = root(a), rb = root(b);
            if(ra != rb){
                if(size(a) < size(b)) swap(a, b);
                par[ra] = rb;
                dat[ra] -= dat[rb];
            }
        }else if(t == 2) {
            a--;
            int r = root(a);
            dat[r] += b;
        }else{
            a--;
            cout << calc(a) << endl;
        }
    }
}
0