結果

問題 No.2337 Equidistant
ユーザー FF256grhyFF256grhy
提出日時 2023-10-14 11:15:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,129 ms / 4,000 ms
コード長 8,862 bytes
コンパイル時間 2,471 ms
コンパイル使用メモリ 213,384 KB
実行使用メモリ 49,824 KB
最終ジャッジ日時 2024-09-16 06:06:48
合計ジャッジ時間 18,307 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 7 ms
5,376 KB
testcase_07 AC 7 ms
5,376 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 7 ms
5,376 KB
testcase_10 AC 8 ms
5,376 KB
testcase_11 AC 740 ms
41,856 KB
testcase_12 AC 683 ms
41,676 KB
testcase_13 AC 704 ms
41,728 KB
testcase_14 AC 703 ms
41,680 KB
testcase_15 AC 766 ms
41,856 KB
testcase_16 AC 758 ms
41,676 KB
testcase_17 AC 722 ms
41,720 KB
testcase_18 AC 741 ms
41,840 KB
testcase_19 AC 719 ms
41,856 KB
testcase_20 AC 701 ms
41,720 KB
testcase_21 AC 742 ms
49,824 KB
testcase_22 AC 660 ms
42,328 KB
testcase_23 AC 694 ms
42,368 KB
testcase_24 AC 899 ms
47,080 KB
testcase_25 AC 669 ms
42,368 KB
testcase_26 AC 1,129 ms
46,976 KB
testcase_27 AC 742 ms
42,368 KB
testcase_28 AC 726 ms
42,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define incII(i, l, r) for(decay_t<decltype(r)> i = (l)    ; i <= (r); i++)
#define incIX(i, l, r) for(decay_t<decltype(r)> i = (l)    ; i <  (r); i++)
#define incXI(i, l, r) for(decay_t<decltype(r)> i = (l) + 1; i <= (r); i++)
#define incXX(i, l, r) for(decay_t<decltype(r)> i = (l) + 1; i <  (r); i++)
#define decII(i, l, r) for(decay_t<decltype(r)> i = (r)    ; i >= (l); i--)
#define decIX(i, l, r) for(decay_t<decltype(r)> i = (r) - 1; i >= (l); i--)
#define decXI(i, l, r) for(decay_t<decltype(r)> i = (r)    ; i >  (l); i--)
#define decXX(i, l, r) for(decay_t<decltype(r)> i = (r) - 1; i >  (l); i--)
#define inc(i, n)  incIX(i, 0, n)
#define dec(i, n)  decIX(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
auto inII = [](auto a, auto l, auto r) -> bool { return (l <= a && a <= r); };
auto inIX = [](auto a, auto l, auto r) -> bool { return (l <= a && a <  r); };
auto inXI = [](auto a, auto l, auto r) -> bool { return (l <  a && a <= r); };
auto inXX = [](auto a, auto l, auto r) -> bool { return (l <  a && a <  r); };
auto setmin   = [](auto & a, auto b) -> bool { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) -> bool { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) -> bool { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) -> bool { return (b >= a ? a = b, true : false); };
using LL = long long int;
using LD = long double;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(a) begin(a), end(a)
#define RALL(a) rbegin(a), rend(a)
#define RV(a) reverse(ALL(a))
#define ST(a) sort(ALL(a))
#define RST(a) sort(RALL(a))
#define SC static_cast
#define SI(a) SC<int>(a.size())
#define SL(a) SC<LL>(a.size())
#define RF(e, ...) for(auto && e: __VA_ARGS__)
#define SF(a, ...) for(auto && [__VA_ARGS__]: a)
#define until(...) while(not(__VA_ARGS__))
#define if_not(...) if(not(__VA_ARGS__))
#define ef else if
#define UR assert(false)
template<typename A>             istream & operator>>(istream & is, vector<A>          & v);
template<typename A>             ostream & operator<<(ostream & os, vector<A>    const & v);
template<typename A, size_t N>   istream & operator>>(istream & is, array<A, N>        & a);
template<typename A, size_t N>   ostream & operator<<(ostream & os, array<A, N>  const & a);
template<typename A, typename B> istream & operator>>(istream & is, pair<A, B>         & p);
template<typename A, typename B> ostream & operator<<(ostream & os, pair<A, B>   const & p);
template<typename ... A>         istream & operator>>(istream & is, tuple<A ...>       & t);
template<typename ... A>         ostream & operator<<(ostream & os, tuple<A ...> const & t);
template<int I = 0, typename T> void tin_ (istream & is, T       & t) { if constexpr(I < tuple_size<T>::value) { is >> get<I>(t); tin_<I + 1>(is, t); } }
template<int I = 0, typename T> void tout_(ostream & os, T const & t) { if constexpr(I < tuple_size<T>::value) { if(I != 0) { os << " "; } os << get<I>(t); tout_<I + 1>(os, t); } }
template<typename A>             istream & operator>>(istream & is, vector<A>          & v) { for(auto && e: v) { is >> e; } return is; }
template<typename A>             ostream & operator<<(ostream & os, vector<A>    const & v) { string ss; for(auto && e: v) { os << ss << e; ss = " "; } return os; }
template<typename A, size_t N>   istream & operator>>(istream & is, array<A, N>        & a) { for(auto && e: a) { is >> e; } return is; }
template<typename A, size_t N>   ostream & operator<<(ostream & os, array<A, N>  const & a) { string ss; for(auto && e: a) { os << ss << e; ss = " "; } return os; }
template<typename A, typename B> istream & operator>>(istream & is, pair<A, B>         & p) { return is >> p.first >> p.second; }
template<typename A, typename B> ostream & operator<<(ostream & os, pair<A, B>   const & p) { return os << p.first << " " << p.second; }
template<typename ... A>         istream & operator>>(istream & is, tuple<A ...>       & t) { tin_ (is, t); return is; }
template<typename ... A>         ostream & operator<<(ostream & os, tuple<A ...> const & t) { tout_(os, t); return os; }
auto * IS = & cin;
auto * OS = & cout;
template<typename ... A> void in(A & ... a) { (* IS >> ... >> a); }
#define IN(...) __VA_ARGS__; in(__VA_ARGS__)
template<typename A                > auto make_v(A a) { return a; }
template<typename A, typename ... M> auto make_v(A a, int n, M ... m) { return vector(n, make_v(a, m ...)); }
template<typename A, typename ... N> auto read_v(N ... n) { auto a = make_v(A { }, n ...); in(a); return a; }
void                                      out_([[maybe_unused]] string const & s) { }
template<typename A                > void out_([[maybe_unused]] string const & s, A const & a) { * OS << a; }
template<typename A, typename ... B> void out_(                 string const & s, A const & a, B const & ... b) { * OS << a << s; out_(s, b ...); }
auto outF  = [](string const & s, string const & t, auto const & ... a) { out_(s, a ...); * OS << t << flush; };
auto outN  = [](auto const & ... a) { outF(""  , ""  , a ...); };
auto outS  = [](auto const & ... a) { outF(" " , " " , a ...); };
auto outL  = [](auto const & ... a) { outF("\n", "\n", a ...); };
auto outSN = [](auto const & ... a) { outF(" " , ""  , a ...); };
auto outNL = [](auto const & ... a) { outF(""  , "\n", a ...); };
auto outSL = [](auto const & ... a) { outF(" " , "\n", a ...); };
auto out   = outSL;
template<typename A                > void disp_(A const & a) { * OS << a; }
template<typename A, typename ... T> void disp_(A const & a, string const & s, T const & ... t) { string ss; for(auto && e: a) { * OS << ss; ss = s; disp_(e, t ...); } }
auto dispI = [](auto const & a, auto const & s, auto const & ... t) { disp_(a, t ...); * OS << s << flush; };
auto dispT = [](auto const & a, auto const & s, auto const & ... t) { for(auto && e: a) { disp_(e, t ...); * OS << s; } * OS << flush; };
auto dispL = [](auto const & a,                 auto const & ... t) { dispT(a, "\n", t ...); };
struct OS_init { OS_init() { * OS << boolalpha << fixed << setprecision(20); } } os_init_;

// ---- ----

#define bit(b, i) (((b) >> (i)) & 1)
#define PC __builtin_popcountll
#define BL(a) (a ? 64 - __builtin_clzll(a) : 0)

class Forest {
private:
	int n, B;
	vector<vector<int>> g, p;
	vector<int> h, f;
	vector<int> s; //
	bool ok;
	void dfs(int v, int pp, int hh) {
		if(f[v] != 0) { return; }
		f[v] = 1;
		p[v][0] = pp;
		h[v] = hh;
		assert(s[v] == 0);
		s[v] = 1;
		RF(e, g[v]) { dfs(e, v, hh + 1); }
		if(v != pp) { s[pp] += s[v]; }
	}
public:
	Forest(int nn = 0) { init(nn); }
	void init(int nn) {
		n = nn;
		B = 0;
		while((1 << B) < n) { B++; }
		g = vector<vector<int>>(n);
		p = vector<vector<int>>(n, vector<int>(B + 1));
		h = vector<int>(n);
		f = vector<int>(n);
		s = vector<int>(n); //
		ok = false;
	}
	void add(int a, int b) {
		assert(! ok);
		assert(inIX(a, 0, n));
		assert(inIX(b, 0, n));
		g[a].PB(b);
		g[b].PB(a);
	}
	void calc(const vector<int> & root = { }) {
		assert(! ok);
		RF(e, root) { dfs(e, e, 0); }
		inc(i, n)   { dfs(i, i, 0); }
		inc(k, B) {
		inc(i, n) {
			p[i][k + 1] = p[p[i][k]][k];
		}
		}
		ok = true;
	}
	bool same(int a, int b) {
		assert(ok);
		assert(inIX(a, 0, n));
		assert(inIX(b, 0, n));
		return (p[a][B] == p[b][B]);
	}
	int lca(int a, int b) {
		if(! same(a, b)) { return -1; }
		int d = abs(h[a] - h[b]);
		if(h[a] > h[b]) { a = up(a, d); }
		if(h[a] < h[b]) { b = up(b, d); }
		assert(h[a] == h[b]);
		if(a == b) { return a; }
		dec(k, B) {
			if(p[a][k] != p[b][k]) {
				a = p[a][k];
				b = p[b][k];
			}
		}
		assert(a != b && p[a][0] == p[b][0]);
		return p[a][0];
	}
	int dist(int a, int b) {
		if(! same(a, b)) { return -1; }
		return h[a] + h[b] - 2 * h[lca(a, b)];
	}
	int up(int a, int x) {
		assert(ok);
		assert(inIX(a, 0, n));
		if(x > h[a]) { return -1; }
		inc(k, B) { if(bit(x, k) == 1) { a = p[a][k]; } }
		return a;
	}
	const vector<vector<int>> & get_g() { return g; }
	const vector<vector<int>> & get_p() { return p; }
	const vector<int>         & get_h() { return h; }
	const vector<int>         & get_s() { return s; } //
};

// ----

int main() {
	int IN(n, q);
	Forest g(n);
	inc(i, n - 1) {
		int IN(a, b);
		a--; b--;
		g.add(a, b);
	}
	g.calc();
	auto d = g.get_h();
	auto s = g.get_s();
	inc(qq, q) {
		int IN(a, b);
		a--; b--;
		if(d[a] % 2 != d[b] % 2) { out(0); continue; }
		int c = g.lca(a, b);
		if(d[a] == d[b]) {
			int x = d[a] - d[c] - 1;
			out(n - s[g.up(a, x)] - s[g.up(b, x)]);
		} else {
			if(d[a] < d[b]) { swap(a, b); }
			int x = ((d[a] - d[c]) + (d[b] - d[c])) / 2;
			out(s[g.up(a, x)] - s[g.up(a, x - 1)]);
		}
	}
}
0