cp_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub SSRS-cp/cp_library

:heavy_check_mark: test/aoj/dsl/dsl_1_a.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_A"
#include <bits/stdc++.h>
using namespace std;
#include "../../../data_structure/unionfind/unionfind.hpp"
int main(){
  int n, q;
  cin >> n >> q;
  unionfind UF(n);
  for (int i = 0; i < q; i++){
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0){
      UF.unite(x, y);
    }
    if (com == 1){
      if (UF.same(x, y)){
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
}
#line 1 "test/aoj/dsl/dsl_1_a.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_A"
#include <bits/stdc++.h>
using namespace std;
#line 2 "data_structure/unionfind/unionfind.hpp"
struct unionfind{
  vector<int> p;
  unionfind(int N){
    p = vector<int>(N, -1);
  }
  int root(int x){
    if (p[x] < 0){
      return x;
    } else {
      p[x] = root(p[x]);
      return p[x];
    }
  }
  bool same(int x, int y){
    return root(x) == root(y);
  }
  void unite(int x, int y){
    x = root(x);
    y = root(y);
    if (x != y){
      if (p[x] < p[y]){
        swap(x, y);
      }
      p[y] += p[x];
      p[x] = y;
    }
  }
};
#line 5 "test/aoj/dsl/dsl_1_a.test.cpp"
int main(){
  int n, q;
  cin >> n >> q;
  unionfind UF(n);
  for (int i = 0; i < q; i++){
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0){
      UF.unite(x, y);
    }
    if (com == 1){
      if (UF.same(x, y)){
        cout << 1 << endl;
      } else {
        cout << 0 << endl;
      }
    }
  }
}
Back to top page