AtCoder Beginner Contest 014

Submission #1886795

Source codeソースコード

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;
import std.concurrency;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;

void main() {
    int N;
    scanln(N);

    auto lca = LCA(N);
    (N-1).times!({
        int x, y;
        scanln(x, y);
        x--; y--;
        lca.addEdge(x, y);
    });

    int Q;
    scanln(Q);
    Q.times!({
        int a, b;
        scanln(a, b);
        a--; b--;
        int depthA = lca.getDepth(a);
        int depthB = lca.getDepth(b);
        int depthC = lca.queryDepth(a, b);
        writeln((depthA - depthC) + (depthB - depthC) + 1);
    });
}


struct LCA {

private:
    alias SegT = SegTree!(SegNode, (a, b) => a.depth<b.depth ? a:b, SegNode(int.max, size_t.max));

    size_t _size;
    Vertex[] _vertices;
    size_t _rootIndex;
    SegT _segT;
    bool _builded;

public:
    // O(N)
    this(size_t size) {
        _size = size;
        _vertices.length = size;
        foreach(i, ref v; _vertices) {
            v = new Vertex(i);
        }
        _rootIndex = 0;
        _builded = false;
    }

    // O(1)
    void addEdge(size_t x, size_t y) {
        _builded = false;
        Vertex vx = _vertices[x];
        Vertex vy = _vertices[y];
        vx.edges ~= Edge(vx, vy);
        vy.edges ~= Edge(vy, vx);
    }

    // O(1)
    void setRoot(size_t index) {
        _builded = false;
        _rootIndex = index;
    }

    // O(log N)
    size_t queryIndex(size_t x, size_t y) {
        return querySegNode(x, y).index;
    }

    // O(log N)
    int queryDepth(size_t x, size_t y) {
        return querySegNode(x, y).depth;
    }

    // O(1)
    int getDepth(size_t x) {
        // return queryDepth(x, x);
        if (!_builded) build();
        return _segT.get(_vertices[x].id).depth;
    }

private:

    // O(N)
    void build() {
        _vertices.each!(v => v.visited = false);

        // Euler tour
        SegNode[] segNodes = new SegNode[2*_size];
        size_t dfs(Vertex v, int depth, size_t id) {
            segNodes[id].depth = depth;
            segNodes[id].index = v.index;
            v.id = id;
            foreach(u; v.edges.map!"a.end") {
                if (u.visited) continue;
                u.visited = true;
                id = dfs(u, depth + 1, id + 1) + 1;
                segNodes[id].depth = depth;
                segNodes[id].index = v.index;
            }
            return id;
        }
        _vertices[_rootIndex].visited = true;
        dfs(_vertices[_rootIndex], 0, 0);

        _segT = SegT(segNodes);

        _builded = true;
    }

    // O(log N)
    SegNode querySegNode(size_t x, size_t y) {
        if (!_builded) build();
        size_t idX = _vertices[x].id;
        size_t idY = _vertices[y].id;
        return _segT.query(min(idX, idY), max(idX, idY) + 1);
    }

    class Vertex {
        size_t index;
        size_t id;
        bool visited;
        Edge[] edges;
        this(size_t index) {
            this.index = index;
        }
    }
    struct Edge {
        Vertex start, end;
    }
    struct SegNode {
        int depth;
        size_t index;
    }
}

// SegTree (Segment Tree)
struct SegTree(T, alias fun, T initValue)
    if (is(typeof(fun(T.init, T.init)) : T)) {

private:
    Node[] _data;
    size_t _size;
    size_t _l, _r;

public:
    // size ... データ数
    // initValue ... 初期値(例えばRMQだとINF)
    this(size_t size) {
        init(size);
    }

    // 配列で指定
    this(T[] ary) {
        init(ary.length);
        update(ary);
    }

    // O(N)
    void init(size_t size){
        _size = 1;
        while(_size < size) {
            _size *= 2;
        }
        _data.length = _size*2-1;
        _data[] = Node(size_t.max, initValue);
        _l = 0;
        _r = size;
    }

    // i番目の要素をxに変更
    // O(logN)
    void update(size_t i, T x) {
        size_t index = i;
        i += _size-1;
        _data[i] = Node(index, x);
        while(i > 0) {
            i = (i-1)/2;
            Node nl = _data[i*2+1];
            Node nr = _data[i*2+2];
            _data[i] = select(nl, nr);
        }
    }

    // 配列で指定
    // O(N)
    void update(T[] ary) {
        foreach(i, e; ary) {
            _data[i+_size-1] = Node(i, e);
        }
        foreach(i; (_size-1).iota.retro) {
            Node nl = _data[i*2+1];
            Node nr = _data[i*2+2];
            _data[i] = select(nl, nr);
        }
    }

    // 区間[a, b)でのクエリ (値の取得)
    // O(logN)
    T query(size_t a, size_t b) {
        return queryRec(a, b, 0, 0, _size).value;
    }

    // 区間[a, b)でのクエリ (indexの取得)
    // O(logN)
    size_t queryIndex(size_t a, size_t b) out(result) {
        // fun == (a, b) => a+b のようなときはindexを聞くとassertion
        assert(result != size_t.max);
    } body {
        return queryRec(a, b, 0, 0, _size).index;
    }

    private Node queryRec(size_t a, size_t b, size_t k, size_t l, size_t r) {
        if (b<=l || r<=a) return Node(size_t.max, initValue);
        if (a<=l && r<=b) return _data[k];
        Node nl = queryRec(a, b, k*2+1, l, (l+r)/2);
        Node nr = queryRec(a, b, k*2+2, (l+r)/2, r);
        return select(nl, nr);
    }

    private Node select(Node nl, Node nr) {
        T v = fun(nl.value, nr.value);
        if (nl.value == v) {
            return nl;
        } else if (nr.value == v) {
            return nr;
        } else {
            return Node(size_t.max, v);
        }
    }

    // O(1)
    T get(size_t i) {
        return _data[_size-1 + i].value;
    }

    // O(N)
    T[] array() {
        return _data[_l+_size-1.._r+_size-1].map!"a.value".array;
    }

private:
    struct Node {
        size_t index;
        T value;
    }
}


// ----------------------------------------------

void scanln(Args...)(ref Args args) {
    foreach(i, ref v; args) {
        "%d".readf(&v);
        (i==args.length-1 ? "\n" : " ").readf;
    }
    // ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}

void times(alias fun)(int n) {
    // n.iota.each!(i => fun());
    foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
    // return n.iota.map!(i => fun()).array;
    T[] res = new T[n];
    foreach(ref e; res) e = fun();
    return res;
}

// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
    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);
            }
        }
    }
}

// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
    template cumulativeFold(fun...)
    if (fun.length >= 1)
    {
        import std.meta : staticMap;
        private alias binfuns = staticMap!(binaryFun, fun);

        auto cumulativeFold(R)(R range)
        if (isInputRange!(Unqual!R))
        {
            return cumulativeFoldImpl(range);
        }

        auto cumulativeFold(R, S)(R range, S seed)
        if (isInputRange!(Unqual!R))
        {
            static if (fun.length == 1)
                return cumulativeFoldImpl(range, seed);
            else
                return cumulativeFoldImpl(range, seed.expand);
        }

        private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
        {
            import std.algorithm.internal : algoFormat;

            static assert(Args.length == 0 || Args.length == fun.length,
                algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
                    Args.stringof, fun.length));

            static if (args.length)
                alias State = staticMap!(Unqual, Args);
            else
                alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);

            foreach (i, f; binfuns)
            {
                static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
                        { args[i] = f(args[i], e); }()),
                    algoFormat("Incompatible function/seed/element: %s/%s/%s",
                        fullyQualifiedName!f, Args[i].stringof, E.stringof));
            }

            static struct Result
            {
            private:
                R source;
                State state;

                this(R range, ref Args args)
                {
                    source = range;
                    if (source.empty)
                        return;

                    foreach (i, f; binfuns)
                    {
                        static if (args.length)
                            state[i] = f(args[i], source.front);
                        else
                            state[i] = source.front;
                    }
                }

            public:
                @property bool empty()
                {
                    return source.empty;
                }

                @property auto front()
                {
                    assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
                    static if (fun.length > 1)
                    {
                        import std.typecons : tuple;
                        return tuple(state);
                    }
                    else
                    {
                        return state[0];
                    }
                }

                void popFront()
                {
                    assert(!empty, "Attempting to popFront an empty cumulativeFold.");
                    source.popFront;

                    if (source.empty)
                        return;

                    foreach (i, f; binfuns)
                        state[i] = f(state[i], source.front);
                }

                static if (isForwardRange!R)
                {
                    @property auto save()
                    {
                        auto result = this;
                        result.source = source.save;
                        return result;
                    }
                }

                static if (hasLength!R)
                {
                    @property size_t length()
                    {
                        return source.length;
                    }
                }
            }

            return Result(range, args);
        }
    }
}

// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
    auto minElement(alias map, Range)(Range r)
    if (isInputRange!Range && !isInfinite!Range)
    {
        alias mapFun = unaryFun!map;
        auto element = r.front;
        auto minimum = mapFun(element);
        r.popFront;
        foreach(a; r) {
            auto b = mapFun(a);
            if (b < minimum) {
                element = a;
                minimum = b;
            }
        }
        return element;
    }
    auto maxElement(alias map, Range)(Range r)
    if (isInputRange!Range && !isInfinite!Range)
    {
        alias mapFun = unaryFun!map;
        auto element = r.front;
        auto maximum = mapFun(element);
        r.popFront;
        foreach(a; r) {
            auto b = mapFun(a);
            if (b > maximum) {
                element = a;
                maximum = b;
            }
        }
        return element;
    }
}

Submission

Task問題 D - 閉路
User nameユーザ名 Ark
Created time投稿日時
Language言語 D (DMD64 v2.070.1)
Status状態 AC
Score得点 100
Source lengthソースコード長 11983 Byte
File nameファイル名
Exec time実行時間 522 ms
Memory usageメモリ使用量 47100 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt
Subtask1 30 / 30 subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt
Subtask2 70 / 70 subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt,subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt,subtask2_01.txt,subtask2_02.txt,subtask2_03.txt,subtask2_04.txt,subtask2_05.txt,subtask2_06.txt,subtask2_07.txt,subtask2_08.txt,subtask2_09.txt,subtask2_10.txt,subtask2_11.txt,subtask2_12.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
subtask0_sample01.txt AC 1 ms 256 KB
subtask0_sample02.txt AC 1 ms 256 KB
subtask0_sample03.txt AC 1 ms 256 KB
subtask1_01.txt AC 172 ms 46076 KB
subtask1_02.txt AC 173 ms 46716 KB
subtask1_03.txt AC 1 ms 256 KB
subtask1_04.txt AC 1 ms 256 KB
subtask1_05.txt AC 3 ms 508 KB
subtask1_06.txt AC 3 ms 636 KB
subtask1_07.txt AC 185 ms 31612 KB
subtask1_08.txt AC 189 ms 33148 KB
subtask1_09.txt AC 190 ms 34940 KB
subtask1_10.txt AC 192 ms 33404 KB
subtask1_11.txt AC 187 ms 33276 KB
subtask1_12.txt AC 186 ms 32380 KB
subtask2_01.txt AC 330 ms 46716 KB
subtask2_02.txt AC 445 ms 47100 KB
subtask2_03.txt AC 106 ms 508 KB
subtask2_04.txt AC 153 ms 508 KB
subtask2_05.txt AC 202 ms 892 KB
subtask2_06.txt AC 201 ms 892 KB
subtask2_07.txt AC 510 ms 33788 KB
subtask2_08.txt AC 506 ms 32764 KB
subtask2_09.txt AC 522 ms 35196 KB
subtask2_10.txt AC 519 ms 32764 KB
subtask2_11.txt AC 516 ms 34428 KB
subtask2_12.txt AC 520 ms 34172 KB