求hack / 求调
查看原帖
求hack / 求调
1286053
Nostopathy楼主2025/8/5 08:32

10分

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define pb push_back
#define rep(a, b, c, d) for(int a=b; a<=c; a+=d)
const int N = 2e5 + 5;
int n;
vector<pii> G[N];
bool cmax(int &x, int y) {
	return (y > x ? x = y, 0 : 1);
}
signed main () {
	cin >> n;
	rep(i, 2, n, 1) {
		int x, y;
		cin >> x >> y;
		G[i].pb({x, y});
		G[x].pb({i, y});
	}
	int ans = 0;
	rep(i, 1, n, 1) {
		sort(G[i].begin(), G[i].end(), greater<pii>());
		if(G[i].size() >= 3)
			cmax(ans, G[i][0].second + G[i][1].second + G[i][2].second);
	}
	rep(i, 1, n, 1) {
		if(G[i].size() <= 1)
			continue;
		for(pii j: G[i]) {
			int x = j.first, y = j.second;
			if(G[x].size() <= 1)
				continue;
			cmax(ans, y + G[x][G[x][0].first == i].second + G[i][G[i][0].first == x].second);
		}
	}
	cout << ans;
	return 0;
}
2025/8/5 08:32
加载中...