結果

問題 No.1600 Many Shortest Path Problems
ユーザー SSRSSSRS
提出日時 2021-07-10 00:05:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 894 ms / 4,000 ms
コード長 5,242 bytes
コンパイル時間 2,352 ms
コンパイル使用メモリ 194,352 KB
実行使用メモリ 42,276 KB
最終ジャッジ日時 2023-09-14 11:29:45
合計ジャッジ時間 28,025 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 864 ms
41,592 KB
testcase_05 AC 887 ms
41,752 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 595 ms
6,200 KB
testcase_11 AC 750 ms
8,052 KB
testcase_12 AC 894 ms
16,876 KB
testcase_13 AC 872 ms
31,676 KB
testcase_14 AC 849 ms
41,752 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 885 ms
24,008 KB
testcase_18 AC 862 ms
41,640 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 876 ms
23,928 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 866 ms
41,584 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 637 ms
29,696 KB
testcase_30 AC 666 ms
27,800 KB
testcase_31 AC 831 ms
23,828 KB
testcase_32 AC 849 ms
23,832 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 773 ms
42,276 KB
testcase_36 AC 625 ms
39,988 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 638 ms
27,884 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 673 ms
27,852 KB
testcase_41 AC 637 ms
29,736 KB
testcase_42 AC 647 ms
27,948 KB
testcase_43 AC 638 ms
27,888 KB
testcase_44 AC 674 ms
26,648 KB
testcase_45 AC 649 ms
29,736 KB
testcase_46 AC 656 ms
27,960 KB
testcase_47 AC 654 ms
27,904 KB
testcase_48 AC 674 ms
27,524 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 1 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 1 ms
4,380 KB
testcase_53 AC 2 ms
4,376 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(){
  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 << endl;
    } 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 << endl;
      } else {
        if (b == p[a]){
          swap(a, b);
        }
        int mn = T2[b];
        if (mn == INF){
          cout << -1 << endl;
        } 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 << endl;
        }
      }
    }
  }
}
0