이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
//#define endl '\n'
vector<vi> dp;
vector<vector<array<int, 2>>> adj;
vi cost;
int n;
void dfs(int p, int parent, int pe) {
dp[p] = {0, 0};
int v = 0;
for (auto i: adj[p]) if (i[0] != parent) {
dfs(i[0], p, i[1]);
v = max(v, -dp[i[0]][0] + dp[i[0]][1] + i[1] + pe);
dp[p][1] += dp[i[0]][0];
}
dp[p][0] = dp[p][1] + v;
// cout << p << " " << dp[p][0] << " " << dp[p][1] << endl;
}
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
cost.resize(n); dp.resize(n, vi(2)); adj.resize(n);
for_(i, 0, n-1) {
int a, b, w; cin >> a >> b >> w;
a -= 1; b -= 1;
adj[a].push_back({b, w}); adj[b].push_back({a, w});
}
int ans = 0;
for_(i, 0, n) {
dfs(i, i, 0);
ans = max(ans, dp[i][1]);
}
cout << ans << endl;
return 0;
}
# | 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... |