代码求调
  • 板块灌水区
  • 楼主sz_jinzikai
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/6/6 13:39
  • 上次更新2023/10/23 13:50:38
查看原帖
代码求调
592476
sz_jinzikai楼主2023/6/6 13:39

题目

题目传送门

错误代码:

# include <bits/stdc++.h>
using namespace std;
typedef pair <int, int> pii;
vector <pii> v[10005];
int n, ans, a, b, c;
int dfs (int u, int f) {
	int maxx = 0, x = 0, y = 0;
	for (pii i : v[u]) {
		if (i.first == f)
			continue ;
		maxx = max (maxx, dfs (i.first, u) + i.second);
		if (maxx >= x)
			y = x, x = maxx;
		else if (maxx > y)
			y = maxx;
	}
	ans = max (ans, x + y);
	return maxx;
}
int main () {
	cin >> n;
	while (-- n) {
		cin >> a >> b >> c;
		v[a].push_back ({b, c});
		v[b].push_back ({a, c});
	}
	dfs (1, -1);
	cout << ans;
	return 0;
}

错误数据:

input

10
1 4 46
6 7 30
7 8 17
1 9 4
2 5 24
6 10 17
1 6 16
2 3 15
1 2 23

output

126

answer

110

2023/6/6 13:39
加载中...