QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#376658 | #2169. 'S No Problem | willow# | WA | 1ms | 7888kb | C++17 | 1.9kb | 2024-04-04 14:44:26 | 2024-04-04 14:44:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 100005;
int n, sum, dp[MAXN][3], ans;
vector< pair<int, int> > tree[MAXN];
pair<int, int> a[MAXN]; int tot;
inline void cmax(int &x, int y) {
if (x < y) x = y;
}
void DFS(int u, int fa) {
for (auto [v, w] : tree[u]) {
if (v == fa) continue;
DFS(v, u);
cmax(dp[u][0], dp[v][0] + w);
cmax(dp[u][1], dp[v][1]);
cmax(dp[u][2], dp[v][2] + w);
}
tot = 0;
for (auto [v, w] : tree[u]) {
if (v == fa) continue;
a[++tot] = make_pair(dp[v][0] + w, v);
}
sort(a + 1, a + tot + 1);
reverse(a + 1, a + tot + 1);
for (auto [v, w] : tree[u]) {
if (v == fa) continue;
if (tot >= 2) {
cmax(ans, dp[v][2] + w + (a[1].second == v? a[2].first : a[1].first));
cmax(dp[u][2], dp[v][1] + (a[1].second == v? a[2].first : a[1].first));
}
if (tot >= 3) {
int sum = a[1].first + a[2].first;
if (a[1].second == v) {
sum += a[3].first - a[1].first;
} else if (a[2].second == v) {
sum += a[3].first - a[2].first;
}
cmax(ans, dp[v][1] + sum);
}
}
if (tot >= 2) {
cmax(dp[u][1], a[1].first + a[2].first);
}
if (tot >= 3) {
cmax(dp[u][2], a[1].first + a[2].first + a[3].first);
}
if (tot >= 4) {
cmax(ans, a[1].first + a[2].first + a[3].first + a[4].first);
}
}
void solve() {
scanf("%d", &n);
for (int i = 1, u, v, w; i < n; ++i) {
scanf("%d %d %d", &u, &v, &w);
sum += w;
tree[u].push_back(make_pair(v, w));
tree[v].push_back(make_pair(u, w));
}
DFS(1, 0);
cmax(ans, dp[1][0]);
cmax(ans, dp[1][1]);
cmax(ans, dp[1][2]);
printf("%d\n", sum * 2 - ans);
}
int main() {
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6196kb
input:
5 1 2 1 1 3 2 1 4 3 1 5 4
output:
10
result:
ok single line: '10'
Test #2:
score: 0
Accepted
time: 1ms
memory: 7888kb
input:
6 6 2 1 4 6 1 3 1 1 1 5 1 1 6 10
output:
15
result:
ok single line: '15'
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 6196kb
input:
6 1 3 1 3 2 1 3 4 10 5 4 1 4 6 1
output:
16
result:
wrong answer 1st lines differ - expected: '15', found: '16'