이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// And you curse yourself for things you never done
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 2e5 + 10, mod = 1e9 + 7;
const ll inf = 1e18 + 10;
vector<pii> v[maxn];
ll ans[maxn], dp[maxn];
void dfs(int u, int par = -1){
ll mx1 = -inf, mx2 = -inf, sm = 0;
for(auto [y, c] : v[u]){
if(y != par){
dfs(y, u);
ll x = max(ans[y], dp[y] + c);
sm+= x;
x = ans[y] - x + c;
if(x > mx1)
mx2 = mx1, mx1 = x;
else if(x > mx2)
mx2 = x;
}
}
dp[u] = sm + mx1;
ans[u] = max(sm, sm + mx1 + mx2);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
int n;
cin >> n;
for(int i = 0; i < n-1; i++){
int a, b, c;
cin >> a >> b >> c;
v[a].PB({b, c});
v[b].PB({a, c});
}
dfs(1);
return cout << ans[1] << endl, 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:24:14: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
for(auto [y, c] : v[u]){
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |