結果

問題 No.2337 Equidistant
ユーザー furonfuron
提出日時 2023-06-02 23:31:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,614 bytes
コンパイル時間 1,916 ms
コンパイル使用メモリ 138,200 KB
実行使用メモリ 46,004 KB
最終ジャッジ日時 2023-08-28 06:09:30
合計ジャッジ時間 16,044 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 635 ms
46,004 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

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];
	}
};

void bfs(vvi& g, int s, vi& dist) {
	int n = g.size();
	dist.assign(n, inf);
	queue<int> que;
	que.push(s);
	dist[s] = 0;
	while (!que.empty()) {
		int v = que.front();
		que.pop();
		int d = dist[v];

		for (auto& u : g[v]) {
			if (dist[u] < inf) continue;
			dist[u] = d + 1;
			que.push(u);
		}
	}
}


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 dist;
	bfs(g, 0, dist);
	vi sz(n);
	dfs(0, -1, sz, g);

	LCA lca(g);

	while (q--) {
		int s, t;
		cin >> s >> t;
		s--; t--;
		int d1 = lca.depth[s];
		int d2 = lca.depth[t];
		if (d1!= d2) {
			if ((d1 + d2) % 2 == 0) {
				if (d1 > d2) {
					int mid = d1 - (d1 + d2) / 2;
					int d = mid;
					int j = 0;
					int p = lca.parent[j][s];

					while (d > 0) {
						if (d & 1) p = lca.parent[j][s];
						d = d >> 1;
						j++;
					}
					int p2 = lca.parent[0][p];
					cout << sz[p2] - sz[p] << endl;
				}
				else {
					int mid = d1 - (d1 + d2) / 2;
					int d = mid;
					int j = 0;
					int p = lca.parent[j][t];

					while (d > 0) {
						if (d&  1) p = lca.parent[j][t];
						d = d >> 1;
						j++;
					}
					int p2 = lca.parent[0][p];
					cout << sz[p2] - sz[p] << endl;
				}
			}
			else {
				cout << 0 << endl;
			}
		}
		else {
			int par = lca.get(s, t);
			int p1 = lca.parent[0][s];
			int p2 = lca.parent[0][t];
			if (p1 != par) {
				cout << n - sz[p1] - sz[p2] << endl;
			}
			else {
				cout << n - sz[par] + 1 << endl;
			}

		}
	}


	
	return 0;
}
0