# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
158441 | arnold518 | 구슬과 끈 (APIO14_beads) | C++14 | 6 ms | 4984 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
int N;
vector<pii> adj[MAXN+10];
ll dp1[MAXN+10], dp2[MAXN+10];
void dfs(int now, int bef, int par)
{
vector<ll> V;
ll S=0;
for(pii nxt : adj[now])
{
if(nxt.first==bef) continue;
dfs(nxt.first, now, nxt.second);
S+=max(dp1[nxt.first], dp2[nxt.first]);
V.push_back(dp2[nxt.first]+nxt.second-max(dp1[nxt.first], dp2[nxt.first]));
}
sort(V.begin(), V.end(), greater<ll>());
dp2[now]=S;
if(V.size()>1 && V[0]+V[1]>=0) dp2[now]+=V[0]+V[1];
if(V.size()<=0) dp1[now]=-987654321987654321;
else dp1[now]=S+par+V[0];
//printf("%d %lld %lld\n", now, dp1[now], dp2[now]);
}
int main()
{
int i, j;
scanf("%d", &N);
for(i=1; i<N; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
dfs(1, 1, 0);
printf("%lld", dp2[1]);
}
컴파일 시 표준 에러 (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... |