結果

問題 No.2337 Equidistant
ユーザー 👑 kmjpkmjp
提出日時 2023-06-02 22:45:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,302 ms / 4,000 ms
コード長 2,037 bytes
コンパイル時間 2,178 ms
コンパイル使用メモリ 204,992 KB
実行使用メモリ 48,792 KB
最終ジャッジ日時 2023-08-28 04:59:17
合計ジャッジ時間 19,055 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
22,784 KB
testcase_01 AC 7 ms
22,736 KB
testcase_02 AC 7 ms
22,732 KB
testcase_03 AC 6 ms
22,936 KB
testcase_04 AC 7 ms
22,940 KB
testcase_05 AC 7 ms
22,804 KB
testcase_06 AC 11 ms
22,876 KB
testcase_07 AC 11 ms
22,864 KB
testcase_08 AC 11 ms
22,908 KB
testcase_09 AC 10 ms
22,896 KB
testcase_10 AC 11 ms
22,896 KB
testcase_11 AC 705 ms
33,316 KB
testcase_12 AC 691 ms
33,272 KB
testcase_13 AC 680 ms
33,268 KB
testcase_14 AC 679 ms
33,380 KB
testcase_15 AC 663 ms
33,276 KB
testcase_16 AC 665 ms
33,316 KB
testcase_17 AC 676 ms
33,384 KB
testcase_18 AC 677 ms
33,380 KB
testcase_19 AC 682 ms
33,300 KB
testcase_20 AC 676 ms
33,276 KB
testcase_21 AC 799 ms
48,792 KB
testcase_22 AC 926 ms
33,480 KB
testcase_23 AC 679 ms
34,028 KB
testcase_24 AC 904 ms
43,576 KB
testcase_25 AC 707 ms
34,164 KB
testcase_26 AC 1,302 ms
43,508 KB
testcase_27 AC 906 ms
34,092 KB
testcase_28 AC 913 ms
34,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
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(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,Q;
vector<int> E[200005];
int P[21][200005],D[200005];
int L[202020],R[202020],id;

void dfs(int cur) {
	L[cur]=id++;
	sort(ALL(E[cur]));
	FORR(e,E[cur]) if(e!=P[0][cur]) D[e]=D[cur]+1, P[0][e]=cur, dfs(e);
	R[cur]=id;
}
int getpar(int cur,int up) {
	int i;
	FOR(i,20) if(up&(1<<i)) cur=P[i][cur];
	return cur;
}

int lca(int a,int b) {
	int ret=0,i,aa=a,bb=b;
	if(D[aa]>D[bb]) swap(aa,bb);
	for(i=19;i>=0;i--) if(D[bb]-D[aa]>=1<<i) bb=P[i][bb];
	for(i=19;i>=0;i--) if(P[i][aa]!=P[i][bb]) aa=P[i][aa], bb=P[i][bb];
	return (aa==bb)?aa:P[0][aa];               // vertex
}


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>Q;
	FOR(i,N-1) {
		cin>>x>>y;
		E[x-1].push_back(y-1);
		E[y-1].push_back(x-1);
	}

	//0以外を根にするならP[0][root]=rootが必要
	dfs(0);
	FOR(i,19) FOR(x,N) P[i+1][x]=P[i][P[i][x]];
	
	while(Q--) {
		cin>>x>>y;
		x--,y--;
		if(D[x]%2!=D[y]%2) {
			cout<<0<<endl;
			continue;
		}
		if(D[x]<D[y]) swap(x,y);
		int lc=lca(x,y);
		if(D[x]==D[y]) {
			x=getpar(x,D[x]-D[lc]-1);
			y=getpar(y,D[y]-D[lc]-1);
			cout<<N-(R[x]-L[x])-(R[y]-L[y])<<endl;
		}
		else {
			y=getpar(x,D[x]-(D[lc]+(D[x]-D[y])/2));
			x=getpar(x,D[x]-D[y]-1);
			cout<<(R[y]-L[y])-(R[x]-L[x])<<endl;
		}
		
		
	}
	
		
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0