結果

問題 No.2337 Equidistant
ユーザー kaikeykaikey
提出日時 2023-06-02 22:03:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,778 bytes
コンパイル時間 2,756 ms
コンパイル使用メモリ 226,172 KB
実行使用メモリ 124,504 KB
最終ジャッジ日時 2024-06-08 23:09:10
合計ジャッジ時間 23,349 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
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 WA -
testcase_22 AC 1,341 ms
84,604 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#include <random>
#include <chrono>
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;++i)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;--i)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define endk '\n'
using namespace std; typedef unsigned long long _ulong; typedef int lint; typedef double ld; typedef pair<lint, lint> plint; typedef pair<ld, ld> pld;
struct fast_ios { fast_ios() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(30); }; } fast_ios_;
template<class T> auto add = [](T a, T b) -> T { return a + b; };
template<class T> auto mul = [](T a, T b) -> T { return a * b; };
template<class T> auto f_max = [](T a, T b) -> T { return max(a, b); };
template<class T> auto f_min = [](T a, T b) -> T { return min(a, b); };
template<class T> using V = vector<T>;
using Vl = V<lint>; using VVl = V<Vl>; using VVVl = V<V<Vl>>;
template< typename T > ostream& operator<<(ostream& os, const vector< T >& v) {
	for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : "");
	return os;
}
template< typename T >istream& operator>>(istream& is, vector< T >& v) {
	for (T& in : v) is >> in;
	return is;
}
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template <class T>
T div_floor(T a, T b) {
	if (b < 0) a *= -1, b *= -1;
	return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
	if (b < 0) a *= -1, b *= -1;
	return a > 0 ? (a - 1) / b + 1 : a / b;
}
template <class F> struct rec {
	F f;
	rec(F&& f_) : f(std::forward<F>(f_)) {}
	template <class... Args> auto operator()(Args &&... args) const {
		return f(*this, std::forward<Args>(args)...);
	}
};
lint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); }
lint digit(lint a) { return (lint)log10(a); }
lint e_dist(plint a, plint b) { return abs(a.first - b.first) * abs(a.first - b.first) + abs(a.second - b.second) * abs(a.second - b.second); }
lint m_dist(plint a, plint b) { return abs(a.first - b.first) + abs(a.second - b.second); }
bool check_overflow(lint a, lint b, lint limit) { if (b == 0) return false; return a > limit / b; } // a * b > c => true
void Worshall_Floyd(VVl& g) { REP(k, SZ(g)) REP(i, SZ(g)) REP(j, SZ(g)) chmin(g[i][j], g[i][k] + g[k][j]); }
const long long MOD1000000007 = 1000000007, MOD998244353 = 998244353, INF = 1e18;
lint dx[8] = { 0, 1, 0, -1, 1, -1, 1, -1 }, dy[8] = { 1, 0, -1, 0, -1, -1, 1, 1 };
bool YN(bool flag) { cout << (flag ? "YES" : "NO") << endk; return flag; } bool yn(bool flag) { cout << (flag ? "Yes" : "No") << endk; return flag; }
struct Edge {
	lint from, to;
	lint cost;
	Edge() {

	}
	Edge(lint u, lint v, lint c) {
		cost = c;
		from = u;
		to = v;
	}
	bool operator<(const Edge& e) const {
		return cost < e.cost;
	}
};
struct WeightedEdge {
	lint to;
	lint cost;
	WeightedEdge(lint v, lint c) {
		to = v;
		cost = c;
	}
	bool operator<(const WeightedEdge& e) const {
		return cost < e.cost;
	}
};
using WeightedGraph = V<V<WeightedEdge>>;
typedef pair<plint, lint> tlint;
typedef pair<ld, ld> pld;
typedef pair<plint, plint> qlint;
typedef pair<lint, string> vstr;
typedef pair<string, string> pstr;
typedef pair<set<lint>, set<lint>> pset;

template< typename G >
struct DoublingLowestCommonAncestor {
	int LOG = 2;
	vector< int > dep;
	const G& g;
	vector< vector< int > > table;

	DoublingLowestCommonAncestor(const G& g) : g(g), dep(g.size()) {
		int curr = 1;
		while (curr < g.size()) {
			curr *= 2;
			LOG++;
		}
		table.assign(LOG, vector< int >(g.size(), 0));
	}

	void dfs(int idx, int par, int d) {
		table[0][idx] = par;
		dep[idx] = d;
		for (auto& to : g[idx]) {
			if (to != par) dfs(to, idx, d + 1);
		}
	}

	void build() {
		dfs(0, -1, 0);
		for (int k = 0; k + 1 < LOG; k++) {
			for (int i = 0; i < table[k].size(); i++) {
				if (table[k][i] == -1) table[k + 1][i] = -1;
				else table[k + 1][i] = table[k][table[k][i]];
			}
		}
	}

	int query(int u, int v) {
		if (dep[u] > dep[v]) swap(u, v);
		for (int i = LOG - 1; i >= 0; i--) {
			if (((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
		}
		if (u == v) return u;
		for (int i = LOG - 1; i >= 0; i--) {
			if (table[i][u] != table[i][v]) {
				u = table[i][u];
				v = table[i][v];
			}
		}
		return table[0][u];
	}

	int _query(int k, int64_t t)
	{
		for (int i = LOG - 1; i >= 0; i--) {
			if ((t >> i) & 1) k = table[i][k];
			if (k == -1) return 0;
		}
		return k;
	}
};

struct Rerooting {
	VVl dp;
	VVl to;
	Rerooting(lint sz) {
		dp.resize(sz);
		to.resize(sz);
	}

	void add_edge(int u, int v) {
		to[u].push_back(v);
		to[v].push_back(u);
	}

	void build() {
		dfs(0);
		dfs2(0, 0);
	}

	lint dfs(lint curr, lint prv = -1) {
		lint sum = 1;
		lint deg = SZ(to[curr]);
		dp[curr] = Vl(deg, 0);
		REP(i, deg) {
			lint nxt = to[curr][i];
			if (nxt == prv) continue;
			dp[curr][i] = dfs(nxt, curr);
			sum += dp[curr][i];
		}
		return sum;
	}

	void dfs2(lint curr, lint score, lint prv = -1) {
		lint deg = SZ(to[curr]);
		REP(i, deg) {
			if (to[curr][i] == prv) dp[curr][i] = score;
		}
		Vl dp_l(deg + 1, 0), dp_r(deg + 1, 0);
		REP(i, deg) {
			dp_l[i + 1] = dp_l[i] + dp[curr][i];
		}
		IREP(i, deg) {
			dp_r[i] = dp_r[i + 1] + dp[curr][i];
		}
		REP(i, deg) {
			lint nxt = to[curr][i];
			if (nxt == prv) continue;
			dfs2(nxt, dp_l[i] + dp_r[i + 1] + 1, curr);
		}
	}
};

int main() {
	lint N, Q;
	cin >> N >> Q;
	VVl to(N);
	Rerooting dp(N);
	V<map<lint, lint>> fdx(N);
	REP(i, N - 1) {
		lint u, v;
		cin >> u >> v; u--; v--;
		to[u].push_back(v);
		to[v].push_back(u);
		fdx[u][v] = SZ(dp.dp[u]);
		fdx[v][u] = SZ(dp.dp[v]);
		dp.add_edge(u, v);
	}
	DoublingLowestCommonAncestor lca(to);
	lca.build();
	dp.build();

	auto getd = [&](lint u, lint v) {
		return lca.dep[u] + lca.dep[v] - 2 * lca.dep[lca.query(u, v)];
	};

	REP(i, Q) {
		lint s, t;
		cin >> s >> t; s--; t--;
		lint d = getd(s, t);
		if (d % 2 == 1) {
			cout << 0 << endk;
			continue;
		}
		d /= 2;
		lint mid = -1, _lca = lca.query(s, t);
		lint sl = lca._query(s, d), tl = lca._query(t, d);
		if (sl != _lca) mid = sl;
		if (tl != _lca) mid = tl;

		lint sn, tn;
		if (sl == tl) {
			sn = lca._query(s, d - 1);
			tn = lca._query(t, d - 1);
			mid = _lca;
		}
		else {
			if (sl != _lca) {
				sn = lca._query(s, d - 1);
				tn = lca._query(s, d + 1);
			}
			else {
				tn = lca._query(t, d - 1);
				sn = lca._query(t, d + 1);
			}
		}
		lint ans = N;
		ans -= dp.dp[mid][fdx[mid][sn]];
		ans -= dp.dp[mid][fdx[mid][tn]];
		cout << ans << endk;
	}
}
0