結果

問題 No.1326 ふたりのDominator
ユーザー 👑 NachiaNachia
提出日時 2022-01-27 19:40:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 10,521 bytes
コンパイル時間 1,667 ms
コンパイル使用メモリ 101,228 KB
実行使用メモリ 13,672 KB
最終ジャッジ日時 2024-09-23 03:05:15
合計ジャッジ時間 3,700 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 60 ms
8,756 KB
testcase_13 AC 61 ms
8,628 KB
testcase_14 AC 62 ms
8,640 KB
testcase_15 AC 60 ms
8,692 KB
testcase_16 AC 59 ms
8,560 KB
testcase_17 AC 60 ms
9,132 KB
testcase_18 AC 68 ms
10,496 KB
testcase_19 AC 56 ms
10,584 KB
testcase_20 AC 59 ms
11,648 KB
testcase_21 AC 62 ms
11,684 KB
testcase_22 AC 75 ms
13,672 KB
testcase_23 AC 51 ms
8,756 KB
testcase_24 AC 61 ms
8,624 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(buf.back());
            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>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)

struct HLD{

    int N;
    vector<int> P;
    vector<int> PP;
    vector<int> PD;
    vector<int> D;

    HLD(const nachia::AdjacencyList& E = nachia::AdjacencyList(1,{},false)){
        N = E.num_vertices();
        P.assign(N, -1);
        vector<int> I = {0};
        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;
            }
        }
        vector<int> Z(N, 1);
        for(int i=N-1; i>=1; i--) Z[P[I[i]]] += Z[I[i]];
        PP.resize(N);
        for(int i=0; i<N; i++) PP[i] = i;
        vector<int> nx(N, -1);
        for(int p : I) if(p != 0){
            if(nx[P[p]] == -1) nx[P[p]] = p;
            if(Z[nx[P[p]]] < Z[p]) nx[P[p]] = p;
        }
        for(int i=0; i<N; i++) if(nx[i] != -1) PP[nx[i]] = i;
        for(int p : I) if(p != 0) PP[p] = PP[PP[p]];

        PD.assign(N,N);
        PD[0] = 0;
        for(int p : I) if(p != 0) PD[p] = min(PD[PP[p]], PD[P[p]]+1);
        D.assign(N,0);
        for(int p : I) D[p] = ((P[p] < 0) ? 0 : D[P[p]]+1);
    }


    int lca(int u, int v){
        if(PD[u] < PD[v]) 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;
    }
  
    vector<vector<int>> lcaP;
    void fastLcaInit(){
        lcaP.assign(5,vector<int>(N,0));
        for(int i=0; i<N; i++) if(PP[i] != 0) lcaP[0][i] = P[PP[i]];
        for(int d=0; d<4; d++) for(int i=0; i<N; i++) lcaP[d+1][i] = lcaP[d][lcaP[d][i]];
    }
    int fastLca(int u, int v){
        if(PD[u] < PD[v]) swap(u, v);
        for(int d=4; d>=0; d--) if(PD[u]-PD[v] >= (1<<d)) u = lcaP[d][u];
        for(int d=4; d>=0; d--) if(lcaP[d][u] != lcaP[d][v]){ u = lcaP[d][u]; v = lcaP[d][v]; }
        if(PP[u] != PP[v]){ u = lcaP[0][u]; v = lcaP[0][v]; }
        return (D[u] > D[v]) ? v : u;
    }

    int dist(int u, int v){
        int g = fastLca(u,v);
        return D[u] + D[v] - D[g] * 2;
    }

};


#include <cstdio>

int main() {
    int n; cin >> n;
    int m; cin >> m;
    std::vector<std::pair<int, int>> edges(m);
    for(auto& [u,v] : edges){ cin >> u >> v; u--; v--; }
    auto hld_bct = HLD(nachia::BiconnectedComponents(n, edges).get_bct());
    hld_bct.fastLcaInit();
    int q; cin >> q;
    for(int queryi=0; queryi<q; queryi++){
        int u,v; cin >> u >> v; u--; v--;
        int d = hld_bct.dist(u,v);
        int ans = std::max(0, d/2-1);
        cout << ans << "\n";
    }
    return 0;
}

struct ios_do_not_sync{
    ios_do_not_sync(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0