# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
26098 | nibnalin | 구슬과 끈 (APIO14_beads) | C++14 | 6 ms | 5120 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = int(2e5)+5, inf = int(1e9)+5;
int dp[maxn][2], dpmx[maxn], dp2[maxn];
vector<pair<int, int>> graph[maxn];
void dfs(int node, int par)
{
pair<int, int> opt = {-inf, -inf};
for(auto it: graph[node])
{
if(it.first != par)
{
dfs(it.first, node);
dpmx[it.first] = max(dp[it.first][0], dp[it.first][1]+it.second);
dp2[node] += dpmx[it.first];
int tmp = it.second+dp[it.first][0]-dpmx[it.first];
if(tmp > opt.first)
{
opt.second = opt.first;
opt.first = tmp;
}
else if(tmp > opt.second)
{
opt.second = tmp;
}
}
}
dp[node][0] = dp2[node]+max(0, opt.first+opt.second);
dp[node][1] = dp2[node]+opt.first;
}
int main(void)
{
int n, u, v, c;
scanf("%d", &n);
for(int i = 1;i < n;i++)
{
scanf("%d%d%d", &u, &v, &c);
u--, v--;
graph[u].push_back({v, c});
graph[v].push_back({u, c});
}
dfs(0, -1);
printf("%d\n", dp[0][0]);
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |