이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define pp pop_back
#define sz(a) (int)(a.size())
#define mp make_pair
#define F first
#define S second
#define next _next
#define prev _prev
#define left _left
#define right _right
#define y1 _y1
#define all(x) x.begin(), x.end()
#define what_is(x) #x << " is " << (x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = (int)1e5 + 123;
const ll INF = (ll)1e18 + 123;
const int inf = (int)1e9 + 123;
const int MOD = (int)1e9 + 7;
void megaRandom() {
unsigned int FOR;
asm("rdtsc" : "=A"(FOR));
srand(FOR);
}
int n;
vector<pii> g[N];
ll dp[3][N], res;
void dfs(int x, int pr = 0, int par_edge = 0) {
ll sum = 0;
for(auto to : g[x]) {
if(to.F == pr) continue;
dfs(to.F, x, to.S);
sum += max(dp[0][to.F], dp[1][to.F]);
dp[0][x] = max({dp[0][x], dp[1][to.F], dp[0][to.F]});
}
for(auto to : g[x]) {
if(to.F == pr) continue;
dp[1][x] = max(dp[1][x], dp[0][to.F] + to.S + par_edge + (sum - max(dp[0][to.F], dp[1][to.F])) );
}
}
int main() {
megaRandom();
cin >> n;
for(int i = 1, x, y, z; i < n; i ++) {
cin >> x >> y >> z;
g[x].pb(mp(y, z));
g[y].pb(mp(x, z));
}
for(int root = 1; root <= n; root ++) {
memset(dp, 0, sizeof dp);
dfs(root);
res = max(res, dp[0][root]);
}
cout << res;
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... |