cp-library

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

View the Project on GitHub Hoshinonono/cp-library

:heavy_check_mark: Test/Aizu Online Judge/DSL/DSL_1_B.test.cpp

Depends on

Code

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

#include <bits/stdc++.h>

#include "Graph/dsu_01_weighted.hpp"


int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    int n, q;
    std::cin >> n >> q;
    Weighted_dsu uf(n);
    while(q--){
        int cmd, u, v, d;
        std::cin >> cmd >> u >> v;
        if(cmd == 0) {
            std::cin >> d;
            uf.merge(u, v, d);
        } else {
            if(!uf.same(u, v)){
                std::cout << '?' << '\n';
                continue;
            }
            std::cout << uf.diff(u, v) << '\n';
        }
    }
}
#line 1 "Test/Aizu Online Judge/DSL/DSL_1_B.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/1/DSL_1_B"

#include <bits/stdc++.h>

#line 1 "Graph/dsu_01_weighted.hpp"
struct Weighted_dsu {
    public:
    Weighted_dsu() : _n(0) {}
    Weighted_dsu(int n) : _n(n), num_component(n), parent_or_size(n, -1), diff_weight(n) {}

    bool merge(int a, int b, long long w) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        w += diff_weight[a] - diff_weight[b];
        if(x == y) return w == 0;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y), w *= -1;
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        diff_weight[y] = w;
        num_component--;
        return true;
    }

    long long diff(int a, int b) {
        assert(same(a, b));
        return diff_weight[b] - diff_weight[a];
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        int r = leader(parent_or_size[a]);
        diff_weight[a] += diff_weight[parent_or_size[a]];
        return parent_or_size[a] = r;
    }

    int size() { 
        return num_component;
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }
    private:
        int _n, num_component;
        std::vector<int> parent_or_size;
        std::vector<long long> diff_weight;
};
#line 5 "Test/Aizu Online Judge/DSL/DSL_1_B.test.cpp"

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    int n, q;
    std::cin >> n >> q;
    Weighted_dsu uf(n);
    while(q--){
        int cmd, u, v, d;
        std::cin >> cmd >> u >> v;
        if(cmd == 0) {
            std::cin >> d;
            uf.merge(u, v, d);
        } else {
            if(!uf.same(u, v)){
                std::cout << '?' << '\n';
                continue;
            }
            std::cout << uf.diff(u, v) << '\n';
        }
    }
}
Back to top page