Submission #363130

#TimeUsernameProblemLanguageResultExecution timeMemory
363130shivensinha4Beads and wires (APIO14_beads)C++17
28 / 100
1098 ms5484 KiB
#include "bits/stdc++.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
//#define endl '\n'

const int MXN = 2e5;
array<int, 2> dp[MXN];
vector<array<int, 2>> adj[MXN];
int n;

void dfs(int p, int parent, int pe) {
	dp[p] = {0, 0};
	int v = 0;
	for (auto i: adj[p]) if (i[0] != parent) {
		dfs(i[0], p, i[1]);
		v = max(v, -dp[i[0]][0] + dp[i[0]][1] + i[1] + pe);
		dp[p][1] += dp[i[0]][0];
	}
	dp[p][0] = dp[p][1] + v;
//	cout << p << " " << dp[p][0] << " " << dp[p][1] << endl;
}

int main() {
#ifdef mlocal
	freopen("test.in", "r", stdin);
#endif
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n;
	
	for_(i, 0, n-1) {
		int a, b, w; cin >> a >> b >> w;
		a -= 1; b -= 1;
		adj[a].push_back({b, w}); adj[b].push_back({a, w});
	}
	
	int ans = 0;
	for_(i, 0, n) {
		dfs(i, i, 0);
		ans = max(ans, dp[i][1]);
	}
	
	cout << ans << endl;
	
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...