Submission #2441393


Source Code Expand

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

void main() {
    immutable int INF = 1 << 30;

    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto K = s[2];
    auto C = readln.split.map!(to!int).array;
    auto G = new Tuple!(int, long)[][](N);
    foreach (i; 0..M) {
        s = readln.split.map!(to!int);
        G[s[0]-1] ~= tuple(s[1]-1, s[2].to!long);
        G[s[1]-1] ~= tuple(s[0]-1, s[2].to!long);
    }

    if (K == 1) {
        writeln(0);
        return;
    }

    Tuple!(int, int, long)[] E;
    
    foreach (i; 0..N) {
        int n = C[i] == 0 ? i + N + 1 : C[i];
        foreach (j; G[i]) {
            int m = C[j[0]] == 0 ? j[0] + N + 1 : C[j[0]];
            if (j[0] > i) continue;
            if (n == m) continue;
            E ~= tuple(n, m, j[1]);
        }
    }

    E.sort!"a[2] < b[2]"();
    auto uf = new UnionFind(2*N);
    long ans = 0;
    int cnt = 0;

    foreach (e; E) {
        if (uf.find(e[0]) == uf.find(e[1])) continue;
        uf.unite(e[0], e[1]);
        ans += e[2];
        cnt += 1;
        if (cnt == K-1) {
            ans.writeln;
            return;
        }
    }

    writeln(-1);
}


class UnionFind {
    int N;
    int[] table;
    long[] cost;

    this(int n) {
        N = n;
        table = new int[](N);
        fill(table, -1);
        cost = new long[](N);
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (table[x] > table[y]) swap(x, y);
        table[x] += table[y];
        table[y] = x;
        cost[x] += cost[y];
    }
}

Submission Info

Submission Time
Task A - Colorful MST
User nebukuro09
Language D (LDC 0.17.0)
Score 200
Code Size 1924 Byte
Status RE
Exec Time 166 ms
Memory 24892 KB

Judge Result

Set Name Sample Subtask1 Subtask2 Subtask3 Subtask4
Score / Max Score 0 / 0 100 / 100 100 / 100 0 / 200 0 / 300
Status
AC × 4
AC × 7
AC × 12
AC × 3
RE × 4
AC × 22
RE × 6
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 00_example_04.txt
Subtask1 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt
Subtask2 s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s2_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt
Subtask3 00_example_02.txt, s3_13.txt, s3_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt
Subtask4 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 00_example_04.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s2_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s3_13.txt, s3_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt, s4_19.txt, s4_20.txt, s4_21.txt, s4_22.txt, s4_23.txt, s4_24.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 1 ms 256 KB
00_example_02.txt AC 1 ms 256 KB
00_example_03.txt AC 1 ms 256 KB
00_example_04.txt AC 1 ms 256 KB
s1_01.txt AC 1 ms 256 KB
s1_02.txt AC 166 ms 24892 KB
s1_03.txt AC 1 ms 256 KB
s1_04.txt AC 1 ms 256 KB
s1_05.txt AC 72 ms 16700 KB
s1_06.txt AC 166 ms 23228 KB
s2_07.txt AC 22 ms 4988 KB
s2_08.txt AC 162 ms 24124 KB
s2_09.txt AC 137 ms 21372 KB
s2_10.txt AC 122 ms 18428 KB
s2_11.txt AC 153 ms 22556 KB
s2_12.txt AC 159 ms 24612 KB
s3_13.txt RE 20 ms 3324 KB
s3_14.txt AC 153 ms 22396 KB
s3_15.txt AC 129 ms 20092 KB
s3_16.txt RE 124 ms 15868 KB
s3_17.txt RE 150 ms 23548 KB
s3_18.txt RE 153 ms 21372 KB
s4_19.txt AC 21 ms 4732 KB
s4_20.txt AC 158 ms 22976 KB
s4_21.txt AC 132 ms 19964 KB
s4_22.txt AC 128 ms 17276 KB
s4_23.txt RE 162 ms 23980 KB
s4_24.txt RE 154 ms 22588 KB