求助WA#3#4
查看原帖
求助WA#3#4
677788
nidezhishangnaqula楼主2024/3/31 22:24
#include<bits/stdc++.h>
using namespace std;
int wid[105], dep[105], widmax, depmax, father[105], n, u, v;
vector<short>kids[105];
void dfs(int u) {
	dep[u] = dep[father[u]] + 1;
	if (dep[u] > depmax) depmax = dep[u];
	wid[dep[u]]++;
	if (wid[dep[u]] > widmax) widmax = wid[dep[u]];
	for (int i = 0; i < kids[u].size(); i++) {
		dfs(kids[u][i]);
	}
}
int main() {

	cin >> n;
	for (int i = 1; i < n; i++) {
		cin >> u >> v;
		father[v] = u;
		kids[u].push_back(v);
	}
	dfs(1);
	cout << depmax << endl << widmax << endl;
	int x, y, n1 = 0, n2 = 0;
	cin >> x >> y;
	if (dep[x] > dep[y]) {
		while (dep[x] > dep[y]) {
			n1++;
			dep[x] = dep[father[x]];
			x = father[x];
		}
	} else if (dep[x] < dep[y]) {
		n2++;
		while (dep[x] < dep[y]) {
			dep[y] = dep[father[y]];
			y = father[y];
		}
	}
	while (x != y) {
		n1++;
		n2++;
		x = father[x];
		y = father[y];
	}
	cout << n1 * 2 + n2;
	return 0;
}
2024/3/31 22:24
加载中...