求助,第一个测试点过不了
查看原帖
求助,第一个测试点过不了
1280199
zqh_7plus楼主2024/4/5 19:36
#include<iostream>
using namespace std;
int d[120]={0},fw[120]={0},n,s[120][120],sd[120],a,xs[120]={0},xxx=1;
void dfs(int now,int m){
	d[now]=m;
	sd[m]++;
	a=max(a,d[now]);
	for(int i=1;i<=n;i++) if(s[now][i]==1&&fw[i]==0){
		fw[i]=1;
		if(d[i]>m) dfs(i,m+1);
	}
}
void lac(int now){
	for(int i=1;i<=n;i++) if(s[now][i]==1&&d[now]>d[i]){
		xs[i]++;
		lac(i);
	}
}
int main( ) {
	int x,y,maax,maxx,ma=1;
	cin>>n;
	for(int i=0; i<n-1; i++) {
		cin>>x>>y;
		s[x][y]=1;
		s[y][x]=1;
	}
	for(int i=0;i<=n;i++) d[i]=99999999;
	cin>>x>>y;
	if(x==y){
		cout<<"0";
		return 0;
	}
	dfs(1,1);
	maxx=sd[1];
	for(int i=1;i<=n;i++) if(sd[i]>maxx) maxx=sd[i];
	cout<<a<<endl;
	cout<<maxx<<endl;
	lac(x);
	lac(y);
	ma=1;
	for(int i=0;i<=n;i++) if(xs[i]==2&&d[i]>d[ma]) ma=i;
	cout<<(d[x]-d[ma])*2+d[y]-d[ma];
}

这是我的代码,只错第一个测试点,求助

2024/4/5 19:36
加载中...