結果

問題 No.1054 Union add query
ユーザー ChanyuhChanyuh
提出日時 2020-08-26 21:44:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 522 ms / 2,000 ms
コード長 2,863 bytes
コンパイル時間 1,552 ms
コンパイル使用メモリ 137,548 KB
実行使用メモリ 43,136 KB
最終ジャッジ日時 2024-04-25 04:05:06
合計ジャッジ時間 6,443 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 373 ms
12,288 KB
testcase_04 AC 522 ms
43,136 KB
testcase_05 AC 327 ms
7,680 KB
testcase_06 AC 311 ms
19,948 KB
testcase_07 AC 314 ms
19,960 KB
testcase_08 AC 227 ms
19,968 KB
testcase_09 AC 494 ms
42,368 KB
testcase_10 AC 136 ms
42,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

struct UnionFind {
private:
    vector<int> par,ran;
    vector<int> edge,size,num_par,num;
    vector<vector<int>> ch;
    int n;
public:
    UnionFind(int x){
      n=x;
      par.resize(n,0);
      ran.resize(n,0);
      edge.resize(n,0);
      size.resize(n,1);
      ch.resize(n,vector<int>());
      num_par.resize(n,0);
      num.resize(n,0);
      rep(i,n){
        par[i]=i;
        ch[i].push_back(i);
      }
    }
    int find(int x) {
        if (par[x]==x)return x;
        else return par[x]=find(par[x]);
    }
    void unite(int x, int y) {
        x=find(x),y=find(y);
        if (x==y){
            edge[x]++;
            return;
        }
        if (ran[x]<ran[y]) {
            par[x]=y;
            size[y]+=size[x];
            edge[y]+=edge[x]+1;
            for(int t:ch[x]){
              ch[y].push_back(t);
              num[t]+=num_par[x]-num_par[y];
            }
        }
        else {
            par[y]=x;
            size[x]+=size[y];
            edge[x]+=edge[y]+1;
            if (ran[x]==ran[y])ran[x]++;
            for(int t:ch[y]){
              ch[x].push_back(t);
              num[t]+=num_par[y]-num_par[x];
            }
        }
    }
    void add(int x,int b){
      num_par[find(x)]+=b;
    }
    int get(int x){
      return num[x]+num_par[find(x)];
    }
    bool same(int x,int y) {
        return find(x)==find(y);
    }
    int getSize(int x){
        return size[find(x)];
    }
    int getEdge(int x){
        return edge[find(x)];
    }
};

int n,q;

void solve(){
  cin >> n >> q;
  UnionFind uf(n);
  rep(i,q){
    int t,a,b;cin >> t >> a >> b;a--;
    if(t==1){
      b--;
      uf.unite(a,b);
    }
    else if(t==2){
      uf.add(a,b);
    }
    else{
      cout << uf.get(a) << endl;
    }
  }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0