結果

問題 No.1600 Many Shortest Path Problems
ユーザー SSRSSSRS
提出日時 2021-07-10 00:04:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 478 ms / 4,000 ms
コード長 5,293 bytes
コンパイル時間 2,409 ms
コンパイル使用メモリ 193,140 KB
実行使用メモリ 42,104 KB
最終ジャッジ日時 2023-09-14 11:29:04
合計ジャッジ時間 16,788 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 415 ms
41,748 KB
testcase_05 AC 431 ms
41,680 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 206 ms
6,100 KB
testcase_11 AC 332 ms
7,992 KB
testcase_12 AC 438 ms
16,656 KB
testcase_13 AC 468 ms
31,676 KB
testcase_14 AC 441 ms
41,588 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 478 ms
23,860 KB
testcase_18 AC 436 ms
41,612 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 457 ms
23,820 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 436 ms
41,656 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 208 ms
29,508 KB
testcase_30 AC 231 ms
27,728 KB
testcase_31 AC 389 ms
23,932 KB
testcase_32 AC 401 ms
23,748 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 302 ms
42,104 KB
testcase_36 AC 176 ms
39,720 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 192 ms
27,888 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 230 ms
27,740 KB
testcase_41 AC 192 ms
29,640 KB
testcase_42 AC 221 ms
27,860 KB
testcase_43 AC 211 ms
28,052 KB
testcase_44 AC 239 ms
26,732 KB
testcase_45 AC 209 ms
29,604 KB
testcase_46 AC 226 ms
27,840 KB
testcase_47 AC 229 ms
27,684 KB
testcase_48 AC 241 ms
27,464 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 1 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 10000000;
const long long MOD = 1000000007;
struct unionfind{
	vector<int> p;
	unionfind(int N){
		p = vector<int>(N, -1);
	}
	int root(int x){
		if (p[x] < 0){
			return x;
		} else {
			p[x] = root(p[x]);
			return p[x];
		}
	}
	bool same(int x, int y){
		return root(x) == root(y);
	}
	void unite(int x, int y){
		x = root(x);
		y = root(y);
		if (x != y){
			if (p[x] < p[y]){
				swap(x, y);
			}
			p[y] += p[x];
			p[x] = y;
		}
	}
};
struct dual_segment_tree{
  int N;
  vector<int> ST;
  dual_segment_tree(){
  }
  dual_segment_tree(int N2){
    N = 1;
    while (N < N2){
      N *= 2;
    }
    ST = vector<int>(N * 2 - 1, INF);
  }
  int operator [](int k){
    k += N - 1;
    int ans = ST[k];
    while (k > 0){
      k = (k - 1) / 2;
      ans = min(ans, ST[k]);
    }
    return ans;
  }
  void range_chmin(int L, int R, int x, int i, int l, int r){
    if (r <= L || R <= l){
      return;
    } else if (L <= l && r <= R){
      ST[i] = min(ST[i], x);
    } else {
      int m = (l + r) / 2;
      range_chmin(L, R, x, i * 2 + 1, l, m);
      range_chmin(L, R, x, i * 2 + 2, m, r);
    }
  }
  void range_chmin(int L, int R, int x){
    range_chmin(L, R, x, 0, 0, N);
  }
};
struct heavy_light_decomposition{
  vector<int> p, sz, in, next;
  vector<int> d1;
  vector<long long> d2;
  dual_segment_tree ST;
  heavy_light_decomposition(vector<int> &p, vector<vector<pair<long long, int>>> &c): p(p){
    int N = p.size();
    sz = vector<int>(N, 0);
    d1 = vector<int>(N, 0);
    d2 = vector<long long>(N, 0);
    dfs1(c);
    in = vector<int>(N, 0);
    next = vector<int>(N, 0);
    int t = 0;
    dfs2(c, t);
    ST = dual_segment_tree(N);
  }
  void dfs1(vector<vector<pair<long long, int>>> &c, int v = 0){
    sz[v] = 1;
    for (auto &P : c[v]){
      int w = P.second;
      d1[w] = d1[v] + 1;
      d2[w] = d2[v] + P.first;
      dfs1(c, w);
      sz[v] += sz[w];
      if (sz[w] > sz[c[v][0].second]){
        swap(P, c[v][0]);
      }
    }
  }
  void dfs2(vector<vector<pair<long long, int>>> &c, int &t, int v = 0){
    in[v] = t;
    t++;
    for (auto P : c[v]){
      int w = P.second;
      if (P == c[v][0]){
        next[w] = next[v];
      } else {
        next[w] = w;
      }
      dfs2(c, t, w);
    }
  }
  int lca(int x, int y){
    while (true){
      if (in[x] > in[y]){
        swap(x, y);
      }
      if (next[x] == next[y]){
        return x;
      }
      y = p[next[y]];
    }
  }
  int dist1(int x, int y){
    return d1[x] + d1[y] - 2 * d1[lca(x, y)];
  }
  long long dist2(int x, int y){
    return d2[x] + d2[y] - 2 * d2[lca(x, y)];
  }
  int operator [](int v){
    return ST[in[v]];
  }
  void path_chmin(int u, int v, int x){
    int w = lca(u, v);
    while (next[u] != next[w]){
      ST.range_chmin(in[next[u]], in[u] + 1, x);
      u = p[next[u]];
    }
    ST.range_chmin(in[w] + 1, in[u] + 1, x);
    while (next[v] != next[w]){
      ST.range_chmin(in[next[v]], in[v] + 1, x);
      v = p[next[v]];
    }
    ST.range_chmin(in[w] + 1, in[v] + 1, x);
  }
};
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> E(M);
  for (int i = 0; i < M; i++){
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    E[i] = make_pair(A, B);
  }
  vector<long long> cost(M);
  cost[0] = 2;
  for (int i = 1; i < M; i++){
    cost[i] = cost[i - 1] * 2 % MOD;
  }
  unionfind UF(N);
  vector<bool> span(M, false);
  vector<vector<pair<long long, int>>> E2(N);
  for (int i = 0; i < M; i++){
    int A = E[i].first;
    int B = E[i].second;
    if (!UF.same(A, B)){
      span[i] = true;
      UF.unite(A, B);
      E2[A].push_back(make_pair(cost[i], B));
      E2[B].push_back(make_pair(cost[i], A));
    }
  }
  vector<int> p(N, -1);
  vector<vector<pair<long long, int>>> c(N);
  vector<int> d(N, 0);
  queue<int> q;
  q.push(0);
  while (!q.empty()){
    int v = q.front();
    q.pop();
    for (auto P : E2[v]){
      int w = P.second;
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(P);
        d[w] = d[v] + 1;
        q.push(w);
      }
    }
  }
  heavy_light_decomposition T(p, c);
  for (int i = 0; i < M; i++){
    if (!span[i]){
      int A = E[i].first;
      int B = E[i].second;
      T.path_chmin(A, B, i);
    }
  }
  vector<int> T2(N);
  for (int i = 0; i < N; i++){
    T2[i] = T[i];
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++){
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    z--;
    int a = E[z].first;
    int b = E[z].second;
    if (!span[z]){
      cout << T.dist2(x, y) % MOD << "\n";
    } else {
      int d1 = T.dist1(x, a) + T.dist1(b, y);
      int d2 = T.dist1(x, b) + T.dist1(a, y);
      int d3 = T.dist1(x, y);
      if (d1 + 1 != d3 && d2 + 1 != d3){
        cout << T.dist2(x, y) % MOD << "\n";
      } else {
        if (b == p[a]){
          swap(a, b);
        }
        int mn = T2[b];
        if (mn == INF){
          cout << -1 << "\n";
        } else {
          int c = E[mn].first;
          int d = E[mn].second;
          long long ans1 = T.dist2(x, c) + cost[mn] + T.dist2(d, y);
          long long ans2 = T.dist2(x, d) + cost[mn] + T.dist2(c, y);
          cout << min(ans1, ans2) % MOD << "\n";
        }
      }
    }
  }
}
0