luckYrat's library.

This documentation is automatically generated by competitive-verifier/competitive-verifier


:heavy_check_mark: cpp/z_test/yosupo-unionfind.test.cpp

Depends on

Required by

Code

#define PROBLEM "https://judge.yosupo.jp/problem/unionfind"

#include "../../cpp/template/template.cpp"

#include "../../cpp/data-structure/union-find.cpp"



int main(){
  int N,Q;cin>>N>>Q;
  UnionFind A(N);
  for(int i = 0; Q > i; i++){
    int t,u,v;cin>>t>>u>>v;
    if(t){
      cout << (A.same(u,v)?1:0) << endl;
    }else{
      A.unite(u,v);
    }
  }
}
#line 1 "cpp/z_test/yosupo-unionfind.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/unionfind"

#line 2 "cpp/template/template.cpp"

//yukicoder@cpp17

#include <iostream>
#include <iomanip>

#include <algorithm>

#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>

#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>

#include <random>

#include <bitset>

#include <complex>

#include <utility>

#include <numeric>

#include <functional>


using namespace std;
using ll = long long;
using P = pair<ll,ll>;


const ll MOD = 998244353;
const ll MODx = 1000000007;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62LL)-1;
const double EPS = (1e-10);


P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};


template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
 
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる

計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/

/* comment outed because can cause bugs
__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
*/
#line 4 "cpp/z_test/yosupo-unionfind.test.cpp"

#line 1 "cpp/data-structure/union-find.cpp"
struct UnionFind {
  int n;
  vector<int> par;
  vector<int> size_;
  UnionFind(int n_) : n(n_), par((size_t)n_), size_((size_t)n_,1){
    for(int i = 0; n > i; i++)par[i] = i;
  }

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

  void unite(int a,int b){
    int ra = root(a);
    int rb = root(b);
    if(ra==rb)return;
    if(size(ra) > size(rb)) swap(ra,rb);
    par[ra] = rb;
    size_[rb] += size_[ra];
  }

  bool same(int a, int b){
    return root(a) == root(b);
  }

  int size(int a){
    return size_[root(a)];
  }
  void debug(){
    for(int i = 0; n > i; i++){
      cout << size_[root(i)] << " ";
    }
    cout << endl;

    return;
  }
};
#line 6 "cpp/z_test/yosupo-unionfind.test.cpp"



int main(){
  int N,Q;cin>>N>>Q;
  UnionFind A(N);
  for(int i = 0; Q > i; i++){
    int t,u,v;cin>>t>>u>>v;
    if(t){
      cout << (A.same(u,v)?1:0) << endl;
    }else{
      A.unite(u,v);
    }
  }
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 7 ms 3 MB
g++ path_00 :heavy_check_mark: AC 175 ms 5 MB
g++ path_01 :heavy_check_mark: AC 210 ms 5 MB
g++ path_02 :heavy_check_mark: AC 105 ms 5 MB
g++ path_03 :heavy_check_mark: AC 109 ms 5 MB
g++ random_00 :heavy_check_mark: AC 167 ms 4 MB
g++ random_01 :heavy_check_mark: AC 170 ms 4 MB
g++ random_02 :heavy_check_mark: AC 128 ms 4 MB
g++ random_03 :heavy_check_mark: AC 38 ms 4 MB
g++ random_04 :heavy_check_mark: AC 108 ms 4 MB
g++ random_05 :heavy_check_mark: AC 153 ms 4 MB
g++ random_06 :heavy_check_mark: AC 137 ms 5 MB
g++ random_07 :heavy_check_mark: AC 31 ms 4 MB
g++ random_08 :heavy_check_mark: AC 59 ms 4 MB
g++ random_09 :heavy_check_mark: AC 237 ms 4 MB
Back to top page