Submission #2441396


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(3*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 700
Code Size 1924 Byte
Status AC
Exec Time 173 ms
Memory 26812 KB

Judge Result

Set Name Sample Subtask1 Subtask2 Subtask3 Subtask4
Score / Max Score 0 / 0 100 / 100 100 / 100 200 / 200 300 / 300
Status
AC × 4
AC × 7
AC × 12
AC × 7
AC × 28
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 23740 KB
s1_03.txt AC 1 ms 256 KB
s1_04.txt AC 1 ms 256 KB
s1_05.txt AC 74 ms 19772 KB
s1_06.txt AC 173 ms 25148 KB
s2_07.txt AC 22 ms 3836 KB
s2_08.txt AC 163 ms 26812 KB
s2_09.txt AC 136 ms 21116 KB
s2_10.txt AC 128 ms 18428 KB
s2_11.txt AC 161 ms 23452 KB
s2_12.txt AC 155 ms 23972 KB
s3_13.txt AC 20 ms 4476 KB
s3_14.txt AC 158 ms 23932 KB
s3_15.txt AC 129 ms 20220 KB
s3_16.txt AC 125 ms 18172 KB
s3_17.txt AC 153 ms 26216 KB
s3_18.txt AC 151 ms 24572 KB
s4_19.txt AC 22 ms 7420 KB
s4_20.txt AC 155 ms 24384 KB
s4_21.txt AC 134 ms 20348 KB
s4_22.txt AC 128 ms 17916 KB
s4_23.txt AC 159 ms 24236 KB
s4_24.txt AC 155 ms 23868 KB