Submission #1984084


Source Code Expand

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long int;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 373;

#define rep(i, n) for (int i = 0; i < (n); ++i)

template <typename T>
using vector2 = vector<vector<T>>;
template <typename T>
vector2<T> init_vector2(size_t n0, size_t n1, T e = T()) {
    return vector2<T>(n0, vector<T>(n1, e));
}

template <typename T>
using vector3 = vector<vector<vector<T>>>;
template <typename T>
vector3<T> init_vector3(size_t n0, size_t n1, size_t n2, T e = T()) {
    return vector3<T>(n0, vector2<T>(n1, vector<T>(n2, e)));
}

class union_find {
   private:
    vector<int> p;
    vector<int> h;
    vector<int> w;

   public:
    union_find(int n) : p(n, -1), h(n, 0), w(n, 1) {}

    int find(int u) {
        if (p[u] == -1) {
            return u;
        }
        return p[u] = find(p[u]);
    }

    void unite(int u, int v) {
        const int u_rt = find(u);
        const int v_rt = find(v);

        if (u_rt == v_rt) {
            return;
        }

        if (h[u_rt] > h[v_rt]) {
            p[v_rt] = u_rt;
            w[u_rt] += w[v_rt];
        } else {
            p[u_rt] = v_rt;
            w[v_rt] += w[u_rt];
            if (h[u_rt] == h[v_rt]) {
                h[v_rt]++;
            }
        }
    }

    int weight(int u) { return w[find(u)]; }
    bool same(int u, int v) { return find(u) == find(v); }
};

int main() {
    int n, m;
    cin >> n >> m;

    union_find uf(n);
    rep(i, m) {
        int a, b;
        cin >> a >> b;
        uf.unite(a - 1, b - 1);
    }

    int cnt = 0;
    rep (i, n) {
        if (i == uf.find(i)) {
            cnt++;
        }
    }
    cout << cnt - 1 << endl;

    return 0;
}

Submission Info

Submission Time
Task B - 道路工事
User somq14
Language C++14 (Clang 3.8.0)
Score 100
Code Size 1921 Byte
Status AC
Exec Time 160 ms
Memory 1408 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 22
Set Name Test Cases
Sample sample1.txt, sample2.txt
All 0.txt, 1.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 2.txt, 3.txt, 4.txt, 5.txt, 6.txt, 7.txt, 8.txt, 9.txt, sample1.txt, sample2.txt
Case Name Status Exec Time Memory
0.txt AC 1 ms 256 KB
1.txt AC 1 ms 256 KB
10.txt AC 1 ms 256 KB
11.txt AC 1 ms 256 KB
12.txt AC 1 ms 256 KB
13.txt AC 1 ms 256 KB
14.txt AC 1 ms 256 KB
15.txt AC 123 ms 256 KB
16.txt AC 2 ms 1408 KB
17.txt AC 2 ms 1408 KB
18.txt AC 2 ms 1408 KB
19.txt AC 160 ms 1408 KB
2.txt AC 1 ms 256 KB
3.txt AC 1 ms 256 KB
4.txt AC 1 ms 256 KB
5.txt AC 1 ms 256 KB
6.txt AC 1 ms 256 KB
7.txt AC 1 ms 256 KB
8.txt AC 1 ms 256 KB
9.txt AC 1 ms 256 KB
sample1.txt AC 1 ms 256 KB
sample2.txt AC 1 ms 256 KB