結果

問題 No.1054 Union add query
ユーザー motmot
提出日時 2020-05-15 22:17:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 1,788 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 83,944 KB
実行使用メモリ 15,544 KB
最終ジャッジ日時 2023-10-19 14:52:49
合計ジャッジ時間 3,506 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,928 KB
testcase_01 AC 2 ms
7,928 KB
testcase_02 AC 2 ms
7,928 KB
testcase_03 AC 159 ms
14,172 KB
testcase_04 AC 174 ms
15,544 KB
testcase_05 AC 159 ms
13,976 KB
testcase_06 AC 149 ms
14,452 KB
testcase_07 AC 138 ms
14,452 KB
testcase_08 AC 140 ms
14,452 KB
testcase_09 AC 176 ms
15,544 KB
testcase_10 AC 113 ms
15,544 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:33:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   33 |     scanf("%d %d", &N, &Q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:36:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   36 |         scanf("%d %d %d", &T[i], &A[i], &B[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

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;
    scanf("%d %d", &N, &Q);
    int T[Q], A[Q], B[Q];
    for(int i=0;i<Q;++i){
        scanf("%d %d %d", &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;
    int tmp, x;
    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);
                num[roota] -= num[rootb];
                par[roota] = rootb;
                siz[rootb] += siz[roota];
            }
        }
        if(T[i]==2){
            roota = root(A[i]);
            num[roota] += B[i];
            /**
            for(int j=0;j<N;++j){
                if(roota == root(j)){
                    num[j] += B[i];
                }
            }
            **/
        }
        if(T[i]==3){
            x = A[i];
            tmp = 0;
            while(x!=par[x]){
                tmp += num[x];
                x = par[x];
            }
            tmp += num[x];
            printf("%d\n", tmp);
        }
    }
}

0