結果

問題 No.2337 Equidistant
ユーザー kwm_tkwm_t
提出日時 2023-06-03 12:33:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 977 ms / 4,000 ms
コード長 4,238 bytes
コンパイル時間 2,181 ms
コンパイル使用メモリ 208,964 KB
実行使用メモリ 43,296 KB
最終ジャッジ日時 2023-08-28 07:46:25
合計ジャッジ時間 12,906 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 417 ms
35,328 KB
testcase_12 AC 412 ms
35,388 KB
testcase_13 AC 411 ms
35,344 KB
testcase_14 AC 422 ms
35,364 KB
testcase_15 AC 402 ms
35,380 KB
testcase_16 AC 401 ms
35,276 KB
testcase_17 AC 390 ms
35,384 KB
testcase_18 AC 384 ms
35,312 KB
testcase_19 AC 377 ms
35,396 KB
testcase_20 AC 394 ms
35,412 KB
testcase_21 AC 549 ms
43,296 KB
testcase_22 AC 357 ms
35,880 KB
testcase_23 AC 338 ms
35,904 KB
testcase_24 AC 724 ms
40,640 KB
testcase_25 AC 376 ms
35,884 KB
testcase_26 AC 977 ms
40,764 KB
testcase_27 AC 416 ms
35,832 KB
testcase_28 AC 411 ms
35,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
class Tree {
public:
	Tree(int n, int root) : n(n), root(root) {
		edge.resize(n);
		for (int i = 0; i < MAXLOGV; i++) parent[i].resize(n);
		depth.resize(n);
		sz.resize(n);
	}
	// uとvをつなぐ
	// lcaを求めることが主目的なので無向グラフとしている
	void unite(int u, int v) {
		edge[u].push_back(v);
		edge[v].push_back(u);
	}
	// initする
	// コンストラクタだけじゃなくてこれも呼ばないとlcaが求められないぞ
	void init() {
		dfs(root, -1, 0);
		for (int k = 0; k + 1 < MAXLOGV; k++) {
			for (int v = 0; v < n; v++) {
				if (parent[k][v] < 0) parent[k + 1][v] = -1;
				else parent[k + 1][v] = parent[k][parent[k][v]];
			}
		}
	}
	// uとvのlcaを求める
	int lca(int u, int v) const {
		if (depth[u] > depth[v]) swap(u, v);
		for (int k = 0; k < MAXLOGV; k++) {
			if ((depth[v] - depth[u]) >> k & 1) {
				v = parent[k][v];
			}
		}
		if (u == v) return u;
		for (int k = MAXLOGV - 1; k >= 0; k--) {
			if (parent[k][u] != parent[k][v]) {
				u = parent[k][u];
				v = parent[k][v];
			}
		}
		return parent[0][u];
	}
	// uのn個親を求める
	int pare(int v, int n) {
		if (depth[v] < n)return -1;
		n = min(n, depth[v]);
		int idx = MAXLOGV;
		while (n) {
			for (int i = idx - 1; i >= 0; --i) {
				if (n < (1 << i))continue;
				if (-1 == parent[i][v])continue;
				n -= (1 << i);
				v = parent[i][v];
				idx = i;
				break;
			}
		}
		return v;
	}
	// uからvに向かってd進んだ頂点を返す
	int JumpOnTree(int u, int v, int d) {
		if (0 == d)return u;
		int distuv = dist(u, v);
		if (distuv < d)return -1;
		int l = lca(u, v);
		if (l == u)return pare(v, distuv - d);
		if (l == v)return pare(u, d);
		int distlu = dist(l, u);
		if (distlu >= d)return pare(u, d);
		return pare(v, distuv - d);
	}
	// uとvの距離を求める
	// edgeを定義しないといけない時はこれじゃダメ
	int dist(int u, int v) const {
		int p = lca(u, v);
		return (depth[u] - depth[p]) + (depth[v] - depth[p]);
	}
	//頂点wが頂点u,vのパス上に存在するか
	bool on_path(int u, int v, int w) {
		return (dist(u, w) + dist(v, w) == dist(u, v));
	}
	int dfs(int v, int p, int d) {
		int ret = 1;
		parent[0][v] = p;
		depth[v] = d;
		for (int next : edge[v]) {
			if (next == p) continue;
			auto get = dfs(next, v, d + 1);
			ret += get;

		}
		sz[v] = ret;
		return ret;
	}
	static const int MAXLOGV = 25;
	// グラフの隣接リスト表現
	vector<vector<int>>edge;
	// 頂点の数
	int n;
	// 根ノードの番号
	int root;
	// 親ノード
	vector<int> parent[MAXLOGV];
	// 根からの深さ
	vector<int> depth;
	vector<int>sz;
};

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, q; cin >> n >> q;
	Tree tree(n, 0);
	rep(i, n - 1) {
		int a, b; cin >> a >> b;
		a--, b--;
		tree.unite(a, b);
	}
	tree.init();
	while (q--) {
		int s, t; cin >> s >> t;
		s--, t--;
		int d = tree.dist(s, t);
		int l = tree.lca(s, t);
		if (1 == d % 2) {
			cout << 0 << endl;
		}
		else {
			int c = tree.JumpOnTree(s, t, d / 2);
			int l = tree.lca(s, t);
			if (l == c) {
				int ns = tree.pare(s, d / 2 - 1);
				int nt = tree.pare(t, d / 2 - 1);
				int ans = n - tree.sz[ns] - tree.sz[nt];
				cout << ans << endl;
			}
			else {
				if (c != tree.lca(s, c)) swap(s, t);
				int ns = tree.pare(s, d / 2 - 1);
				int ans = tree.sz[c] - tree.sz[ns];
				cout << ans << endl;
			}
		}
	}
	return 0;
}
0