結果
| 問題 | 
                            No.2337 Equidistant
                             | 
                    
| コンテスト | |
| ユーザー | 
                             furon
                         | 
                    
| 提出日時 | 2023-06-02 22:47:18 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,909 bytes | 
| コンパイル時間 | 1,513 ms | 
| コンパイル使用メモリ | 133,676 KB | 
| 最終ジャッジ日時 | 2025-02-13 19:46:56 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 3 WA * 25 | 
ソースコード
#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--;
		if (lca.depth[s] != lca.depth[t]) {
			if ((lca.depth[s] + lca.depth[t]) % 2 == 0) {
				cout << 1 << endl;
			}
			else {
				cout << 0 << endl;
			}
		}
		else {
			int par = lca.get(s, t);
			cout << n - sz[par] + 1 << endl;
		}
	}
	return 0;
}
            
            
            
        
            
furon