Submission #1001624


Source Code Expand

import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
void times(alias fun)(int n) {
    foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
    T[] res = new T[n];
    foreach(ref e; res) e = fun();
    return res;
}
// fold was added in D 2.071.0.
template fold(fun...) if (fun.length >= 1) {
    auto fold(R, S...)(R r, S seed) {
        static if (S.length < 2) {
            return reduce!fun(seed, r);
        } else {
            return reduce!fun(tuple(seed), r);
        }
    }
}

long INF = long.max/3;
void main() {
    int N, Q;
    readf("%d %d\n", &N, &Q);

    auto tree = redBlackTree!((a, b) => a.w < b.w, true, Edge)([]);
    Edge[] es = N.iota.map!(i => Edge(i, (i+1)%N, INF)).array;
    Q.times!({
        int a, b, c;
        readf("%d %d %d\n", &a, &b, &c);
        tree.insert(Edge(a, b, c));
        es[a].w = min(es[a].w, c+1);
        es[b].w = min(es[b].w, c+2);
    });
    foreach(i; 0..N*2) {
        es[(i+1)%N].w = min(es[(i+1)%N].w, es[i%N].w + 2);
    }
    tree.insert(es);

    long ans = 0;
    auto set = DisjointSet(N);
    tree[].each!((e){
        if (!set.same(e.s, e.g)) {
            set.unite(e.s, e.g);
            ans += e.w;
        }
    });
    ans.writeln;
}

struct Edge {
    int s, g;
    long w;
}

// 互いに素な集合
// UnionFindTreeによる実装
struct DisjointSet {

private:
    Vertex[] _vertices;

public:
    this(size_t size) {
        _vertices = new Vertex[size];
        foreach(i, ref v; _vertices) {
            v.index = i;
            v.parent = i;
        }
    }

    void makeSet(size_t[] indices...) {
        foreach(index; indices) {
            _vertices[index] = Vertex(index, index);
        }
    }

    void unite(size_t x, size_t y) {
        link(findSet(x), findSet(y));
    }

    void link(size_t x, size_t y) {
        if (x==y) return;
        if (_vertices[x].rank > _vertices[y].rank) {
            _vertices[y].parent = x;
        } else {
            _vertices[x].parent = y;
            if (_vertices[x].rank == _vertices[y].rank) {
                _vertices[y].rank++;
            }
        }
    }

    bool same(size_t x, size_t y) {
        return findSet(x) == findSet(y);
    }

    size_t findSet(size_t index) {
        if (_vertices[index].parent == index) {
            return index;
        } else {
            return _vertices[index].parent = findSet(_vertices[index].parent);
        }
    }

private:
    struct Vertex {
        size_t index;
        size_t parent;
        size_t rank = 1;
    }
}

Submission Info

Submission Time
Task G - Zigzag MST
User arkark
Language D (DMD64 v2.070.1)
Score 1300
Code Size 2860 Byte
Status AC
Exec Time 471 ms
Memory 34044 KB

Judge Result

Set Name sample all
Score / Max Score 0 / 0 1300 / 1300
Status
AC × 3
AC × 33
Set Name Test Cases
sample sample-01.txt, sample-02.txt, sample-03.txt
all sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt
Case Name Status Exec Time Memory
01-01.txt AC 2 ms 256 KB
01-02.txt AC 263 ms 13436 KB
01-03.txt AC 426 ms 34044 KB
01-04.txt AC 106 ms 21116 KB
01-05.txt AC 103 ms 21116 KB
01-06.txt AC 96 ms 21116 KB
01-07.txt AC 84 ms 21116 KB
01-08.txt AC 90 ms 21244 KB
01-09.txt AC 110 ms 21756 KB
01-10.txt AC 297 ms 27772 KB
01-11.txt AC 390 ms 23804 KB
01-12.txt AC 467 ms 33788 KB
01-13.txt AC 460 ms 34044 KB
01-14.txt AC 471 ms 34044 KB
01-15.txt AC 466 ms 34044 KB
01-16.txt AC 458 ms 34044 KB
01-17.txt AC 460 ms 34044 KB
01-18.txt AC 411 ms 34044 KB
01-19.txt AC 90 ms 21116 KB
01-20.txt AC 136 ms 21244 KB
01-21.txt AC 185 ms 24700 KB
01-22.txt AC 413 ms 34044 KB
01-23.txt AC 372 ms 34044 KB
01-24.txt AC 123 ms 21884 KB
01-25.txt AC 372 ms 34044 KB
01-26.txt AC 102 ms 21116 KB
01-27.txt AC 128 ms 21756 KB
01-28.txt AC 292 ms 29180 KB
01-29.txt AC 337 ms 25084 KB
01-30.txt AC 401 ms 34044 KB
sample-01.txt AC 2 ms 256 KB
sample-02.txt AC 2 ms 256 KB
sample-03.txt AC 2 ms 256 KB