QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#235189#6559. A Tree and Two Edgesugly2333WA 2ms10092kbC++201.6kb2023-11-02 15:49:132023-11-02 15:49:14

Judging History

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

  • [2023-11-02 15:49:14]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:10092kb
  • [2023-11-02 15:49:13]
  • 提交

answer

//Δ_B
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 222222;
const int M = 22;
int n,m=20,q,a1,b1,a2,b2,f[N],d[N],g[N][M];
vector<int> v[N];
int fnd(int x){
	if(f[x]==x)
		return x;
	return f[x]=fnd(f[x]);
}
void dfs(int u,int fa){
	g[u][0]=fa;
	d[u]=d[fa]+1;
	int i,x;
	for(i=0;i<v[u].size();i++){
		x=v[u][i];
		if(x!=fa)
			dfs(x,u);
	}
}
int lca(int x,int y){
	int j;
	if(d[x]>d[y])
		swap(x,y);
	for(j=20;j>=0;j--)
		if(d[x]<=d[g[y][j]])
			y=g[y][j];
	if(x==y)
		return x;
	for(j=20;j>=0;j--)
		if(g[x][j]!=g[y][j])
			x=g[x][j],y=g[y][j];
	return g[x][0];
}
int chk(int a,int b,int c){
	int x=lca(a,b);
	return !((lca(a,c)==c||lca(b,c)==c)&&lca(c,x)==x);
}
int chk2(int a1,int b1,int a2,int b2){
	return chk(a1,b1,lca(a2,b2))&&chk(a2,b2,lca(a1,b1));
}
int chk3(int a1,int b1,int a2,int b2,int a3,int b3){
	return chk2(a1,b1,a2,b2)&&chk2(a2,b2,a3,b3)&&chk2(a3,b3,a1,b1);
}
int main(){
	int i,j,x,y;
	scanf("%d%d",&n,&q);
	for(i=1;i<=n;i++)
		f[i]=i;
	for(i=1;i<=n+1;i++){
		scanf("%d%d",&x,&y);
		if(fnd(x)==fnd(y)){
			if(a1)
				a2=x,b2=y;
			else
				a1=x,b1=y;
		}
		else{
			v[x].push_back(y);
			v[y].push_back(x);
			f[fnd(y)]=x;
		}
	}
	dfs(1,0);
	for(j=1;j<=m;j++)
		for(i=1;i<=n;i++)
			g[i][j]=g[g[i][j-1]][j-1];
	while(q--){
		scanf("%d%d",&x,&y);
		i=1;
		i+=chk2(x,a1,b1,y)+chk2(x,b1,a1,y);
		i+=chk2(x,a2,b2,y)+chk2(x,b2,a2,y);
		i+=chk3(x,a1,b1,a2,b2,y)+chk3(x,b1,a1,a2,b2,y)+chk3(x,a1,b1,b2,a2,y)+chk3(x,b1,a1,b2,a2,y);
		printf("%d\n",i);
	}
	return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 10092kb

input:

4 6
1 2
1 3
1 4
2 3
2 4
1 2
1 3
1 4
2 3
2 4
3 4

output:

3
2
3
3
3
4

result:

wrong answer 2nd lines differ - expected: '3', found: '2'