Kuro-orzz's Library

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

View on GitHub

:heavy_check_mark: DataStructure/DSU/test/aizu_dsl_1_b_weighted_dsu.test.cpp

Depends on

Code

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

#include "../../../template.h"
#include "../Weighted_Dsu.h"

void solve() {
    int n, q; cin >> n >> q;
    WeightedDsu g(n);
    while (q--) {
        int type; cin >> type;
        if (type == 0) {
            int x, y, z; cin >> x >> y >> z;
            g.merge(x, y, z);
        } else if (type == 1) {
            int x, y; cin >> x >> y;
            int res = g.getDiff(x, y);
            if (res != INT_MAX) {
                cout << res << '\n';
            } else {
                cout << "?\n";
            }
        }
    }
}
#line 1 "DataStructure/DSU/test/aizu_dsl_1_b_weighted_dsu.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/1/DSL_1_B"

#line 2 "template.h"

#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define MOD (ll)(1e9+7)
#define all(x) (x).begin(),(x).end()
#define unique(x) x.erase(unique(all(x)), x.end())
#define INF32 ((1ull<<31)-1)
#define INF64 ((1ull<<63)-1)
#define inf (ll)1e18

#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

const int mod = 998244353;

void solve();

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    // cin.exceptions(cin.failbit);
    // int t; cin >> t;
    // while(t--)
        solve();
    cerr << "\nTime run: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << '\n';
    return 0;
}
#line 2 "DataStructure/DSU/Weighted_Dsu.h"

struct WeightedDsu {
    vector<int> par, sz;
    vector<ll> diff;

    WeightedDsu() {}
    WeightedDsu(int n): par(n+1), sz(n+1, 1), diff(n+1) {
        iota(all(par), 0);
    }

    int find(int v) {
        if (v == par[v]) return v;
        int p = par[v];
        int root = find(p);
        diff[v] += diff[p];
        return par[v] = root;
    }

    // diff[v] - diff[u] = x
    void merge(int u, int v, int x) {
        int pu = find(u);
        int pv = find(v);
        if (pu == pv) return;
        if(sz[pu] < sz[pv]) {
            swap(u, v);
            swap(pu, pv);
            x = -x;
        }
        par[pv] = pu;
        sz[pu] += sz[pv];
        diff[pv] = diff[u] - diff[v] + x;
    }

    // diff[v] - diff[u]
    int getDiff(int u, int v) {
        int pu = find(u);
        int pv = find(v);
        if (pu != pv) return INT_MAX;
        return diff[v] - diff[u];
    }
};
#line 5 "DataStructure/DSU/test/aizu_dsl_1_b_weighted_dsu.test.cpp"

void solve() {
    int n, q; cin >> n >> q;
    WeightedDsu g(n);
    while (q--) {
        int type; cin >> type;
        if (type == 0) {
            int x, y, z; cin >> x >> y >> z;
            g.merge(x, y, z);
        } else if (type == 1) {
            int x, y; cin >> x >> y;
            int res = g.getDiff(x, y);
            if (res != INT_MAX) {
                cout << res << '\n';
            } else {
                cout << "?\n";
            }
        }
    }
}
Back to top page