C++ · Solution

Road Network

This C++ solution uses disjoint set union for Road Network. Read the reasoning, inspect the code, or try your own test case below.

GeometryDisjoint set unionC++63 lines
Solution177of 248
Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Disjoint set union

Road Network: maintain connected components with parent representatives, merging sets as relationships are added and querying representatives to test connectivity.

ConnectivityGraphs

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

View exact source file ↗
Implementation

Road_Network.cpp

C++

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int INF = 0x3f3f3f3f; //❄️
    #define int long long
    vector<int> parent;
    struct node {
        int c, x, y;
        node(int c, int x, int y) : c(c), x(x), y(y){}
    };
    struct edge {
        int u, v; double d;
        edge(int u, int v, double d) : u(u), v(v), d(d){}
        bool operator<(const edge& other) const {
            return d < other.d;
        }
    };
    int find(int x) {
        if(parent[x] == x) return x;
        return parent[x] = find(parent[x]);
    }
    void unite(int x, int y) {
        x = find(x), y = find(y);
        parent[x] = y;
    }
    signed main() {
        ios::sync_with_stdio(0); cin.tie(0); 
        int n, m; cin >> n >> m;
        parent.resize(n + 1);
        for (int i = 0; i <= n; i++) parent[i] = i;
        vector<node> vv;
        vector<edge> e;
        for (int i = 0; i < n; i++) {
            int c = i + 1, x, y; cin >> x >> y;
            vv.emplace_back(c, x, y);
        }
        for (int i = 0; i < m; i++) {
            int x, y; cin >> x >> y;
            e.emplace_back(x, y, 0);
        }
        for (int i = 0; i < vv.size(); i++) {
            auto [c, x, y] = vv[i];
            for (int j = i + 1; j < vv.size(); j++) {
                auto[cc, xx, yy] = vv[j];
                double dist = sqrt((xx - x) * (xx - x) + (yy - y) * (yy - y));
                e.emplace_back(c, cc, dist);
            }
        }
        sort(e.begin(), e.end());
        int cnt = 0;
        double ans = 0;
        for (int i = 0; i < e.size(); i++) {
            auto[u, v, d] = e[i];
            if(find(u) != find(v)){
                unite(u, v);
                cnt++;
                ans += d;
            }
            if (cnt == n - 1) break;
        }
        cout << fixed << setprecision(2) << ans << '\n';
        return 0;
    }
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.