제출 #552151

#제출 시각아이디문제언어결과실행 시간메모리
552151QwertyPi구슬과 끈 (APIO14_beads)C++14
0 / 100
4 ms5008 KiB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back

using namespace std;
typedef pair<int, int> pii;
const int N = 2e5 + 5;
vector<pii> G[N];
int dp[N][2], W[N];

void dfs(int v, int p = -1){
	int ttl = 0;
	vector<int> ws;
	for(auto [i, w] : G[v]){
		if(i == p) continue;
		W[i] = w;
		dfs(i, v);
		ws.push_back(w + dp[i][0] - dp[i][1]);
		ttl += dp[i][1];
	}
	sort(ws.begin(), ws.end(), greater<>());
	dp[v][0] = ttl;
	if(p != -1 && ws.size() >= 1) dp[v][1] = ttl + W[v] + ws[0];
	if(ws.size() >= 2) dp[v][0] = max(dp[v][0], ttl + ws[0] + ws[1]);
	dp[v][1] = max(dp[v][0], dp[v][1]);
}

int32_t main(){
	int n;
	cin >> n;
	for(int i = 0; i < n - 1; i++){
		int u, v, we;
		cin >> u >> v >> we;
		G[u].pb({v, we});
		G[v].pb({u, we});
	}
	dfs(1);
//	for(int i = 1; i <= n; i++){
//		cout << i << ": " << dp[i][0] << ' ' << dp[i][1] << endl;
//	}
	cout << max(dp[1][0], dp[1][1]) << endl;
}

컴파일 시 표준 에러 (stderr) 메시지

beads.cpp: In function 'void dfs(long long int, long long int)':
beads.cpp:16:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   16 |  for(auto [i, w] : G[v]){
      |           ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...