QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#408516#8547. Whose Land?dijahWA 3ms8484kbC++982.5kb2024-05-10 16:12:462024-05-10 16:12:48

Judging History

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

  • [2024-05-10 16:12:48]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:8484kb
  • [2024-05-10 16:12:46]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int n,m,k,dfn[100005],bfn[100005],num,f[100005][21],deep[100005],l[100005],r[100005],re[100005],out[100005],d[100005],s=0,f1[100005][21];
vector<int> a[100005],t[100005];
bool cmp(int x,int y)
{
	return dfn[x]<dfn[y];
}
void dfs1(int x,int y)
{
	deep[x]=deep[y]+1,f[x][0]=y,dfn[x]=++num;
	for(int i=0;i<a[x].size();i++)if(a[x][i]!=y)dfs1(a[x][i],x);
	out[x]=num;
	return;
}
void dfs2(int x,int y,int dis,int z)
{
	if(dis>k)return;
	d[z]++;
	for(int i=0;i<a[x].size();i++)if(a[x][i]!=y)dfs2(a[x][i],x,dis+1,z);
	return;
}
int le(int x,int y)
{
	int mid,left=l[deep[x]+y],right=r[deep[x]+y],s=n+1;
	if(left==0)return 0;
	while(left<=right)
	{
		mid=(left+right)>>1;
		if(dfn[re[mid]]>=dfn[x])right=mid-1,s=min(s,mid);
		else left=mid+1;
	}
	if(s>n||dfn[re[s]]>out[x])return 0;
	return s;
}
int ri(int x,int y)
{
	int mid,left=l[deep[x]+y],right=r[deep[x]+y],s=0;
	if(left==0)return 0;
	while(left<=right)
	{
		mid=(left+right)>>1;
		if(dfn[re[mid]]<=out[x])left=mid+1,s=max(s,mid); 
		else right=mid-1;
	}
	if(s<0||dfn[re[s]]<dfn[x])return 0;
	return s;
}
int LCA(int x,int y)
{
	if(deep[x]<deep[y])swap(x,y);
	for(int i=20;i>=0;i--)s+=(deep[f[x][i]]>=deep[y])?f1[x][i]:0,x=(deep[f[x][i]]>=deep[y])?f[x][i]:x;
	if(x==y)return x;
	for(int i=20;i>=0;i--)s+=(f[x][i]!=f[y][i])?f1[x][i]+f1[y][i]:0,x=(f[x][i]!=f[y][i])?f[x][i]:x,y=(deep[x]!=deep[y])?f[y][i]:y; 
	s+=f1[x][0]+f1[y][0];
	return f[x][0];
}
void poi()
{
	int x,y;
	scanf("%d%d%d",&n,&k,&m);
	for(int i=1;i<=n;i++)a[i].clear(),t[i].clear(),l[i]=r[i]=re[i]=out[i]=d[i]=0;
	for(int i=1;i<n;i++)scanf("%d%d",&x,&y),a[x].push_back(y),a[y].push_back(x);
	num=0,dfs1(1,0),num=0;
	for(int i=1;i<=n;i++)t[deep[i]].push_back(i);
	for(int i=1;i<=n;i++)sort(t[i].begin(),t[i].end(),cmp);
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<t[i].size();j++)bfn[t[i][j]]=++num,re[num]=t[i][j];
		if(t[i].size()!=0)l[i]=bfn[t[i][0]],r[i]=num;
		else break;
	}
	for(int i=1;i<=n;i++)
	{
		dfs2(i,0,0,i),x=le(i,k),y=ri(i,k);
		if(x!=0)f1[i][0]=y-x+1;
	}
	for(int i=1;i<=20;i++)
	{
		for(int j=1;j<=n;j++)f[j][i]=f[f[j][i-1]][i-1],f1[j][i]=f1[j][i-1]+f1[f[j][i-1]][i-1];
	}
//	for(int i=1;i<=n;i++)cout<<i<<':'<<f1[i][0]<<' '<<d[i]<<' '<<bfn[i]<<' '<<deep[i]<<'\n';
	for(int i=1;i<=m;i++)s=0,scanf("%d%d",&x,&y),printf("%d\n",d[LCA(x,y)]+s);
	return;
}
int main()
{
	int qwe;
	scanf("%d",&qwe);
	for(int i=1;i<=qwe;i++)poi();








  return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 8484kb

input:

2
5 1 2
1 2
1 3
2 4
2 5
2 2
2 3
8 2 3
1 2
1 3
2 4
2 5
4 6
5 7
7 8
2 2
2 5
3 4

output:

4
5
7
8
7

result:

wrong answer 5th numbers differ - expected: '6', found: '7'