結果

問題 No.2337 Equidistant
ユーザー leaf_1415leaf_1415
提出日時 2023-06-02 22:35:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,990 ms / 4,000 ms
コード長 11,580 bytes
コンパイル時間 2,423 ms
コンパイル使用メモリ 129,620 KB
実行使用メモリ 66,304 KB
最終ジャッジ日時 2024-06-09 00:12:51
合計ジャッジ時間 18,143 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,192 KB
testcase_01 AC 6 ms
8,064 KB
testcase_02 AC 6 ms
8,064 KB
testcase_03 AC 5 ms
8,192 KB
testcase_04 AC 5 ms
8,064 KB
testcase_05 AC 5 ms
8,064 KB
testcase_06 AC 7 ms
8,448 KB
testcase_07 AC 8 ms
8,448 KB
testcase_08 AC 8 ms
8,448 KB
testcase_09 AC 8 ms
8,448 KB
testcase_10 AC 7 ms
8,448 KB
testcase_11 AC 583 ms
54,400 KB
testcase_12 AC 598 ms
54,400 KB
testcase_13 AC 585 ms
54,528 KB
testcase_14 AC 585 ms
54,400 KB
testcase_15 AC 599 ms
54,528 KB
testcase_16 AC 591 ms
54,400 KB
testcase_17 AC 606 ms
54,400 KB
testcase_18 AC 612 ms
54,400 KB
testcase_19 AC 589 ms
54,400 KB
testcase_20 AC 598 ms
54,400 KB
testcase_21 AC 1,115 ms
66,304 KB
testcase_22 AC 497 ms
53,904 KB
testcase_23 AC 504 ms
55,808 KB
testcase_24 AC 1,484 ms
62,336 KB
testcase_25 AC 520 ms
55,808 KB
testcase_26 AC 1,990 ms
62,080 KB
testcase_27 AC 540 ms
55,808 KB
testcase_28 AC 545 ms
55,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#include <array>
#include <unordered_set>
#include <unordered_map>
#define rep(x, s, t) for(ll x = (s); (x) <= (t); (x)++)
#define per(x, s, t) for(ll x = (s); (x) >= (t); (x)--)
#define reps(x, s) for(ll x = 0; (x) < (ll)(s).size(); (x)++)
#define pers(x, s) for(ll x = (ll)(s).size()-1; (x) >= 0; (x)--)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define sz(x) ((ll)(x).size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define outl(...) dump_func(__VA_ARGS__)
#define outf(x) cout << fixed << setprecision(16) << (x) << endl
#define pb push_back
#define fi first
#define se second
#define inf 2e18
#define eps 1e-9
const double PI = 3.1415926535897932384626433;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

struct edge{
	ll to, cost;
	edge(){}
	edge(ll a, ll b){ to = a, cost = b;}
};
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

const int mod = 998244353;
//const int mod = 1000000007;

struct mint{
	int x;
	mint(ll y = 0){if(y < 0 || y >= mod) y = (y%mod+mod)%mod; x = y;}
	mint(const mint &ope) {x = ope.x;}
	mint operator-(){return mint(-x);}
	mint operator+(const mint &ope){return mint(x) += ope;}
	mint operator-(const mint &ope){return mint(x) -= ope;}
	mint operator*(const mint &ope){return mint(x) *= ope;}
	mint operator/(const mint &ope){return mint(x) /= ope;}
	mint& operator+=(const mint &ope){x += ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator-=(const mint &ope){x += mod - ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator*=(const mint &ope){ll tmp = x; tmp *= ope.x, tmp %= mod; x = tmp; return *this;}
	mint& operator/=(const mint &ope){
		ll n = mod-2; mint mul = ope;
		while(n){if(n & 1) *this *= mul; mul *= mul; n >>= 1;}
		return *this;
	}
	mint inverse(){return mint(1) / *this;}
	bool operator ==(const mint &ope){return x == ope.x;}
	bool operator !=(const mint &ope){return x != ope.x;}
	bool operator <(const mint &ope)const{return x < ope.x;}
};
mint modpow(mint a, ll n){
	if(n == 0) return mint(1);
	if(n % 2) return a * modpow(a, n-1);
	else return modpow(a*a, n/2);
}
istream& operator >>(istream &is, mint &ope){ll t; is >> t, ope = mint(t); return is;}
ostream& operator <<(ostream &os, mint &ope){return os << ope.x;}
ostream& operator <<(ostream &os, const mint &ope){return os << ope.x;}

ll modpow(ll a, ll n, ll mod){
	if(n == 0) return 1;
	if(n % 2) return ((a%mod) * (modpow(a, n-1, mod)%mod)) % mod;
	else return modpow((a*a)%mod, n/2, mod) % mod;
}

vector<mint> fact, fact_inv;
void make_fact(int n){
	fact.resize(n+1), fact_inv.resize(n+1);
	fact[0] = mint(1); rep(i, 1, n) fact[i] = fact[i-1] * mint(i);
	fact_inv[n] = fact[n].inverse(); per(i, n-1, 0) fact_inv[i] = fact_inv[i+1] * mint(i+1);
}
mint comb(int n, int k){ if(n < 0 || k < 0 || n < k) return mint(0); return fact[n] * fact_inv[k] * fact_inv[n-k];}
mint perm(int n, int k){ return comb(n, k) * fact[k]; }
mint divide(int n, int k){ return comb(n+k-1, k-1); }
template<typename T> T comb2(ll n, ll k){ if(n < 0 || k < 0 || n < k) return T(0); T ret = 1; rep(i, 1, k) ret *= n-k+i, ret /= i; return ret;}

vector<ll> prime, pvec, qrime;
void make_prime(int n){
	prime.resize(n+1);
	rep(i, 2, n){
		if(prime[i] == 0) pvec.push_back(i), prime[i] = i;
		for(auto p : pvec){ if(i*p > n || p > prime[i]) break; prime[i*p] = p;}
	}
}
void make_qrime(int n){
	qrime.resize(n+1);
	rep(i, 2, n){int ni = i / prime[i]; if(prime[i] == prime[ni]) qrime[i] = qrime[ni] * prime[i]; else qrime[i] = prime[i];}
}
void factorize(ll n, map<ll, ll> &mp){
	mp.clear();
	for(auto p : pvec) while(n % p == 0) mp[p]++, n /= p;
	if(n > 1) mp[n]++;
}

bool exceed(ll x, ll y, ll m){return y > 0 && x >= m / y + 1;}
void mark(){ cout << "*" << endl; }
void yes(){ cout << "Yes" << endl; }
void no(){ cout << "No" << endl; }
ll floor(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return a/b; else return -((-a+b-1)/b); }
ll ceil(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return (a+b-1)/b; else return -((-a)/b); }
ll modulo(ll a, ll b){ b = abs(b); return a - floor(a, b) * b;}
ll sgn(ll x){ if(x > 0) return 1; if(x < 0) return -1; return 0;}
ll gcd(ll a, ll b){if(b == 0) return a; return gcd(b, a%b);}
ll lcm(ll a, ll b){return a/gcd(a, b)*b;}
template<typename T> T arith(T x){return x*(x+1)/2;}
template<typename T> T arith2(T x){return x*(x+1)*(x*2+1)/6;}
ll digitnum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret++; return ret;}
ll digitsum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret += x % b; return ret;}
string lltos(ll x, ll b = 10){if(x == 0) return "0"; string ret; for(;x;x/=b) ret += x % b + '0'; reverse(all(ret)); return ret;}
ll stoll(string &s, ll b = 10){ll ret = 0; for(auto c : s) ret *= b, ret += c - '0'; return ret;}
template<typename T> void uniq(T &vec){sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}
int popcount(ull x){
	x -= ((x>>1)&0x5555555555555555ULL), x = (x & 0x3333333333333333ULL) + ((x>>2) & 0x3333333333333333ULL);
	return (((x + (x>>4)) & 0x0F0F0F0F0F0F0F0FULL) * 0x0101010101010101ULL) >> 56;
}
template<typename T> vector<pair<T, ll>> rle(vector<T> vec){
	vector<pair<T, ll>> ret;
	for(auto x : vec){if(sz(ret) == 0 || ret.back().first != x) ret.push_back(P(x, 1)); else ret.back().second++;}
	return ret;
}
vector<pair<char, ll>> rle(string s){ vector<char> vec; for(auto c : s) vec.push_back(c); return rle(vec);}

template<class S, class T> pair<S, T>& operator+=(pair<S, T> &s, const pair<S, T> &t){s.first += t.first, s.second += t.second; return s;}
template<class S, class T> pair<S, T>& operator-=(pair<S, T> &s, const pair<S, T> &t){s.first -= t.first, s.second -= t.second; return s;}
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first-t.first, s.second-t.second);}
template<class T> T dot(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.first + s.second*t.second;}
template<class T> T cross(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.second - s.second*t.first;}
template<class T> T mdist(pair<T, T> s, pair<T, T> t){return abs(s.first-t.first) + abs(s.second-t.second);}
template<class T> T cdist(pair<T, T> s, pair<T, T> t){return max(abs(s.first-t.first), abs(s.second-t.second));}
template<class T> T edist2(pair<T, T> s, pair<T, T> t){return (s.first-t.first)*(s.first-t.first) + (s.second-t.second)*(s.second-t.second);}

template<typename T> ostream& operator << (ostream& os, vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, deque<T>& deq){reps(i,  deq) os << deq[i] << " "; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, map<T, U>& ope){ for(auto p : ope) os << "(" << p.first << ", " << p.second << "),";return os;}
template<typename T> ostream& operator << (ostream& os, set<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, multiset<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> void outa(T a[], ll s, ll t){rep(i, s, t){ cout << a[i]; if(i < t) cout << " ";} cout << endl;}
template<typename T, size_t N> ostream& operator << (ostream& os, array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
template<typename T, size_t N> ostream& operator << (ostream& os, const array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
void dump_func(){cout << endl;}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail){cout << head; if(sizeof...(Tail) > 0) cout << " "; dump_func(std::move(tail)...);}
template<typename T> void bssert(bool b, T t){ if(!b) cout << t << endl, exit(0); }


struct LCA{
	struct edge{
		int to; ll cost;
		edge(){}
		edge(int a, int b){to = a, cost = b;}
	};

	int n, logn;
	vector<vector<edge> > G;
	vector<vector<int> > prev;
	vector<int> depth;
	vector<ll> dist;

	LCA(){}
	LCA(int n){init(n);}
	void init(int n){
		this->n = n;
		G.clear(), G.resize(n+1);
	}
	void add_edge(int u, int v, ll w = 1){
		G[u].push_back(edge(v, w));
		G[v].push_back(edge(u, w));
	}
	void predfs(int v, int p, int d, ll l)
	{
		prev[v][0] = p, depth[v] = d, dist[v] = l;
		for(auto e : G[v]){
			if(e.to == p) continue;
			predfs(e.to, v, d+1, l+e.cost);
		}
	}
	void calc(int root = 1){
		logn = 0;
		for(int i = 0; ; i++) if(n <= (1<<i)){ logn = i; break; }
		chmax(logn, 1);

		prev.resize(n+1);
		for(int i = 0; i < prev.size(); i++) prev[i].resize(logn);
		depth.resize(n+1), dist.resize(n+1);

		prev[0][0] = 0;
		predfs(root, 0, 0, 0);
		for(int i = 1; i < logn; i++){
			for(int j = 0; j <= n; j++){
				prev[j][i] = prev[prev[j][i-1]][i-1];
			}
		}
	}
	int getLCA(int u, int v){
		int x = u, y = v;
		if(depth[y] > depth[x]) swap(x, y);

		for(int i = logn-1; i >= 0; i--){
			if(depth[x] - (1<<i) >= depth[y]) x = prev[x][i];
		}
		if(x == y) return x;
		for(int i = logn-1; i >= 0; i--){
			if(prev[x][i] != prev[y][i]){
				x = prev[x][i], y = prev[y][i];
			}
		}
		x = prev[x][0];
		return x;
	}
	ll getDist(int u, int v){
		return dist[u]+dist[v]-dist[getLCA(u, v)]*2;
	}
	int getAncestor(int v, int k)
	{
		for(int i = 0; i < logn; i++){
			if(k & (1<<i)) v = prev[v][i];
		}
		return v;
	}
	int getNext(int u, int v){
		if(u == v) return -1;
		if(getLCA(u, v) == u) return getAncestor(v, depth[v]-depth[u]-1);
		else return prev[u][0];
	}
};

ll n, Q;
vector<ll> G[200005];
ll sub[200005];

void dfs(int v, int p)
{
	sub[v] = 1;
	for(auto u : G[v]){
		if(u == p) continue;
		dfs(u, v);
		sub[v] += sub[u];
	}
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> Q;
	LCA lca(n);

	ll u, v;
	rep(i, 1, n-1){
		cin >> u >> v;
		lca.add_edge(u, v);
		G[u].pb(v);
		G[v].pb(u);
	}
	dfs(1, -1);
	lca.calc();

	rep(i, 1, Q){
		cin >> u >> v;
		ll d = lca.getDist(u, v);
		if(d % 2){
			cout << 0 << "\n";
			continue;
		}
		d /= 2;

		ll l = lca.getLCA(u, v), m;
		if(lca.depth[u]-lca.depth[l] > lca.depth[v]-lca.depth[l]) m = lca.getAncestor(u, d);
		else m = lca.getAncestor(v, d);
		u = lca.getNext(m, u), v = lca.getNext(m, v);

		ll ans = sub[m];
		if(lca.depth[u] > lca.depth[m]) ans -= sub[u];
		if(lca.depth[v] > lca.depth[m]) ans -= sub[v];
		if(m == l) ans += sub[1] - sub[l];
		cout << ans << "\n";
	}


	return 0;
}
0