# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
665750 | AmirAli_H1 | 구슬과 끈 (APIO14_beads) | C++17 | 4 ms | 5088 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define debug(x) cerr << #x << ": " << x << endl;
#define kill(x) cout << x << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
int n;
const int maxn = 2e5 + 5;
vector<pll> adj[maxn];
ll dp[maxn][2];
bool mark[maxn];
void dfs(int v, ll x = -1) {
mark[v] = 1;
vector<ll> ls;
for (auto f : adj[v]) {
int u = f.F; ll w = f.S;
if (!mark[u]) {
dfs(u, w);
dp[v][0] += dp[u][1];
dp[v][1] += dp[u][1];
ls.pb((dp[u][0] + w) - dp[u][1]);
}
}
sort(all(ls), greater<ll>());
if (len(ls) >= 2 && ls[0] + ls[1] > 0) dp[v][0] += (ls[0] + ls[1]);
if (len(ls) >= 1 && x != -1 && x + ls[0] > 0) dp[v][1] += (x + ls[0]);
dp[v][1] = max(dp[v][1], dp[v][0]);
ls.clear();
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
file_io("beads.in", "beads.out");
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v; ll w;
cin >> u >> v >> w; u--; v--;
adj[u].pb(Mp(v, w)); adj[v].pb(Mp(u, w));
}
dfs(0);
cout << dp[0][1] << endl;
return 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... |