結果

問題 No.1600 Many Shortest Path Problems
ユーザー 👑 NachiaNachia
提出日時 2022-01-22 15:49:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 299 ms / 4,000 ms
コード長 8,739 bytes
コンパイル時間 1,680 ms
コンパイル使用メモリ 107,156 KB
実行使用メモリ 34,716 KB
最終ジャッジ日時 2024-11-27 13:13:55
合計ジャッジ時間 14,504 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 299 ms
32,556 KB
testcase_05 AC 298 ms
32,420 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 109 ms
9,544 KB
testcase_11 AC 129 ms
10,684 KB
testcase_12 AC 187 ms
15,712 KB
testcase_13 AC 255 ms
25,340 KB
testcase_14 AC 292 ms
32,420 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 2 ms
5,248 KB
testcase_17 AC 216 ms
20,920 KB
testcase_18 AC 297 ms
32,436 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 2 ms
5,248 KB
testcase_21 AC 209 ms
20,912 KB
testcase_22 AC 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 296 ms
32,436 KB
testcase_25 AC 2 ms
5,248 KB
testcase_26 AC 1 ms
5,248 KB
testcase_27 AC 2 ms
5,248 KB
testcase_28 AC 1 ms
5,248 KB
testcase_29 AC 145 ms
19,932 KB
testcase_30 AC 151 ms
20,712 KB
testcase_31 AC 181 ms
20,972 KB
testcase_32 AC 180 ms
20,872 KB
testcase_33 AC 2 ms
5,248 KB
testcase_34 AC 2 ms
5,248 KB
testcase_35 AC 211 ms
33,624 KB
testcase_36 AC 156 ms
34,716 KB
testcase_37 AC 2 ms
5,248 KB
testcase_38 AC 150 ms
20,840 KB
testcase_39 AC 2 ms
5,248 KB
testcase_40 AC 159 ms
20,720 KB
testcase_41 AC 135 ms
19,932 KB
testcase_42 AC 141 ms
20,708 KB
testcase_43 AC 140 ms
20,716 KB
testcase_44 AC 145 ms
20,652 KB
testcase_45 AC 142 ms
19,936 KB
testcase_46 AC 149 ms
20,712 KB
testcase_47 AC 153 ms
20,716 KB
testcase_48 AC 156 ms
20,700 KB
testcase_49 AC 1 ms
5,248 KB
testcase_50 AC 2 ms
5,248 KB
testcase_51 AC 1 ms
5,248 KB
testcase_52 AC 2 ms
5,248 KB
testcase_53 AC 1 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <utility>
#include <iostream>

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; }
    };
private:
    int mn;
    std::vector<int> E;
    std::vector<int> I;
public:
    AdjacencyList(int n, std::vector<std::pair<int,int>> edges, bool rev){
        if(n == 0){ mn = 0; I = {0}; E = {}; return; }
        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);
    }
    AdjacencyListRange operator[](int u) const {
        return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] };
    }
    int num_vertices() const { return mn; }
};

#include <iostream>
#include <vector>
#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 <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ull MOD = 1000000007;
#define rep(i,n) for(int i=0; i<(n); i++)

struct mll{
  using u32 = uint32_t;
  using u64 = uint64_t;
  u32 v;
  mll(u32 x = 0) : v(x) {}
  u32 val() const { return v; }
  mll& operator+=(const mll& r){ v += r.val(); if(v >= MOD) v -= MOD; return *this; }
  mll& operator-=(const mll& r){ v += MOD - r.val(); if(v >= MOD) v -= MOD; return *this; }
  mll& operator*=(const mll& r) { v = (u64)v * r.val() % MOD; return *this; }
  mll operator+(const mll& r) const { mll res = *this; res += r; return res; }
  mll operator-(const mll& r) const { mll res = *this; res -= r; return res; }
  mll operator*(const mll& r) const { mll res = *this; res *= r; return res; }
  mll operator-() const { return v ? mll(MOD-v) : mll(0); }
};

struct DSU{
  vector<int> V;
  DSU(int n){
    V.resize(n);
    rep(i,n) V[i] = i;
  }
  int leader(int a){ if(V[a] == a) return a; return V[a] = leader(V[a]); }
  void merge(int r,int c){ V[leader(c)] = leader(r); }
};

struct Edge{ int u,v,i; };

int N, M;
vector<Edge> J;
vector<vector<Edge>> E;
vector<Edge> flows;
vector<mll> cost;

vector<int> P;
vector<int> Eidx;
vector<mll> dep;

vector<int> iEidx;
vector<int> flowIdx;

nachia::HeavyLightDecomposition hld;



void read_graph(){
  cin >> N >> M;
  E.resize(N);
  vector<pair<int,int>> idE;
  cost.resize(M);
  cost[0] = 2;
  for(int i=1; i<M; i++) cost[i] = cost[i-1] * 2;
  DSU G1(N);
  rep(i,M){
    int u,v; cin >> u >> v; u--; v--;
    J.push_back({u,v,i});
    if(G1.leader(u) == G1.leader(v)){
      flows.push_back({u,v,i});
    }
    else{
      G1.merge(u,v);
      E[u].push_back({u,v,i});
      E[v].push_back({v,u,i});
      idE.push_back(make_pair(u,v));
    }
  }
  hld = nachia::HeavyLightDecomposition(AdjacencyList(N, idE, true));
}

void initLCA(){
  vector<int> I;
  P.assign(N,-1);
  I.push_back(0);
  dep.assign(N,0);
  Eidx.assign(N,-1);
  rep(i,I.size()){
    int p = I[i];
    for(Edge e : E[p]) if(P[p] != e.v){
      P[e.v] = p;
      dep[e.v] = dep[p] + cost[e.i];
      Eidx[e.v] = e.i;
      I.push_back(e.v);
    }
  }
}

int LCA(int u,int v){
  return hld.lca(u,v);
}

int dist0(int u,int v){
  return hld.dist(u,v);
}
mll dist1(int u,int v){
  int g = LCA(u,v);
  return dep[u] + dep[v] - dep[g] * 2;
}

void build_flows(){

  iEidx.assign(M,-1);
  rep(i,N) if(Eidx[i] != -1) iEidx[Eidx[i]] = i;

  flowIdx.assign(N,-1);
  
  DSU G2(N);
  for(Edge e : flows){
    int g = G2.leader(LCA(e.u,e.v));
    for(int s : {e.u,e.v}){
      int p = G2.leader(s);
      while(p != g){
        flowIdx[p] = e.i;
        G2.merge(P[p],p);
        p = G2.leader(p);
      }
    }
  }
}

int path_includes(int u,int v,int z){
  if(iEidx[z] == -1) return -1;
  if(hld.median(u,v,J[z].u) != J[z].u) return -1;
  if(hld.median(u,v,J[z].v) != J[z].v) return -1;
  int res = flowIdx[iEidx[z]];
  if(res == -1) return -2;
  return res;
}

mll shortest_using(int u,int v,int z){
  mll ans = cost[z];
  if(dist0(u,J[z].u) + dist0(v,J[z].v) < dist0(u,J[z].v) + dist0(v,J[z].u))
    ans += dist1(u,J[z].u) + dist1(v,J[z].v);
  else
    ans += dist1(u,J[z].v) + dist1(v,J[z].u);
  return ans;
}


int main(){
  read_graph();
  initLCA();
  build_flows();
  int Q; cin >> Q;
  rep(q,Q){
    int u,v,z; cin >> u >> v >> z; u--; v--; z--;
    int inc = path_includes(u,v,z);
    if(inc == -2){ cout << "-1\n"; continue; }
    if(inc == -1){ cout << dist1(u,v).val() << "\n"; continue; }
    auto e = J[inc];
    mll ans = shortest_using(u,v,e.i);
    cout << ans.val() << "\n";
  }
  return 0;
}

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

0