結果

問題 No.2337 Equidistant
ユーザー furonfuron
提出日時 2023-06-03 15:24:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,174 ms / 4,000 ms
コード長 3,042 bytes
コンパイル時間 1,381 ms
コンパイル使用メモリ 133,832 KB
実行使用メモリ 45,152 KB
最終ジャッジ日時 2023-08-28 07:56:40
合計ジャッジ時間 18,599 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 7 ms
4,376 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 765 ms
29,880 KB
testcase_12 AC 758 ms
29,844 KB
testcase_13 AC 772 ms
29,844 KB
testcase_14 AC 785 ms
30,100 KB
testcase_15 AC 764 ms
30,024 KB
testcase_16 AC 782 ms
29,812 KB
testcase_17 AC 779 ms
29,884 KB
testcase_18 AC 786 ms
29,868 KB
testcase_19 AC 794 ms
29,868 KB
testcase_20 AC 784 ms
29,876 KB
testcase_21 AC 922 ms
45,152 KB
testcase_22 AC 687 ms
30,404 KB
testcase_23 AC 712 ms
30,672 KB
testcase_24 AC 1,074 ms
40,208 KB
testcase_25 AC 742 ms
30,620 KB
testcase_26 AC 1,174 ms
39,920 KB
testcase_27 AC 735 ms
30,336 KB
testcase_28 AC 740 ms
30,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#include <bitset>
#include <set>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <random>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
const int inf = (1 << 30) - 1;
const ll INF = 1LL << 60;
//const int MOD = 1000000007;
const int MOD = 998244353;

using Graph = vector<vector<int> >;
struct LCA {
	vector<vector<int> > parent; // parent[d][v] := 2^d-th parent of v
	vector<int> depth;
	LCA() {}
	LCA(const Graph& G, int r = 0) { init(G, r); }
	void init(const Graph& G, int r = 0) {
		int V = (int)G.size();
		int h = 1;
		while ((1 << h) < V) ++h;
		parent.assign(h, vector<int>(V, -1));
		depth.assign(V, -1);
		dfs(G, r, -1, 0);
		for (int i = 0; i + 1 < (int)parent.size(); ++i)
			for (int v = 0; v < V; ++v)
				if (parent[i][v] != -1)
					parent[i + 1][v] = parent[i][parent[i][v]];
	}
	void dfs(const Graph& G, int v, int p, int d) {
		parent[0][v] = p;
		depth[v] = d;
		for (auto e : G[v]) if (e != p) dfs(G, e, v, d + 1);
	}
	int get(int u, int v) {
		if (depth[u] > depth[v]) swap(u, v);
		for (int i = 0; i < (int)parent.size(); ++i)
			if ((depth[v] - depth[u]) & (1 << i))
				v = parent[i][v];
		if (u == v) return u;
		for (int i = (int)parent.size() - 1; i >= 0; --i) {
			if (parent[i][u] != parent[i][v]) {
				u = parent[i][u];
				v = parent[i][v];
			}
		}
		return parent[0][u];
	}
	// 頂点uから深さd遡った頂点の番号
	int la(int u, int d) {
		int j = 0;
		int p = u;

		while (d > 0) {
			if (d & 1) p = parent[j][p];
			d = (d >> 1);
			j++;
		}
		return p;
	}
};


void dfs(int v, int p, vi& sz, const vvi& g) {

	for (auto u : g[v]) {
		if (u == p) continue;
		dfs(u, v, sz, g);
	}

	sz[v] = 1;
	for (auto u : g[v]) {
		sz[v] += sz[u];
	}
}



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

	vi sz(n);
	dfs(0, -1, sz, g);

	LCA lca(g);

	while (q--) {
		int s, t;
		cin >> s >> t;
		s--; t--;
		int par = lca.get(s, t);
		int d1 = lca.depth[s] - lca.depth[par];
		int d2 = lca.depth[t] - lca.depth[par];

		if (d1 != d2) {
			if ((d1 + d2) % 2 == 0) {
				int mid = (d1 + d2) / 2;
				int v;
				if (d1 > d2) v = s;
				else v = t;
				
				int pa = lca.la(v, mid);
				int ch = lca.la(v, mid - 1);

				cout << sz[pa] - sz[ch] << endl;
			}
			else {
				cout << 0 << endl;
			}
		}
		else {
			int p1 = lca.la(s, d1 - 1);
			int p2 = lca.la(t, d2 - 1);
			cout << n - sz[p1] - sz[p2] << endl;

		}
	}

	return 0;
}
0