80分,一直没看出问题,求帮忙,感激不尽
查看原帖
80分,一直没看出问题,求帮忙,感激不尽
313163
scanf_楼主2021/5/28 11:35
#include<bits/stdc++.h>
using namespace std;
int n , s1 , s2 , maxDepth , maxLayer , dis , d1 , d2;
struct node{
	int lchild , rchild;
//	int layer;
}e[105];
int a[105];

void DFS(int root , int depth)
{
	if(root == 0){
		maxDepth = max(maxDepth , depth - 1);
		return;
	}
//	cout << root << " ";
	if(root == s1){
		d1 = (depth - 1) * 2;
	}
	if(root == s2){
		d2 = depth - 1;
	}
	a[depth] ++;
	DFS(e[root].lchild , depth + 1);
	DFS(e[root].rchild , depth + 1);
}
int main()
{
	cin >> n;
	for(int i = 1;  i <= n - 1; i ++){
		int root , child;
		cin >> root >> child;
		if(e[root].lchild == 0){
			e[root].lchild = child;
		}else{
			e[root].rchild = child;
		}
	}
	cin >> s1 >> s2;
	DFS(1 , 1);
	for(int i = 1 ; i <= 100 ; i ++){
//		if(a[i]) cout << a[i] << " ";
		maxLayer = max(maxLayer , a[i]);
	}
	printf("%d\n%d\n%d" , maxDepth , maxLayer , d1 + d2);
	return 0;
}
2021/5/28 11:35
加载中...