luckYrat's library.

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


:heavy_check_mark: cpp/z_test/aoj-DSL_1_B.test.cpp

Depends on

Required by

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/1/DSL_1_B"

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

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



int main(){
  int n,q;cin>>n>>q;
  PUnionFind A(n);
  for(int i = 0; q > i; i++){
    int t;cin>>t;
    if(t){
      int x,y;cin>>x>>y;
      auto ret = A.diff(x,y);
      if(ret.first)cout << ret.second << endl;
      else cout << "?" << endl;
    }else{
      int x,y,z;cin>>x>>y>>z;
      A.unite(x,y,z);
    }
  }
}
#line 1 "cpp/z_test/aoj-DSL_1_B.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/1/DSL_1_B"

#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/aoj-DSL_1_B.test.cpp"

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

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

    int r = root(par[x]);
    weight_val[x] += weight_val[par[x]];
    return par[x] = r;
  }

  int weight(int x){
    root(x);
    return weight_val[x];
  }

  pair<bool,int> diff(int x, int y){
    if(!same(x,y))return {false, 0};
    return {true, weight(y) - weight(x)};
  }

  void unite(int a,int b,int c){
    c += weight(a);
    c -= weight(b);
    int ra = root(a);
    int rb = root(b);
    if(size_[ra] < size_[rb]){
      swap(ra, rb);
      swap(a,b);
      c = -c;
    }
    if(ra==rb){
      return;
    }

    par[rb] = ra;
    size_[ra] += size_[rb];
    weight_val[rb] = c;
  }

  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;
  }
};
using PUnionFind = PotentializedUnionFind;
#line 6 "cpp/z_test/aoj-DSL_1_B.test.cpp"



int main(){
  int n,q;cin>>n>>q;
  PUnionFind A(n);
  for(int i = 0; q > i; i++){
    int t;cin>>t;
    if(t){
      int x,y;cin>>x>>y;
      auto ret = A.diff(x,y);
      if(ret.first)cout << ret.second << endl;
      else cout << "?" << endl;
    }else{
      int x,y,z;cin>>x>>y>>z;
      A.unite(x,y,z);
    }
  }
}

Test cases

Env Name Status Elapsed Memory
g++ 00_sample_00.in :heavy_check_mark: AC 6 ms 3 MB
g++ 01_small_00.in :heavy_check_mark: AC 6 ms 3 MB
g++ 02_corner_00.in :heavy_check_mark: AC 5 ms 3 MB
g++ 03_general_00.in :heavy_check_mark: AC 5 ms 3 MB
g++ 04_rand_00.in :heavy_check_mark: AC 6 ms 3 MB
g++ 04_rand_01.in :heavy_check_mark: AC 6 ms 3 MB
g++ 04_rand_02.in :heavy_check_mark: AC 5 ms 3 MB
g++ 04_rand_03.in :heavy_check_mark: AC 6 ms 3 MB
g++ 04_rand_04.in :heavy_check_mark: AC 6 ms 3 MB
g++ 04_rand_05.in :heavy_check_mark: AC 6 ms 3 MB
g++ 04_rand_06.in :heavy_check_mark: AC 6 ms 3 MB
g++ 04_rand_07.in :heavy_check_mark: AC 6 ms 3 MB
g++ 05_large_00.in :heavy_check_mark: AC 41 ms 4 MB
g++ 05_large_01.in :heavy_check_mark: AC 92 ms 4 MB
g++ 06_maximum_00.in :heavy_check_mark: AC 123 ms 4 MB
g++ 06_maximum_02.in :heavy_check_mark: AC 131 ms 4 MB
g++ 07_dense_00.in :heavy_check_mark: AC 15 ms 3 MB
g++ 07_dense_01.in :heavy_check_mark: AC 24 ms 4 MB
g++ 07_dense_02.in :heavy_check_mark: AC 77 ms 4 MB
g++ 07_dense_03.in :heavy_check_mark: AC 90 ms 4 MB
g++ 08_shell_00.in :heavy_check_mark: AC 283 ms 4 MB
g++ 09_extreme_line_00.in :heavy_check_mark: AC 336 ms 4 MB
g++ 09_extreme_same_00.in :heavy_check_mark: AC 227 ms 4 MB
g++ 09_extreme_same_01.in :heavy_check_mark: AC 250 ms 4 MB
Back to top page