結果

問題 No.1326 ふたりのDominator
ユーザー 👑 NachiaNachia
提出日時 2022-01-27 19:12:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 12,536 bytes
コンパイル時間 1,541 ms
コンパイル使用メモリ 99,932 KB
実行使用メモリ 12,172 KB
最終ジャッジ日時 2024-09-23 03:06:13
合計ジャッジ時間 5,695 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 67 ms
9,120 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 AC 64 ms
12,172 KB
testcase_22 RE -
testcase_23 RE -
testcase_24 AC 69 ms
9,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <utility>

namespace nachia{

struct AdjacencyList{
public:
    struct AdjacencyListRange{
        using iterator = typename std::vector<int>::const_iterator;
        iterator begi, endi;
        iterator begin() const { return begi; }
        iterator end() const { return endi; }
        int size() const { return (int)std::distance(begi, endi); }
        const int& operator[](int i) const { return begi[i]; }
    };
private:
    int mn;
    std::vector<int> E;
    std::vector<int> I;
public:
    AdjacencyList(int n, std::vector<std::pair<int,int>> edges, bool rev){
        mn = n;
        std::vector<int> buf(n+1, 0);
        for(auto [u,v] : edges){ ++buf[u]; if(rev) ++buf[v]; }
        for(int i=1; i<=n; i++) buf[i] += buf[i-1];
        E.resize(buf[n]);
        for(int i=(int)edges.size()-1; i>=0; i--){
            auto [u,v] = edges[i];
            E[--buf[u]] = v;
            if(rev) E[--buf[v]] = u;
        }
        I = std::move(buf);
    }
    AdjacencyList(const std::vector<std::vector<int>>& edges = {}){
        int n = mn = edges.size();
        std::vector<int> buf(n+1, 0);
        for(int i=0; i<n; i++) buf[i+1] = buf[i] + edges[i].size();
        E.resize(buf[n]);
        for(int i=0; i<n; i++) for(int j=0; j<(int)edges[i].size(); j++) E[buf[i]+j] = edges[i][j];
        I = std::move(buf);
    }
    static AdjacencyList from_raw(std::vector<int> targets, std::vector<int> bounds){
        AdjacencyList res;
        res.mn = bounds.size() - 1;
        res.E = std::move(targets);
        res.I = std::move(bounds);
        return res;
    }
    AdjacencyListRange operator[](int u) const {
        return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] };
    }
    int num_vertices() const { return mn; }
    int num_edges() const { return E.size(); }
    AdjacencyList reversed_edges() const {
        AdjacencyList res;
        int n = res.mn = mn;
        std::vector<int> buf(n+1, 0);
        for(int v : E) ++buf[v];
        for(int i=1; i<=n; i++) buf[i] += buf[i-1];
        res.E.resize(buf[n]);
        for(int u=0; u<n; u++) for(int v : operator[](u)) res.E[--buf[v]] = u;
        res.I = std::move(buf);
        return res;
    }
};



struct AdjacencyListEdgeIndexed{
public:
    struct Edge { int to; int edgeidx; };
    struct AdjacencyListRange{
        using iterator = typename std::vector<Edge>::const_iterator;
        iterator begi, endi;
        iterator begin() const { return begi; }
        iterator end() const { return endi; }
        int size() const { return (int)std::distance(begi, endi); }
        const Edge& operator[](int i) const { return begi[i]; }
    };
private:
    int mn;
    std::vector<Edge> E;
    std::vector<int> I;
public:
    AdjacencyListEdgeIndexed(int n, const std::vector<std::pair<int,int>>& edges, bool rev){
        mn = n;
        std::vector<int> buf(n+1, 0);
        for(auto [u,v] : edges){ ++buf[u]; if(rev) ++buf[v]; }
        for(int i=1; i<=n; i++) buf[i] += buf[i-1];
        E.resize(buf[n]);
        for(int i=(int)edges.size()-1; i>=0; i--){
            auto [u,v] = edges[i];
            E[--buf[u]] = { v, i };
            if(rev) E[--buf[v]] = { u, i };
        }
        I = std::move(buf);
    }
    AdjacencyListEdgeIndexed() : AdjacencyListEdgeIndexed(0, {}, false) {}
    AdjacencyListRange operator[](int u) const {
        return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] };
    }
    int num_vertices() const { return mn; }
    int num_edges() const { return E.size(); }
    AdjacencyListEdgeIndexed reversed_edges() const {
        AdjacencyListEdgeIndexed res;
        int n = res.mn = mn;
        std::vector<int> buf(n+1, 0);
        for(auto [v,i] : E) ++buf[v];
        for(int i=1; i<=n; i++) buf[i] += buf[i-1];
        res.E.resize(buf[n]);
        for(int u=0; u<n; u++) for(auto [v,i] : operator[](u)) res.E[--buf[v]] = {u,i};
        res.I = std::move(buf);
        return res;
    }
};

} // namespace nachia

#include <vector>
#include <iostream>

namespace nachia{

class BiconnectedComponents{
private:
    int mn;
    int mm;
    int mnum_bcs;
    std::vector<std::pair<int, int>> medges;
    std::vector<int> edgeidx_to_bcidx;
public:
    BiconnectedComponents(int n, std::vector<std::pair<int, int>> edges){
        std::vector<int> dfsi_to_vtx;
        std::vector<int> vtx_to_dfsi;
        std::vector<int> linked_over;
        std::vector<int> dfs_parent;
        mn = n;
        int m = edges.size();
        medges = std::move(edges);
        nachia::AdjacencyListEdgeIndexed adj(n, medges, true);
        dfsi_to_vtx.resize(n);
        vtx_to_dfsi.resize(n);
        dfs_parent.assign(n, -1);
        linked_over.assign(n, -1);
        int dfsi = 0;

        auto dfs1 = [&](int p, auto self)->int {
            vtx_to_dfsi[p] = dfsi;
            dfsi_to_vtx[dfsi] = p;
            int backedge = dfsi;
            dfsi++;
            for(auto [nx,i] : adj[p]){
                if(dfs_parent[nx] != -1) backedge = std::min(backedge, vtx_to_dfsi[nx]);
                else{
                    dfs_parent[nx] = i;
                    int link = self(nx, self);
                    backedge = std::min(backedge, link);
                    linked_over[nx] = (link < vtx_to_dfsi[p]) ? 1 : 0;
                }
            }
            return backedge;
        };
        for(int i=0; i<n; i++) if(dfs_parent[i] == -1){
            dfs_parent[i] = -2;
            dfs1(i, dfs1);
        }
        
        std::vector<int> res(m);
        auto dfs2 = [&](int p, int bcid, int& maxbcid, auto self)-> void {
            if(dfs_parent[p] < 0){
                for(auto [nx,i] : adj[p]) if(dfs_parent[nx] == i){
                    bcid = maxbcid++;
                    self(nx, bcid, maxbcid, self);
                }
                return;
            }
            for(auto [nx,i] : adj[p]) if(dfs_parent[nx] != i) res[i] = bcid;
            for(auto [nx,i] : adj[p]) if(dfs_parent[nx] == i){
                int nx_bcid = bcid;
                if(!linked_over[nx]) nx_bcid = maxbcid++;
                self(nx, nx_bcid, maxbcid, self);
            }
        };
        int bcid = 0;
        for(int i=0; i<n; i++) if(dfs_parent[i] < 0) dfs2(i, -1, bcid, dfs2);
        edgeidx_to_bcidx = std::move(res);
        mm = m;
        mnum_bcs = bcid;
    }

    int get_num_bcts() const { return mnum_bcs; }

    std::vector<std::vector<int>> get_bcs() const {
        std::vector<std::vector<int>> res(mnum_bcs);
        for(int i=0; i<mm; i++){
            res[edgeidx_to_bcidx[i]].push_back(i);
        }
        return res;
    }

    AdjacencyList get_bct() const {
        int bct_n = mn + mnum_bcs;
        AdjacencyList bc_edgelists; {
            std::vector<int> buf(mnum_bcs+1);
            for(int bci : edgeidx_to_bcidx) ++buf[bci];
            for(int i=1; i<=mnum_bcs; i++) buf[i] += buf[i-1];
            std::vector<int> E(mnum_bcs+1);
            for(int i=0; i<mm; i++) E[--buf[edgeidx_to_bcidx[i]]] = i;
            bc_edgelists = AdjacencyList::from_raw(std::move(E), std::move(buf));
        }
        std::vector<std::pair<int, int>> res(bct_n - 1);
        int resi = 0;
        std::vector<int> visited(mn);
        for(int bci=0; bci<mnum_bcs; bci++){
            for(int e : bc_edgelists[bci]){
                auto [u,v] = medges[e];
                if(!visited[u]){ visited[u] = 1; res[resi++] = {mn+bci,u}; }
                if(!visited[v]){ visited[v] = 1; res[resi++] = {mn+bci,v}; }
            }
            for(int e : bc_edgelists[bci]){
                auto [u,v] = medges[e];
                visited[u] = visited[v] = 0;
            }
        }
        return AdjacencyList(bct_n, res, true);
    }
};

} // namespace nachia


#include <algorithm>

namespace nachia{

struct HeavyLightDecomposition{
private:

    int N;
    std::vector<int> P;
    std::vector<int> PP;
    std::vector<int> PD;
    std::vector<int> D;
    std::vector<int> I;

    std::vector<int> rangeL;
    std::vector<int> rangeR;

public:

    HeavyLightDecomposition(const AdjacencyList& E = AdjacencyList(1, {}, false)){
        N = E.num_vertices();
        P.assign(N, -1);
        I = {0};
        I.reserve(N);
        for(int i=0; i<(int)I.size(); i++){
            int p = I[i];
            for(int e : E[p]) if(P[p] != e){
                I.push_back(e);
                P[e] = p;
            }
        }
        std::vector<int> Z(N, 1);
        std::vector<int> nx(N, -1);
        PP.resize(N);
        for(int i=0; i<N; i++) PP[i] = i;
        for(int i=N-1; i>=1; i--){
            int p = I[i];
            Z[P[p]] += Z[p];
            if(nx[P[p]] == -1) nx[P[p]] = p;
            if(Z[nx[P[p]]] < Z[p]) nx[P[p]] = p;
        }

        for(int p : I) if(nx[p] != -1) PP[nx[p]] = p;

        PD.assign(N,N);
        PD[0] = 0;
        D.assign(N,0);
        for(int p : I) if(p != 0){
            PP[p] = PP[PP[p]];
            PD[p] = std::min(PD[PP[p]], PD[P[p]]+1);
            D[p] = D[P[p]]+1;
        }
        
        rangeL.assign(N,0);
        rangeR.assign(N,0);
        std::vector<int> dfs;
        dfs.push_back(0);
        while(dfs.size()){
            int p = dfs.back();
            rangeR[p] = rangeL[p] + Z[p];
            int ir = rangeR[p];
            dfs.pop_back();
            for(int e : E[p]) if(P[p] != e) if(e != nx[p]){
                rangeL[e] = (ir -= Z[e]);
                dfs.push_back(e);
            }
            if(nx[p] != -1){
                rangeL[nx[p]] = rangeL[p] + 1;
                dfs.push_back(nx[p]);
            }
        }

        I.resize(N);
        for(int i=0; i<N; i++) I[rangeL[i]] = i;
    }

    int depth(int p) const {
        return D[p];
    }

    int lca(int u, int v) const {
        if(PD[u] < PD[v]) std::swap(u, v);
        while(PD[u] > PD[v]) u = P[PP[u]];
        while(PP[u] != PP[v]){ u = P[PP[u]]; v = P[PP[v]]; }
        return (D[u] > D[v]) ? v : u;
    }

    int dist(int u, int v) const {
        return depth(u) + depth(v) - depth(lca(u,v)) * 2;
    }

    std::vector<std::pair<int,int>> path(int r, int c, bool include_root = true, bool reverse_path = false) const {
        if(PD[c] < PD[r]) return {};
        std::vector<std::pair<int,int>> res(PD[c]-PD[r]+1);
        for(int i=0; i<(int)res.size()-1; i++){
            res[i] = std::make_pair(rangeL[PP[c]], rangeL[c]+1);
            c = P[PP[c]];
        }
        if(PP[r] != PP[c] || D[r] > D[c]) return {};
        res.back() = std::make_pair(rangeL[r]+(include_root?0:1), rangeL[c]+1);
        if(res.back().first == res.back().second) res.pop_back();
        if(!reverse_path) std::reverse(res.begin(),res.end());
        else for(auto& a : res) a = std::make_pair(N - a.second, N - a.first);
        return move(res);
    }

    std::pair<int,int> subtree(int p){
        return std::make_pair(rangeL[p], rangeR[p]);
    }

    int to_seq(int vertex) const {
        return rangeL[vertex];
    }
    int to_vtx(int seqidx) const {
        return I[seqidx];
    }

    int median(int x, int y, int z) const {
        return lca(x,y) ^ lca(y,z) ^ lca(x,z);
    }

    int la(int from, int to, int d) const {
        if(d < 0) return -1;
        int g = lca(from,to);
        int dist0 = D[from] - D[g] * 2 + D[to];
        if(dist0 < d) return -1;
        int p = from;
        if(D[from] - D[g] < d){ p = to; d = dist0 - d; }
        while(D[p] - D[PP[p]] < d){
            d -= D[p] - D[PP[p]] + 1;
            p = P[PP[p]];
        }
        return I[rangeL[p] - d];
    }
};

} // namespace nachia


#include <cstdio>

/*
int main() {
    int n; scanf("%d", &n);
    int m; scanf("%d", &m);
    std::vector<std::pair<int, int>> edges(m);
    for(auto& [u,v] : edges) scanf("%d%d", &u, &v);
    auto bcs = nachia::BiconnectedComponents(n, edges).get_bcs();
    printf("%d\n", (int)bcs.size());
    for(auto& bc : bcs){
        printf("%d", (int)bc.size());
        for(auto v : bc) printf(" %d", v);
        printf("\n");
    }
    return 0;
}
*/
int main() {
    int n; scanf("%d", &n);
    int m; scanf("%d", &m);
    std::vector<std::pair<int, int>> edges(m);
    for(auto& [u,v] : edges){ scanf("%d%d", &u, &v); u--; v--; }
    auto hld_bct = nachia::HeavyLightDecomposition(nachia::BiconnectedComponents(n, edges).get_bct());
    int q; scanf("%d", &q);
    for(int queryi=0; queryi<q; queryi++){
        int u,v; scanf("%d%d", &u, &v); u--; v--;
        int d = hld_bct.dist(u,v);
        int ans = std::max(0, d/2-1);
        printf("%d\n", ans);
    }
    return 0;
}
0