結果

問題 No.1054 Union add query
ユーザー motmot
提出日時 2020-05-15 22:05:23
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,465 bytes
コンパイル時間 657 ms
コンパイル使用メモリ 84,360 KB
実行使用メモリ 18,024 KB
最終ジャッジ日時 2023-10-19 14:29:22
合計ジャッジ時間 4,709 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,712 KB
testcase_01 AC 2 ms
7,712 KB
testcase_02 AC 2 ms
7,712 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll big=1e18;
const double PI=3.14159265359;

int par[500005], siz[500005], num[500005];

int root(int x){
    while(x!=par[x]) x = par[x];
    return x;
}

int main() {
    int N, Q;
    cin>>N>>Q;
    int T[Q], A[Q], B[Q];
    for(int i=0;i<Q;++i){
        cin>>T[i]>>A[i]>>B[i];
        A[i]--;
        if(T[i]==1){
            B[i]--;
        }
    }

    for(int i=0;i<N;++i){
        par[i] = i;
        siz[i] = 1;
        num[i] = 0;
    }

    int roota, rootb;
    for(int i=0;i<Q;++i){
        if(T[i]==1){
            roota = root(A[i]);
            rootb = root(B[i]);
            if(roota != rootb){
                if(siz[roota] > siz[rootb]) swap(roota, rootb);
                par[roota] = rootb;
                siz[rootb] += siz[roota];
            }
        }
        if(T[i]==2){
            roota = root(A[i]);
            for(int j=0;j<N;++j){
                if(roota == root(j)){
                    num[j] += B[i];
                }
            }
        }
        if(T[i]==3){
            cout<<num[A[i]]<<endl;
        }
    }
}

0