結果

問題 No.2337 Equidistant
ユーザー 沙耶花沙耶花
提出日時 2023-06-02 21:44:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,673 ms / 4,000 ms
コード長 2,270 bytes
コンパイル時間 5,205 ms
コンパイル使用メモリ 266,228 KB
実行使用メモリ 54,840 KB
最終ジャッジ日時 2023-08-28 03:05:37
合計ジャッジ時間 23,287 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 7 ms
4,380 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 7 ms
4,380 KB
testcase_11 AC 829 ms
36,124 KB
testcase_12 AC 813 ms
36,224 KB
testcase_13 AC 825 ms
36,352 KB
testcase_14 AC 808 ms
36,204 KB
testcase_15 AC 808 ms
36,244 KB
testcase_16 AC 811 ms
36,092 KB
testcase_17 AC 837 ms
36,080 KB
testcase_18 AC 831 ms
36,256 KB
testcase_19 AC 827 ms
36,468 KB
testcase_20 AC 825 ms
36,164 KB
testcase_21 AC 1,148 ms
54,840 KB
testcase_22 AC 680 ms
36,840 KB
testcase_23 AC 746 ms
36,752 KB
testcase_24 AC 1,510 ms
48,572 KB
testcase_25 AC 764 ms
36,972 KB
testcase_26 AC 1,673 ms
48,408 KB
testcase_27 AC 769 ms
36,616 KB
testcase_28 AC 778 ms
36,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001

struct lca{
	
	vector<int> depth;
	vector<vector<int>> parents;
	
	int max_j=18;
	
	lca(int n,vector<vector<int>> &E){
		rep(i,100){
			if((1<<i)>E.size()){
				max_j = i;
				break;
			}
		}
		depth.assign(E.size(),-1);
		parents.assign(E.size(),vector<int>(max_j,-1));
		
		
		
		stack<int> s;
		s.push(n);
		depth[n] = 0;
		while(s.size()!=0){
			int k = s.top();
			s.pop();
			for(int i=0;i<E[k].size();i++){
				int x = E[k][i];
				if(depth[x]!=-1)continue;
				s.push(x);
				depth[x] = depth[k]+1;
				for(int j=0;j<max_j;j++){
					if(j==0){
						parents[x][j] = k;
					}
					else{
						parents[x][j] = parents[parents[x][j-1]][j-1];
					}
					if(parents[x][j] == -1)break;
				}
			}
		}
	}
	
	
	int kth_parent(int u,int k){
		for(int i=0;i<max_j;i++){
			if(k==0)break;
			if(u==-1)break;
			if(k%2){
				u = parents[u][i];
			}
			k/=2;
		}
		return u;
	}
	
	int query(int u,int v){
		if(depth[u]<depth[v])swap(u,v);
		u = kth_parent(u,depth[u]-depth[v]);
		
		if(u==v){
			return u;
		}
		
		for(int i=max_j-1;i>=0;i--){
			if(parents[u][i]!=parents[v][i]){
				u = parents[u][i];
				v = parents[v][i];
			}
		}
		
		return parents[u][0];
		
	}
	
	int get_distance(int u,int v){
		return depth[u]+depth[v]-2*depth[query(u,v)];
	}
	
	
};

vector<int> sz;

void dfs(int cv,int pv,vector<vector<int>> &E){
	sz[cv] = 1;
	rep(i,E[cv].size()){
		int to = E[cv][i];
		if(to==pv)continue;
		dfs(to,cv,E);
		sz[cv] += sz[to];
	}
}

int main(){
	
	int n,q;
	cin>>n>>q;
	vector<vector<int>> E(n);
	rep(i,n-1){
		int a,b;
		cin>>a>>b;
		a--,b--;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	lca L(0,E);
	sz.resize(n);
	dfs(0,-1,E);
	rep(_,q){
		int a,b;
		cin>>a>>b;
		a--,b--;
		int d =L.get_distance(a,b);
		if(d%2==1){
			cout<<0<<endl;
			continue;
		}
		if(L.depth[a]<L.depth[b])swap(a,b);
		int c = L.kth_parent(a,d/2);
		if(L.query(a,b)!=c){
			cout<<sz[c] - sz[L.kth_parent(a,d/2 - 1)]<<endl;
		}
		else{
			cout<<n - sz[L.kth_parent(a,d/2-1)] - sz[L.kth_parent(b,d/2-1)]<<endl; 
		}
	}
	
	return 0;
}
0