Submission #1515275


Source Code Expand

import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

int readint() {
    return readln.chomp.to!int;
}

int[] readints() {
    return readln.split.map!(to!int).array;
}

void main() {
    auto nm = readints();
    int n = nm[0], m = nm[1];

    auto uf = new UnionFind(n + 1);
    for (int i = 0; i < m; i++) {
        auto ab = readints();
        int a = ab[0], b = ab[1];

        uf.unite(a, b);
    }

    int ans = 0;
    for (int i = 2; i <= n; i++) {
        if (!uf.isSame(1, i)) {
            ans++;
            uf.unite(1, i);
        }
    }

    writeln(ans);
}

class UnionFind {
    private int[] _data;

    this(int n) {
        _data = new int[](n + 1);
        _data[] = -1;
    }

    int root(int a) {
        if (_data[a] < 0)
            return a;
        return _data[a] = root(_data[a]);
    }

    bool unite(int a, int b) {
        int rootA = root(a);
        int rootB = root(b);
        if (rootA == rootB)
            return false;
        _data[rootA] += _data[rootB];
        _data[rootB] = rootA;
        return true;
    }

    bool isSame(int a, int b) {
        return root(a) == root(b);
    }

    int size(int a) {
        return -_data[root(a)];
    }
}

Submission Info

Submission Time
Task B - 道路工事
User noriok
Language D (DMD64 v2.070.1)
Score 100
Code Size 1365 Byte
Status AC
Exec Time 90 ms
Memory 2556 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 75 ms 1276 KB
16.txt AC 3 ms 636 KB
17.txt AC 3 ms 2556 KB
18.txt AC 3 ms 636 KB
19.txt AC 90 ms 1660 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