結果

問題 No.1600 Many Shortest Path Problems
ユーザー 👑 NachiaNachia
提出日時 2021-07-22 23:07:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 547 ms / 4,000 ms
コード長 4,565 bytes
コンパイル時間 1,718 ms
コンパイル使用メモリ 107,844 KB
実行使用メモリ 42,416 KB
最終ジャッジ日時 2024-07-17 19:48:00
合計ジャッジ時間 14,748 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 522 ms
41,360 KB
testcase_05 AC 530 ms
41,560 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 121 ms
9,572 KB
testcase_11 AC 156 ms
10,968 KB
testcase_12 AC 263 ms
18,740 KB
testcase_13 AC 405 ms
31,744 KB
testcase_14 AC 510 ms
41,440 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 311 ms
25,260 KB
testcase_18 AC 538 ms
41,384 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 315 ms
25,256 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 547 ms
41,432 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 208 ms
24,236 KB
testcase_30 AC 214 ms
25,116 KB
testcase_31 AC 247 ms
25,284 KB
testcase_32 AC 267 ms
25,216 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 296 ms
42,408 KB
testcase_36 AC 233 ms
42,416 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 209 ms
25,136 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 216 ms
25,140 KB
testcase_41 AC 177 ms
24,360 KB
testcase_42 AC 183 ms
25,008 KB
testcase_43 AC 186 ms
25,140 KB
testcase_44 AC 210 ms
25,024 KB
testcase_45 AC 205 ms
24,272 KB
testcase_46 AC 207 ms
25,132 KB
testcase_47 AC 219 ms
25,144 KB
testcase_48 AC 233 ms
25,104 KB
testcase_49 AC 2 ms
5,376 KB
testcase_50 AC 2 ms
5,376 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/modint>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace atcoder;
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ull MOD = 1000000007;
using mll = static_modint<1000000007>;
#define rep(i,n) for(int i=0; i<(n); i++)

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 HLD{

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

  vector<int> rangeL;
  vector<int> rangeR;

  HLD(const vector<vector<int>>& E = {{}}){
    N = E.size();
    P.assign(N, -1);
    vector<int> I = {0};
    rep(i,I.size()){
      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;
    }
    rep(i,N) 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 == 0) ? 0 : (D[P[p]]+1);
    
    lcaP.assign(5,vector<int>(N,0));
    rep(i,N) if(PP[i] != 0) lcaP[0][i] = P[PP[i]];
    rep(d,4) rep(i,N) lcaP[d+1][i] = lcaP[d][lcaP[d][i]];
  }
  
  int lca(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){
    return D[u] + D[v] - 2 * D[lca(u,v)];
  }
};


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;
HLD hld;
vector<int> Eidx;
vector<mll> dep;

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



void read_graph(){
  cin >> N >> M;
  vector<vector<int>> E2(N);
  E.resize(N);
  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});
      E2[u].push_back(v);
      E2[v].push_back(u);
    }
  }
  hld = HLD(E2);
}

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);
    }
  }
}

mll dist1(int u,int v){
  int g = hld.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(hld.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.dist(u,J[z].u) + hld.dist(v,J[z].v) == hld.dist(v,J[z].u) + hld.dist(u,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(hld.dist(u,J[z].u) + hld.dist(v,J[z].v) < hld.dist(u,J[z].v) + hld.dist(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