結果

問題 No.1054 Union add query
ユーザー hs0319boyhs0319boy
提出日時 2020-05-15 22:28:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,639 bytes
コンパイル時間 1,658 ms
コンパイル使用メモリ 175,044 KB
実行使用メモリ 11,096 KB
最終ジャッジ日時 2023-10-19 15:13:41
合計ジャッジ時間 6,333 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vin=vector<int>;
using vll=vector<long long>;
using vvin=vector<vector<int>>;
using vvll=vector<vector<long long>>;
using vstr=vector<string>;
using vvstr=vector<vector<string>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using vbo=vector<bool>;
using vvbo=vector<vector<bool>>;
using vpii=vector<pair<int,int>>;
using pqsin=priority_queue<int,vector<int>,greater<int>>;
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define decp(n) cout<<fixed<<setprecision((int)n)
const int inf=1e9+7;
const ll INF=1e18;

vin par(200050);
vin deep(200050);
void init(int n);
int root(int x);
void unite(int x,int y);
bool same(int x,int y);

int main(){
    int n,q;cin>>n>>q;
    queue<ll> que;
    init(n);
    vll wei(n+1,0);
    rep(i,q){
        int t,a,b;
        scanf("%ld %ld %ld",&t,&a,&b);
        if(t==1)unite(a,b);
        if(t==2){
            rep(i,n){
                if(same(i+1,a))wei[i+1]+=b;
            }
        }
        if(t==3)que.push(wei[a]);
    }
    while(que.size()){
        printf("%ld\n",que.front());
        que.pop();
    }
}

void init(int n){
    rep(i,n+1){
        par[i]=i;
        deep[i]=0;
    }
}
int root(int x){
    if(par[x]==x)return x;
    else return par[x]=root(par[x]);
}
void unite(int x,int y){
    x=root(x);
    y=root(y);
    if(x==y)return;
    if(deep[x]<deep[y])par[x]=y;
    else{
        par[y]=x;
        if(deep[x]==deep[y])deep[x]++;
    }
}
bool same(int x,int y){
    return root(x)==root(y);
}
0