QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#664531#6473. Cat and MouseDaiRuiChen007RE 0ms0kbC++171.4kb2024-10-21 21:01:362024-10-21 21:01:37

Judging History

你现在查看的是最新测评结果

  • [2024-10-21 21:01:37]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-10-21 21:01:36]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int MAXN=5e4+5;
unordered_map <int,int> d[MAXN];
vector <int> G[MAXN];
int n,f[MAXN],g[MAXN],dep[MAXN],up[MAXN][20],L[MAXN],R[MAXN],dcnt;
void dfs(int u,int fz) {
	L[u]=++dcnt,up[u][0]=fz,dep[u]=dep[fz]+1;
	for(int k=1;k<20;++k) up[u][k]=up[up[u][k-1]][k-1];
	vector <int> e;
	for(int v:G[u]) if(v^fz) dfs(v,u),e.push_back(v);
	R[u]=dcnt,G[u].swap(e);
}
int nxt(int s,int t) { //s->t
	if(L[t]<L[s]||R[s]<L[t]) return up[s][0];
	return *lower_bound(G[s].begin(),G[s].end(),t,[&](int x,int y){ return R[x]<R[y]; });
}
void solve() {
	int X;
	scanf("%d%d",&n,&X);
	for(int i=1;i<=n;++i) f[i]=g[i]=0,G[i].clear(),d[i].clear();
	for(int i=1,u,v;i<n;++i) {
		scanf("%d%d",&u,&v);
		G[u].push_back(v),G[v].push_back(u);
		g[u]=f[u],f[u]=v,g[v]=f[v],f[v]=u;
	}
	dcnt=0,dfs(1,0);
	queue <array<int,2>> q;
	d[1][f[X]^1?f[X]:g[X]]=0;
	q.push({1,f[X]^1?f[X]:g[X]});
	while(q.size()) {
		int u=q.front()[0],v=q.front()[1],dis=d[u][v]; q.pop();
		if(!v) return printf("%d\n",dis),void();
		auto ext=[&](int x,int y) {
			if(!d[x].count(y)) d[x][y]=dis+1,q.push({x,y});
		};
		if(u==f[v]) ext(u,g[v]),ext(v,u),ext(f[u],u);
		else {
			int z=nxt(u,v);
			if(z==f[v]) ext(z,g[v]),ext(u,z);
			else ext(z,f[v]);
		}
	}
}
signed main() {
	freopen("toptree.in","r",stdin);
	freopen("toptree.out","w",stdout);
	int T; scanf("%d",&T);
	while(T--) solve();
	return 0;
}

详细

Test #1:

score: 0
Dangerous Syscalls

input:

100
50000 11576
13519 31672
47035 48424
15051 40308
19661 34367
9452 17569
390 9986
26705 26923
37100 10713
25643 42676
9267 19233
49342 37739
6298 3807
5751 22924
25960 4439
33941 13190
12456 3487
29455 14728
49187 6717
36264 16848
46193 37225
30433 49509
2672 30522
12241 13826
42170 21723
19926 39...

output:


result: