제출 #650203

#제출 시각아이디문제언어결과실행 시간메모리
650203ymm구슬과 끈 (APIO14_beads)C++17
0 / 100
3 ms5016 KiB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 200'010;
vector<pii> A[N];
int n;

pii dfs(int v, int p, int w)
{
	int sum = 0;
	vector<int> vec; // without par - with par + w
	for (auto [u, e] : A[v]) {
		if (u == p)
			continue;
		auto [x, y] = dfs(u, v, e);
		vec.push_back(y - x + e);
		sum += x;
	}
	sort(vec.rbegin(), vec.rend());
	pii ans;
	ans.second = sum;
	if (vec.size() >= 2)
		ans.second = max(ans.second, sum + vec[0] + vec[1]);
	ans.first = ans.second;
	if (vec.size() >= 1 && p >= 0)
		ans.first = max(ans.first, sum + w + vec[0]);
	return ans;
}

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	cin >> n;
	Loop (i,1,n) {
		int v, u, w;
		cin >> v >> u >> w;
		--v; --u;
		A[v].push_back({u, w});
		A[u].push_back({v, w});
	}
	cout << dfs(0, -1, 0).first << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...