QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#631376 | #277. Beads and wires | makrav | 0 | 24ms | 9632kb | C++20 | 1.5kb | 2024-10-12 01:51:22 | 2024-10-12 01:51:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define sc second
#define int ll
void solve() {
int n; cin >> n;
vector<vector<pair<int, int>>> g(n);
int s = 0;
for (int i = 0; i < n - 1; i++) {
int u, v, w; cin >> u >> v >> w;
u--; v--;
g[u].pb({v, w});
g[v].pb({u, w});
s += w;
}
vector<vector<int>> dp(n, vector<int>(2));
// 0 - vertex, 1 - edge
auto dfs = [&](int v, int p, auto&&self) -> void {
int s0 = 0;
vector<int> dlts;
for (auto &[u, w] : g[v]) {
if (u != p) {
self(u, v, self);
int nd0 = min(dp[u][1], dp[u][0] + w);
int nd1 = dp[u][0];
s0 += nd0;
dlts.pb(nd1 - nd0);
}
}
sort(all(dlts));
dp[v][0] = min(s0, (dlts.size() < 2 ? s0 : s0 + dlts[0] + dlts[1]));
dp[v][1] = (dlts.empty() ? 2e9 + 1 : s0 + dlts[0]);
//cout << v << ' ' << dp[v][0] << ' ' << dp[v][1] << '\n';
};
dfs(0, 0, dfs);
cout << s - dp[0][0] << '\n';
}
signed main() {
int tt = 1;
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
cin >> tt;
#else
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#endif
while (tt--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 13
Accepted
time: 0ms
memory: 3552kb
input:
5 1 2 10 1 3 40 1 4 15 1 5 20
output:
60
result:
ok single line: '60'
Test #2:
score: 13
Accepted
time: 0ms
memory: 3612kb
input:
10 4 10 2 1 2 21 1 3 13 6 7 1 7 9 5 2 4 3 2 5 8 1 6 55 6 8 34
output:
140
result:
ok single line: '140'
Test #3:
score: 13
Accepted
time: 0ms
memory: 3588kb
input:
10 4 10 5 1 10 7 3 10 10 3 9 10 2 3 5 3 8 9 7 10 3 5 8 9 4 6 6
output:
61
result:
ok single line: '61'
Test #4:
score: 13
Accepted
time: 0ms
memory: 3552kb
input:
10 7 10 1 2 7 3 6 7 5 1 9 5 5 7 3 2 3 3 3 4 3 2 9 5 6 8 3
output:
30
result:
ok single line: '30'
Test #5:
score: 13
Accepted
time: 0ms
memory: 3860kb
input:
10 3 10 6 3 7 6 8 9 3 1 5 1 2 10 4 4 10 2 1 8 6 1 10 10 6 9 7
output:
44
result:
ok single line: '44'
Test #6:
score: 0
Wrong Answer
time: 0ms
memory: 3652kb
input:
10 5 6 9 2 3 5 1 10 8 4 5 9 2 7 8 5 7 10 6 9 4 2 8 9 1 7 5
output:
63
result:
wrong answer 1st lines differ - expected: '62', found: '63'
Subtask #2:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 0ms
memory: 3620kb
input:
100 48 93 4 12 87 1 27 72 10 3 43 2 33 86 2 37 80 8 51 98 4 34 57 3 5 51 7 15 100 9 41 72 4 14 30 10 27 80 7 50 71 7 6 77 6 36 66 1 47 61 2 71 92 7 20 94 7 3 59 1 55 69 7 16 33 5 71 79 6 18 96 4 4 43 3 1 55 4 65 75 10 82 96 2 30 74 9 12 19 6 46 93 1 83 94 9 22 24 1 27 51 9 33 92 4 25 42 10 71 81 8 3...
output:
471
result:
wrong answer 1st lines differ - expected: '441', found: '471'
Subtask #3:
score: 0
Wrong Answer
Test #23:
score: 0
Wrong Answer
time: 0ms
memory: 4508kb
input:
5000 2198 4992 964 2521 2711 961 3408 4585 975 2746 3304 974 2514 3411 972 602 4079 952 1193 3742 996 774 4530 942 2234 3877 905 346 1537 959 886 3841 937 3126 3342 938 211 2423 977 1725 4740 944 2125 4020 961 1235 3070 903 3109 4669 970 3923 4815 972 2525 4784 979 1159 1511 968 1900 3573 960 2087 2...
output:
4143163
result:
wrong answer 1st lines differ - expected: '3848169', found: '4143163'
Subtask #4:
score: 0
Wrong Answer
Test #32:
score: 0
Wrong Answer
time: 24ms
memory: 9632kb
input:
50000 22758 25880 990 917 25140 901 10537 30620 913 10368 14209 993 40077 48077 900 5064 20728 937 39641 39844 906 819 11079 965 12203 41474 993 13965 27792 923 20713 39385 991 4266 46545 938 27293 40442 937 12696 27693 961 10394 24153 998 24393 26653 926 25825 32799 963 33049 38343 946 15252 41731 ...
output:
41679589
result:
wrong answer 1st lines differ - expected: '38479584', found: '41679589'