結果

問題 No.2337 Equidistant
ユーザー shobonvipshobonvip
提出日時 2023-06-03 01:51:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 986 ms / 4,000 ms
コード長 2,535 bytes
コンパイル時間 4,100 ms
コンパイル使用メモリ 262,776 KB
実行使用メモリ 43,820 KB
最終ジャッジ日時 2023-08-28 07:23:47
合計ジャッジ時間 20,153 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 695 ms
40,056 KB
testcase_12 AC 705 ms
39,976 KB
testcase_13 AC 705 ms
39,932 KB
testcase_14 AC 700 ms
39,924 KB
testcase_15 AC 692 ms
39,976 KB
testcase_16 AC 680 ms
40,088 KB
testcase_17 AC 693 ms
39,960 KB
testcase_18 AC 688 ms
39,920 KB
testcase_19 AC 721 ms
39,920 KB
testcase_20 AC 760 ms
39,988 KB
testcase_21 AC 814 ms
41,716 KB
testcase_22 AC 691 ms
43,820 KB
testcase_23 AC 690 ms
40,760 KB
testcase_24 AC 944 ms
41,420 KB
testcase_25 AC 719 ms
40,612 KB
testcase_26 AC 986 ms
41,352 KB
testcase_27 AC 745 ms
40,752 KB
testcase_28 AC 743 ms
40,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;

typedef modint998244353 mint;
typedef long long ll;

int main(){
	int n, q; cin >> n >> q;
	vector<vector<int>> ikeru(n, vector<int>(0));
	
	for (int i=0; i<n-1; i++){
		int a, b; cin >> a >> b;
		a--; b--;
		ikeru[a].push_back(b);
		ikeru[b].push_back(a);
	}

	vector<vector<int>> db(30, vector<int>(n, -1));
	vector<int> mada = {~0, 0};

	vector<int> tansaku(n);
	tansaku[0] = 1;
	vector<int> siz(n);
	vector<int> depth(n);

	while(!mada.empty()){
		int i = mada.back();
		mada.pop_back();
		if (i >= 0){
			for (int j: ikeru[i]){
				if (tansaku[j] == 0){
					tansaku[j] = 1;
					db[0][j] = i;
					depth[j] = depth[i] + 1;
					mada.push_back(~j);
					mada.push_back(j);
				}
			}
		}else{
			i = ~i;
			int tmp = 1;
			for (int j: ikeru[i]){
				if (tansaku[j] == 2){
					tmp += siz[j];
				}
			}
			tansaku[i] = 2;
			siz[i] = tmp;
		}
	}

	for (int i=0; i<29; i++){
		for (int j=0; j<n; j++){
			if (db[i][j] == -1) continue;
			db[i+1][j] = db[i][db[i][j]];
		}
	}

	for (int i=0; i<q; i++){
		int s, t;
		cin >> s >> t;
		s--; t--;

		// lca
		int x = s, y = t;
		if (depth[x] < depth[y]){
			swap(x, y);
		}
		int dist = 0;
		if (depth[x] > depth[y]){
			int k = depth[x] - depth[y];
			for (int j=0; j<30; j++){
				if (k >> j & 1){
					x = db[j][x];
					dist += 1 << j;
				}
			}
		}
		if (x != y){
			for (int j=29; j>=0; j--){
				if (db[j][x] == -1) continue;
				if (db[j][x] != db[j][y]){
					x = db[j][x];
					y = db[j][y];
					dist += 1 << j+1;
				} 
			}
		}
		if (x != y){
			x = db[0][x];
			dist += 2;
		}

		if (dist % 2 == 1){
			cout << 0 << "\n";
		}else{
			if (x == s || x == t){
				if (x == t) swap(s, t);
				int w = t, g = dist / 2 - 1;
				for (int j=0; j<30; j++){
					if (g >> j & 1){
						w = db[j][w];
					}
				}
				int ans = 0;
				ans -= siz[w];
				w = db[0][w];
				ans += siz[w];
				cout << ans << "\n";
			}else if(depth[s] != depth[t]){
				if (depth[s] > depth[t]){
					swap(s, t);
				}
				int w = t, g = dist / 2 - 1;
				for (int j=0; j<30; j++){
					if (g >> j & 1){
						w = db[j][w];
					}
				}
				int ans = 0;
				ans -= siz[w];
				w = db[0][w];
				ans += siz[w];
				cout << ans << "\n";
			}else{
				int v = t, w = s;
				int g = dist / 2 - 1;
				for (int j=0; j<30; j++){
					if (g >> j & 1){
						w = db[j][w];
						v = db[j][v];
					}
				}
				int ans = 0;
				ans = n;
				ans -= siz[w];
				ans -= siz[v];
				cout << ans << "\n";
			}
		}

	}

}
0