WA第一个点,91pts求调
查看原帖
WA第一个点,91pts求调
1077447
jinshixian楼主2025/8/3 21:30
#include <bits/stdc++.h>

using namespace std;

int n, d[10], dep = -1, wid = -1, ans;

struct TreeNode{
	int l, r, depth, fa;
} a[101];

inline void dfs(int x, int y){
	int x2 = x, y2 = y;
	if (a[x2].depth > a[y2].depth)
	    while (a[x2].depth != a[y2].depth){
			x2 = a[x2].fa;
			ans += 2;
		}
	if (a[x2].depth < a[y2].depth)
	    while (a[x2].depth != a[y2].depth){
			y2 = a[y2].fa;
			ans++;
		}
	x = x2, y = y2;
	while (x2 != y2)
		x2 = a[x2].fa, y2 = a[y2].fa;
	while (x != x2){
		x = a[x].fa;
		ans += 2; 
	}
	while (y != x2){
		y = a[y].fa;
		ans++;
	}
	cout << ans << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	a[1].depth = 1;
	a[1].fa = 1;
	for (int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		if (!a[u].l)
		    a[u].l = v;
		else 
		    a[u].r = v;
		a[v].fa = u;
		a[v].depth = a[u].depth + 1;
		d[a[v].depth]++;
		dep = max(dep, a[v].depth);
	}
	for (int i = 1; i <= dep; i++)
	    wid = max(wid, d[i]);
	int x, y;
	cin >> x >> y;
	cout << dep << endl << wid << endl;
	dfs(x, y);
}
2025/8/3 21:30
加载中...