結果

問題 No.1326 ふたりのDominator
ユーザー QCFiumQCFium
提出日時 2020-12-03 01:16:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 1,994 bytes
コンパイル時間 2,059 ms
コンパイル使用メモリ 180,420 KB
実行使用メモリ 15,280 KB
最終ジャッジ日時 2023-10-24 10:26:09
合計ジャッジ時間 4,745 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 92 ms
11,264 KB
testcase_13 AC 94 ms
11,264 KB
testcase_14 AC 96 ms
11,272 KB
testcase_15 AC 103 ms
11,308 KB
testcase_16 AC 111 ms
11,508 KB
testcase_17 AC 116 ms
12,396 KB
testcase_18 AC 133 ms
14,072 KB
testcase_19 AC 111 ms
14,652 KB
testcase_20 AC 109 ms
13,788 KB
testcase_21 AC 128 ms
13,788 KB
testcase_22 AC 138 ms
15,280 KB
testcase_23 AC 80 ms
11,404 KB
testcase_24 AC 91 ms
11,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

std::vector<std::vector<int> > hen;

std::vector<int> ord, out;
std::vector<int> tour;
int ord_cnt = 0;
std::vector<int> low;
std::vector<int> depth;
std::vector<std::vector<int> > parent;

void dfs0(int i, int prev) {
	parent[0][i] = prev;
	ord[i] = ord_cnt++;
	tour.push_back(i);
	low[i] = ord[i];
	for (auto j : hen[i]) {
		if (j == prev) continue;
		if (ord[j] == -1) {
			depth[j] = depth[i] + 1;
			dfs0(j, i);
			low[i] = std::min(low[i], low[j]);
		} else low[i] = std::min(low[i], ord[j]);
	}
	out[i] = ord_cnt;
}
std::vector<int> sum;
void dfs1(int i) {
	sum[i] += parent[0][i] != -1 && low[i] >= ord[parent[0][i]];
	for (auto j : hen[i]) if (depth[j] == depth[i] + 1) {
		sum[j] = sum[i];
		dfs1(j);
	}
}

int main() {
	int n = ri();
	int m = ri();
	hen.resize(n);
	for (int i = 0; i < m; i++) {
		int a = ri() - 1;
		int b = ri() - 1;
		hen[a].push_back(b);
		hen[b].push_back(a);
	}
	
	ord.resize(n, -1);
	out.resize(n, -1);
	low.resize(n);
	depth.resize(n);
	parent.resize(20, std::vector<int>(n, -1));
	dfs0(0, -1);
	for (int i = 1; i < 20; i++) for (int j = 0; j < n; j++)
		parent[i][j] = parent[i - 1][j] == -1 ? -1 : parent[i - 1][parent[i - 1][j]];
	
	sum.resize(n);
	dfs1(0);
	
	int q = ri();
	for (int i = 0; i < q; i++) {
		int a = ri() - 1;
		int b = ri() - 1;
		if (depth[a] < depth[b]) std::swap(a, b);
		int res;
		if (a == b) res = 0;
		else if (ord[a] >= ord[b] && out[a] <= out[b]) { // a is a descendant of b
			res = sum[a];
			for (int k = 20; k--; ) if ((depth[a] - depth[b] - 1) >> k & 1) a = parent[k][a];
			res -= sum[a];
		} else {
			res = sum[a] + sum[b];
			for (int k = 20; k--; ) if ((depth[a] - depth[b]) >> k & 1) a = parent[k][a];
			for (int k = 20; k--; ) if (parent[k][a] != parent[k][b]) a = parent[k][a], b = parent[k][b];
			res -= sum[a] + sum[b];
			int x = ord[parent[0][a]];
			if (low[a] >= x || low[b] >= x) res++;
		}
		printf("%d\n", res);
	}
	return 0;
}
0