結果

問題 No.1600 Many Shortest Path Problems
ユーザー e869120e869120
提出日時 2021-05-07 15:39:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 866 ms / 4,000 ms
コード長 5,774 bytes
コンパイル時間 1,494 ms
コンパイル使用メモリ 98,916 KB
実行使用メモリ 102,056 KB
最終ジャッジ日時 2024-07-01 14:19:28
合計ジャッジ時間 24,010 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
22,016 KB
testcase_01 AC 16 ms
22,016 KB
testcase_02 AC 16 ms
22,144 KB
testcase_03 AC 17 ms
22,144 KB
testcase_04 AC 825 ms
101,616 KB
testcase_05 AC 817 ms
101,616 KB
testcase_06 AC 17 ms
22,144 KB
testcase_07 AC 16 ms
22,144 KB
testcase_08 AC 16 ms
22,144 KB
testcase_09 AC 16 ms
22,016 KB
testcase_10 AC 324 ms
41,648 KB
testcase_11 AC 378 ms
44,312 KB
testcase_12 AC 536 ms
60,008 KB
testcase_13 AC 709 ms
86,472 KB
testcase_14 AC 834 ms
101,480 KB
testcase_15 AC 16 ms
22,144 KB
testcase_16 AC 16 ms
22,272 KB
testcase_17 AC 625 ms
72,748 KB
testcase_18 AC 821 ms
101,604 KB
testcase_19 AC 17 ms
22,016 KB
testcase_20 AC 17 ms
22,144 KB
testcase_21 AC 598 ms
72,764 KB
testcase_22 AC 16 ms
22,016 KB
testcase_23 AC 16 ms
22,144 KB
testcase_24 AC 866 ms
101,608 KB
testcase_25 AC 17 ms
22,144 KB
testcase_26 AC 17 ms
22,144 KB
testcase_27 AC 17 ms
22,144 KB
testcase_28 AC 17 ms
22,144 KB
testcase_29 AC 650 ms
82,668 KB
testcase_30 AC 636 ms
79,176 KB
testcase_31 AC 587 ms
72,692 KB
testcase_32 AC 559 ms
72,756 KB
testcase_33 AC 12 ms
22,272 KB
testcase_34 AC 13 ms
22,272 KB
testcase_35 AC 567 ms
101,184 KB
testcase_36 AC 410 ms
102,056 KB
testcase_37 AC 13 ms
22,016 KB
testcase_38 AC 582 ms
80,288 KB
testcase_39 AC 12 ms
22,144 KB
testcase_40 AC 615 ms
79,552 KB
testcase_41 AC 436 ms
82,560 KB
testcase_42 AC 467 ms
79,168 KB
testcase_43 AC 430 ms
79,040 KB
testcase_44 AC 466 ms
77,284 KB
testcase_45 AC 625 ms
82,684 KB
testcase_46 AC 559 ms
79,172 KB
testcase_47 AC 665 ms
79,052 KB
testcase_48 AC 690 ms
78,764 KB
testcase_49 AC 17 ms
22,016 KB
testcase_50 AC 17 ms
22,144 KB
testcase_51 AC 16 ms
22,144 KB
testcase_52 AC 17 ms
22,144 KB
testcase_53 AC 16 ms
22,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

class UnionFind {
public:
	vector<int> par;

	void init(int sz) {
		par.resize(sz, -1);
	}
	int root(int pos) {
		if (par[pos] == -1) return pos;
		par[pos] = root(par[pos]);
		return par[pos];
	}
	void unite(int u, int v) {
		u = root(u); v = root(v);
		if (u == v) return;
		par[u] = v;
	}
	bool same(int u, int v) {
		if (root(u) == root(v)) return true;
		return false;
	}
};

class RangeMin {
public:
	int size_ = 1;
	vector<int> dat;

	void init(int sz) {
		while (size_ <= sz) size_ *= 2;
		dat.resize(size_ * 2, (1 << 30));
	}
	void update(int pos, int x) {
		pos += size_;
		dat[pos] = x;
		while (pos >= 2) {
			pos >>= 1;
			dat[pos] = min(dat[pos * 2], dat[pos * 2 + 1]);
		}
	}
	int query_(int l, int r, int a, int b, int u) {
		if (l <= a && b <= r) return dat[u];
		if (r <= a || b <= l) return (1 << 30);
		int v1 = query_(l, r, a, (a + b) >> 1, u * 2);
		int v2 = query_(l, r, (a + b) >> 1, b, u * 2 + 1);
		return min(v1, v2);
	}
	int query(int l, int r) {
		return query_(l, r, 0, size_, 1);
	}
};

// 入力ほか
long long mod = 1000000007;
long long N, M, Q;
long long A[1 << 18], B[1 << 18], C[1 << 18];
long long X[1 << 18], Y[1 << 18], Z[1 << 18];
bool mst[1 << 18];

// 最小全域木
UnionFind UF;
long long dist1[1 << 18];
long long dist2[1 << 18];
long long ord[1 << 18];
long long cl[1 << 18];
long long cr[1 << 18], cnts;
long long par[1 << 18][24], pre[1 << 18];
vector<pair<int, int>> G[1 << 18], H[1 << 18];
vector<int> I[1 << 18];

// 最小全域木以外の辺
RangeMin P1, P2;
vector<tuple<int, int, int>> tup1, tup2;
int Important[1 << 18];

// 出力
long long Answer[1 << 18];

void dfs(int pos, long long dep1, long long dep2) {
	cnts++;
	cl[pos] = cnts;
	ord[cnts] = pos;
	dist1[pos] = dep1;
	dist2[pos] = dep2;
	for (int i = 0; i < G[pos].size(); i++) {
		if (cl[G[pos][i].first] >= 1) continue;
		par[G[pos][i].first][0] = pos;
		pre[G[pos][i].first] = G[pos][i].second;
		H[pos].push_back(G[pos][i]);
		dfs(G[pos][i].first, dep1 + 1, (dep2 + C[G[pos][i].second]) % mod);
	}
	cr[pos] = cnts;
}

int prevs(int pos, int x) {
	for (int i = 21; i >= 0; i--) {
		if (x >= (1 << i)) { pos = par[pos][i]; x -= (1 << i); }
	}
	return pos;
}

int lca(int u, int v) {
	if (dist1[u] > dist1[v]) swap(u, v);
	v = prevs(v, dist1[v] - dist1[u]);
	if (u == v) return u;

	for (int i = 21; i >= 0; i--) {
		if (par[u][i] != par[v][i]) {
			u = par[u][i];
			v = par[v][i];
		}
	}
	return par[u][0];
}

long long getdist1(int u, int v) {
	int w = lca(u, v);
	return dist1[u] + dist1[v] - 2 * dist1[w];
}

long long getdist2(int u, int v) {
	int w = lca(u, v);
	return (dist2[u] + dist2[v] - 2LL * dist2[w] + mod * mod) % mod;
}

int main() {
	// Step #1. 入力
	scanf("%lld%lld", &N, &M);
	for (int i = 1; i <= M; i++) scanf("%lld%lld", &A[i], &B[i]);
	scanf("%lld", &Q);
	for (int i = 1; i <= Q; i++) scanf("%lld%lld%lld", &X[i], &Y[i], &Z[i]);
	C[1] = 2LL;
	for (int i = 2; i <= M; i++) C[i] = (2LL * C[i - 1]) % mod;

	// Step #2. 最小全域木を求める
	UF.init(N + 2);
	for (int i = 1; i <= M; i++) {
		if (UF.same(A[i], B[i]) == false) {
			UF.unite(A[i], B[i]);
			mst[i] = true;
			G[A[i]].push_back(make_pair(B[i], i));
			G[B[i]].push_back(make_pair(A[i], i));
		}
	}
	dfs(1, 0, 0);

	// Step #3. LCA を求める
	for (int i = 1; i <= 21; i++) {
		for (int j = 1; j <= N; j++) par[j][i] = par[par[j][i - 1]][i - 1];
	}
	for (int i = 1; i <= M; i++) {
		if (mst[i] == true) continue;
		I[A[i]].push_back(i);
		I[B[i]].push_back(i);
	}

	// Step #4. 重要な辺を求める
	P1.init(N + 2);
	P2.init(N + 2);
	for (int i = 1; i <= M; i++) {
		if (mst[i] == true) {
			int idx = A[i]; if (dist1[A[i]] < dist1[B[i]]) idx = B[i];
			tup1.push_back(make_tuple(cl[idx], -i, cr[idx]));
			tup2.push_back(make_tuple(cr[idx], i, cl[idx]));
		}
		if (mst[i] == false) {
			int dl = cl[A[i]], dr = cl[B[i]]; if (dl > dr) swap(dl, dr);
			tup1.push_back(make_tuple(dl, i, dr));
			tup2.push_back(make_tuple(dr, -i, dl));
		}
	}
	sort(tup1.begin(), tup1.end());
	sort(tup2.begin(), tup2.end());
	reverse(tup2.begin(), tup2.end());
	for (int i = 1; i <= M; i++) Important[i] = (1 << 30);

	for (int i = 0; i < tup1.size(); i++) {
		int p1 = get<0>(tup1[i]), p2 = get<2>(tup1[i]), idx = get<1>(tup1[i]);
		if (idx < 0) {
			int ret = P1.query(p1, p2 + 1);
			Important[-idx] = min(Important[-idx], ret);
		}
		else {
			int rem = P1.dat[p2 + P1.size_];
			P1.update(p2, min(rem, idx));
		}
	}
	for (int i = 0; i < tup2.size(); i++) {
		int p1 = get<0>(tup2[i]), p2 = get<2>(tup2[i]), idx = get<1>(tup2[i]);
		if (idx >= 0) {
			int ret = P2.query(p2, p1 + 1);
			Important[idx] = min(Important[idx], ret);
		}
		else {
			int rem = P2.dat[p2 + P2.size_];
			P2.update(p2, min(rem, -idx));
		}
	}

	// Step #5. 答えを求める
	for (int i = 1; i <= Q; i++) {
		int d1 = getdist1(X[i], A[Z[i]]) + 1 + getdist1(B[Z[i]], Y[i]);
		int d2 = getdist1(X[i], B[Z[i]]) + 1 + getdist1(A[Z[i]], Y[i]);
		int d3 = getdist1(X[i], Y[i]);
		if ((d1 != d3 && d2 != d3) || mst[Z[i]] == false) {
			Answer[i] = getdist2(X[i], Y[i]);
			Answer[i] %= mod;
		}
		else if (Important[Z[i]] == (1 << 30)) {
			Answer[i] = -1;
		}
		else {
			int idx = Important[Z[i]];
			int e1 = getdist1(X[i], A[idx]) + 1 + getdist1(B[idx], Y[i]);
			int e2 = getdist1(X[i], B[idx]) + 1 + getdist1(A[idx], Y[i]);
			if (e1 <= e2) {
				Answer[i] = getdist2(X[i], A[idx]) + C[idx] + getdist2(B[idx], Y[i]);
				Answer[i] %= mod;
			}
			else {
				Answer[i] = getdist2(X[i], B[idx]) + C[idx] + getdist2(A[idx], Y[i]);
				Answer[i] %= mod;
			}
		}
	}

	// Step #6. 出力
	for (int i = 1; i <= Q; i++) {
		printf("%lld\n", Answer[i]);
	}
	return 0;
}
0