Submission #1869755


Source Code Expand

/+ dub.sdl:
    name "A"
    dependency "dcomp" version=">=0.8.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;

// import dcomp.graph.hldecomp;

int main() {
    import std.typecons;
    Scanner sc = new Scanner(stdin);
    int n;
    sc.read(n);
    alias E = Tuple!(int, "to");
    E[][] g = new E[][](n);
    foreach (i; 0..n-1) {
        int a, b;
        sc.read(a, b); a--; b--;
        g[a] ~= E(b); g[b] ~= E(a);        
    }
    auto hlinfo = g.hlDecomposition(0);
    int[] dep = new int[n];
    void dfs(int p, int b, int nd = 0) {
        dep[p] = nd;
        foreach (e; g[p]) {
            if (e.to == b) continue;
            dfs(e.to, p, nd+1);
        }
    }
    dfs(0, -1);
    int q;
    sc.read(q);
    foreach (i; 0..q) {
        int a, b;
        sc.read(a, b); a--; b--;
        int c = hlinfo.calcLCA(a, b);
        writeln(1 + dep[a] + dep[b] - 2*dep[c]);
    }
    return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;

 
static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }
     
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/graph/hldecomp.d */
// module dcomp.graph.hldecomp;

// import dcomp.container.stackpayload;

struct HLInfo {
    int[2][] id;  
    int[][] lines;  
    int[2][] par;  
    int[] lineDPS;  
    this(size_t n) {
        id = new int[2][n];
    }
}

 
int calcLCA(in HLInfo hl, int a, int b) {
    import std.algorithm : swap;
    with (hl) {
        int[2] xl = id[a];
        int[2] yl = id[b];
        if (lineDPS[xl[0]] < lineDPS[yl[0]]) swap(xl, yl);
        while (lineDPS[xl[0]] > lineDPS[yl[0]]) {
            xl = par[xl[0]];
        }
        while (xl[0] != yl[0]) {
            xl = par[xl[0]];
            yl = par[yl[0]];
        }
        if (xl[1] > yl[1]) swap(xl, yl);
        return lines[xl[0]][xl[1]];
    }
}

HLInfo hlDecomposition(T)(in T g, int rt) {
    auto n = g.length;
    auto hl = HLInfo(n);
    with (hl) {
        int[] sz = new int[n];
        int calcSZ(int p, int b) {
            sz[p] = 1;
            foreach (e; g[p]) {
                if (e.to == b) continue;
                sz[p] += calcSZ(e.to, p);
            }
            return sz[p];
        }
        calcSZ(rt, -1);
        int idc = 0;
        StackPayload!(int[2]) par_buf;
        StackPayload!int line_buf, dps_buf;
        void dfs(int p, int b, int height) {
            line_buf ~= p;
            id[p] = [idc, height];
            int nx = -1, buf = -1;
            foreach (e; g[p]) {
                if (e.to == b) continue;
                if (buf < sz[e.to]) {
                    buf = sz[e.to];
                    nx = e.to;
                }
            }
            if (nx == -1) {
                 
                lines ~= line_buf.data.dup;
                line_buf.clear;
                idc++;
                return;
            }

            dfs(nx, p, height+1);
            foreach (e; g[p]) {
                if (e.to == b || e.to == nx) continue;
                par_buf ~= id[p];
                dps_buf ~= dps_buf.data[id[p][0]] + 1;
                dfs(e.to, p, 0);
            }
        }
        par_buf ~= [-1, -1];
        dps_buf ~= 0;
        dfs(rt, -1, 0);
        par = par_buf.data;
        lineDPS = dps_buf.data;
    }
    return hl;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

// import dcomp.container.stackpayload;

 
class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray; 
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {
                 
                 
                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}


 
 

 
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/stackpayload.d */
// module dcomp.container.stackpayload;

 
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

     
    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
    
    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }  
    ref inout(T) front() inout { return this[0]; }  
    ref inout(T) back() inout { return this[$-1]; }  

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }  
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }  
     
    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }  
    alias opOpAssign(string op : "~") = insertBack;  
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }  
}

 

/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/

Submission Info

Submission Time
Task D - 閉路
User yosupo
Language D (LDC 0.17.0)
Score 100
Code Size 8356 Byte
Status AC
Exec Time 108 ms
Memory 20860 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 30 / 30 70 / 70
Status
AC × 3
AC × 12
AC × 27
Set Name Test Cases
Sample subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
Subtask1 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 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
Case Name Status Exec Time Memory
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 53 ms 20860 KB
subtask1_02.txt AC 52 ms 17276 KB
subtask1_03.txt AC 1 ms 256 KB
subtask1_04.txt AC 1 ms 256 KB
subtask1_05.txt AC 2 ms 380 KB
subtask1_06.txt AC 2 ms 380 KB
subtask1_07.txt AC 69 ms 11516 KB
subtask1_08.txt AC 72 ms 10620 KB
subtask1_09.txt AC 71 ms 10620 KB
subtask1_10.txt AC 73 ms 10748 KB
subtask1_11.txt AC 71 ms 7932 KB
subtask1_12.txt AC 70 ms 9980 KB
subtask2_01.txt AC 77 ms 17788 KB
subtask2_02.txt AC 79 ms 17660 KB
subtask2_03.txt AC 23 ms 508 KB
subtask2_04.txt AC 25 ms 508 KB
subtask2_05.txt AC 27 ms 636 KB
subtask2_06.txt AC 27 ms 764 KB
subtask2_07.txt AC 104 ms 12412 KB
subtask2_08.txt AC 105 ms 10876 KB
subtask2_09.txt AC 105 ms 10364 KB
subtask2_10.txt AC 106 ms 10620 KB
subtask2_11.txt AC 108 ms 11772 KB
subtask2_12.txt AC 105 ms 10620 KB