This documentation is automatically generated by competitive-verifier/competitive-verifier
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_B"
#include "../../data_structure/weighted_union_find.hpp"
#include "../../template/template.hpp"
int main() {
int N, Q;
cin >> N >> Q;
WeightedUnionFind<int> uf(N);
while (Q--) {
int t, x, y;
cin >> t >> x >> y;
if (t == 0) {
int z;
cin >> z;
uf.merge(x, y, z);
} else {
if (uf.same(x, y)) {
cout << uf.diff(x, y) << endl;
} else {
cout << '?' << LF;
}
}
}
return 0;
}
#line 1 "test/data_structure/weighted_union_find.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_B"
#line 2 "data_structure/weighted_union_find.hpp"
#line 2 "template/template.hpp"
/**
* @author ku_senjan
* @title 提出用テンプレート
* @see https://github.com/kogetsu0728/ku-library
*/
#line 2 "template/constant.hpp"
#line 2 "template/include.hpp"
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#line 2 "template/type_alias.hpp"
#line 4 "template/type_alias.hpp"
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <class T, bool REVERSE = false>
using heap = priority_queue<T, vector<T>, conditional_t<REVERSE, greater<T>, less<T>>>;
#line 5 "template/constant.hpp"
template <class T>
inline constexpr T INF = numeric_limits<T>::max() / 2;
inline constexpr array<int, 4> DY4 = {0, -1, 0, 1};
inline constexpr array<int, 4> DX4 = {1, 0, -1, 0};
inline constexpr array<int, 8> DY8 = {0, -1, -1, -1, 0, 1, 1, 1};
inline constexpr array<int, 8> DX8 = {1, 1, 0, -1, -1, -1, 0, 1};
inline constexpr char LF = '\n';
#line 2 "template/macro.hpp"
#line 5 "template/macro.hpp"
/**
* @see https://trap.jp/post/1224/
*/
#ifdef LOCAL
inline constexpr bool IS_LOCAL = true;
#else
inline constexpr bool IS_LOCAL = false;
#endif
#define IF_LOCAL if constexpr (IS_LOCAL)
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define overload4(a, b, c, d, e, ...) e
#define rep1(i, a) for (ll i = 0; (i) < (ll)(a); ++(i))
#define rep2(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); ++(i))
#define rep3(i, a, b, c) for (ll i = (ll)(a); (i) < (ll)(b); (i) += (ll)(c))
#define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(i, a) for (ll i = (ll)(a); (i) >= 0; --(i))
#define rrep2(i, a, b) for (ll i = (ll)(a); (i) >= (ll)(b); --(i))
#define rrep3(i, a, b, c) for (ll i = (ll)(a); (i) >= (ll)(b); (i) -= (ll)(c))
#define rrep(...) overload4(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)
#line 13 "template/template.hpp"
#line 2 "utility/choose_min_max.hpp"
/**
* @title Choose Minimum / Maximum
*/
template <class T>
bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#line 2 "utility/set_io.hpp"
#line 4 "utility/set_io.hpp"
void set_io(int d = 16) {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(d);
return;
}
#line 4 "data_structure/weighted_union_find.hpp"
/**
* @brief Weighted Union Find (重み付きUnion Find)
*/
template <class T>
class WeightedUnionFind {
private:
int n, comp;
vector<int> par, rank;
vector<T> wei;
public:
WeightedUnionFind() : WeightedUnionFind(0) {}
WeightedUnionFind(int _n) : WeightedUnionFind(_n, T(0)) {}
WeightedUnionFind(int _n, const T& _e)
: n(_n), comp(_n), par(_n, -1), rank(_n), wei(n, _e) {}
int size() const { return comp; }
int size(int x) { return -par[leader(x)]; }
int leader(int x) {
if (par[x] < 0) {
return x;
}
int rx = leader(par[x]);
wei[x] += wei[par[x]];
return par[x] = rx;
}
T weight(int x) {
leader(x);
return wei[x];
}
bool same(int x, int y) { return leader(x) == leader(y); }
bool merge(int x, int y, T w) {
w += weight(x) - weight(y);
x = leader(x);
y = leader(y);
if (x == y) {
return false;
}
if (size(x) < size(y)) {
swap(x, y);
w = -w;
}
if (rank[x] == rank[y]) {
rank[x]++;
}
comp--;
par[x] += par[y];
par[y] = x;
wei[y] = w;
return true;
}
T diff(int x, int y) {
assert(same(x, y));
return weight(y) - weight(x);
}
vector<vector<int>> groups() {
vector<vector<int>> mem(n), res;
rep (i, 0, n) {
mem[leader(i)].push_back(i);
}
rep (i, 0, n) {
if (!mem[i].empty()) {
res.push_back(mem[i]);
}
}
return res;
}
};
#line 5 "test/data_structure/weighted_union_find.test.cpp"
int main() {
int N, Q;
cin >> N >> Q;
WeightedUnionFind<int> uf(N);
while (Q--) {
int t, x, y;
cin >> t >> x >> y;
if (t == 0) {
int z;
cin >> z;
uf.merge(x, y, z);
} else {
if (uf.same(x, y)) {
cout << uf.diff(x, y) << endl;
} else {
cout << '?' << LF;
}
}
}
return 0;
}
| Env | Name | Status | Elapsed | Memory |
|---|---|---|---|---|
| g++ | 00_sample_00.in |
|
5 ms | 3 MB |
| g++ | 01_small_00.in |
|
4 ms | 3 MB |
| g++ | 02_corner_00.in |
|
5 ms | 3 MB |
| g++ | 03_general_00.in |
|
4 ms | 3 MB |
| g++ | 04_rand_00.in |
|
4 ms | 4 MB |
| g++ | 04_rand_01.in |
|
4 ms | 4 MB |
| g++ | 04_rand_02.in |
|
5 ms | 3 MB |
| g++ | 04_rand_03.in |
|
4 ms | 4 MB |
| g++ | 04_rand_04.in |
|
5 ms | 3 MB |
| g++ | 04_rand_05.in |
|
5 ms | 4 MB |
| g++ | 04_rand_06.in |
|
5 ms | 4 MB |
| g++ | 04_rand_07.in |
|
5 ms | 3 MB |
| g++ | 05_large_00.in |
|
68 ms | 4 MB |
| g++ | 05_large_01.in |
|
50 ms | 4 MB |
| g++ | 06_maximum_00.in |
|
109 ms | 4 MB |
| g++ | 06_maximum_02.in |
|
108 ms | 4 MB |
| g++ | 07_dense_00.in |
|
18 ms | 4 MB |
| g++ | 07_dense_01.in |
|
29 ms | 4 MB |
| g++ | 07_dense_02.in |
|
48 ms | 4 MB |
| g++ | 07_dense_03.in |
|
83 ms | 4 MB |
| g++ | 08_shell_00.in |
|
271 ms | 4 MB |
| g++ | 09_extreme_line_00.in |
|
214 ms | 4 MB |
| g++ | 09_extreme_same_00.in |
|
294 ms | 4 MB |
| g++ | 09_extreme_same_01.in |
|
234 ms | 4 MB |