QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#857033 | #277. Beads and wires | makrav | 28 | 452ms | 4096kb | C++20 | 1.5kb | 2025-01-15 00:57:55 | 2025-01-15 00:57:56 |
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
const ll INF = 1'000'000'000'000'000'000;
void solve() {
int n; cin >> n;
vector<vector<pair<int, int>>> g(n);
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});
}
ll ans = 0;
for (int i = 0; i < n; i++) {
vector<array<ll, 2>> dp(n);
auto dfs = [&](int v, ll inc_w, int p, auto&&self) -> void {
dp[v][1] = inc_w;
ll bb = -INF, sm = 0;
for (auto [u, w] : g[v]) {
if (u != p) {
self(u, w, v, self);
bb = max(bb, dp[u][1] - dp[u][0]);
sm += dp[u][0];
}
}
dp[v][1] += sm;
dp[v][0] = max(dp[v][0], max(sm, bb + inc_w + sm));
};
dfs(i, -INF, i, dfs);
ans = max(ans, dp[i][0]);
}
cout << ans << '\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;
}
詳細信息
Subtask #1:
score: 13
Accepted
Test #1:
score: 13
Accepted
time: 1ms
memory: 3584kb
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: 1ms
memory: 3456kb
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: 3712kb
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: 1ms
memory: 3712kb
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: 3584kb
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: 13
Accepted
time: 1ms
memory: 3584kb
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:
62
result:
ok single line: '62'
Test #7:
score: 13
Accepted
time: 0ms
memory: 3584kb
input:
10 2 3 7 6 9 7 2 10 6 4 9 2 2 8 5 1 6 1 6 7 10 6 8 9 2 5 6
output:
41
result:
ok single line: '41'
Test #8:
score: 13
Accepted
time: 1ms
memory: 3712kb
input:
10 7 9 1 3 5 9 1 6 3 5 9 3 5 8 10 6 10 9 2 5 3 2 4 10 8 10 6
output:
53
result:
ok single line: '53'
Test #9:
score: 13
Accepted
time: 1ms
memory: 3712kb
input:
10 3 6 9 2 5 5 2 4 1 4 9 2 5 7 7 2 10 3 5 6 10 1 2 6 5 8 3
output:
43
result:
ok single line: '43'
Test #10:
score: 13
Accepted
time: 1ms
memory: 3584kb
input:
10 2 3 4 4 8 9 1 8 2 2 4 6 4 5 7 4 7 4 2 10 6 4 9 1 6 7 1
output:
39
result:
ok single line: '39'
Test #11:
score: 13
Accepted
time: 1ms
memory: 3712kb
input:
10 1 8 4 2 3 7 3 10 4 2 4 7 6 7 5 3 7 7 7 9 10 5 9 6 3 8 2
output:
48
result:
ok single line: '48'
Test #12:
score: 13
Accepted
time: 1ms
memory: 3712kb
input:
10 8 9 7 2 3 8 2 6 5 2 9 1 2 10 3 5 9 3 4 5 3 7 8 9 1 2 2
output:
35
result:
ok single line: '35'
Subtask #2:
score: 15
Accepted
Test #13:
score: 15
Accepted
time: 0ms
memory: 3712kb
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:
441
result:
ok single line: '441'
Test #14:
score: 15
Accepted
time: 1ms
memory: 3584kb
input:
100 38 97 8 26 29 9 62 73 7 13 62 8 6 63 3 22 82 9 57 90 10 34 40 10 86 87 6 62 72 8 35 67 4 24 47 10 14 60 9 43 92 4 85 91 1 56 62 5 57 69 6 30 45 8 69 92 5 15 99 1 6 78 9 64 89 1 20 88 8 66 92 2 31 63 4 31 70 8 42 79 2 11 59 2 9 97 6 24 62 8 8 21 9 84 100 7 4 50 10 54 63 5 24 98 10 2 97 1 4 25 3 2...
output:
498
result:
ok single line: '498'
Test #15:
score: 15
Accepted
time: 1ms
memory: 3584kb
input:
100 13 54 5 7 94 5 9 39 7 52 53 8 9 80 9 23 45 2 41 63 1 66 94 2 35 70 1 15 35 1 60 66 1 11 46 3 46 79 2 8 70 7 8 29 7 70 75 10 8 77 10 11 36 8 14 45 10 12 24 1 8 73 3 1 23 10 12 86 5 8 72 3 28 74 8 63 69 5 7 49 6 5 19 4 23 37 3 19 83 1 10 59 7 56 78 10 54 82 10 4 88 8 7 8 10 22 51 4 8 97 3 21 35 10...
output:
460
result:
ok single line: '460'
Test #16:
score: 15
Accepted
time: 0ms
memory: 3712kb
input:
200 147 182 33 101 186 14 7 66 10 73 180 33 43 130 36 130 166 5 20 23 48 1 148 5 43 49 12 20 104 20 27 53 41 71 124 39 21 123 39 44 200 24 18 25 46 17 38 11 48 144 49 22 126 7 57 153 12 96 124 39 18 126 40 28 134 35 15 87 16 43 75 44 129 157 28 75 143 14 12 104 9 57 128 50 107 153 7 68 174 15 20 163...
output:
4537
result:
ok single line: '4537'
Test #17:
score: 15
Accepted
time: 0ms
memory: 3712kb
input:
200 33 93 17 63 114 24 19 93 42 151 168 9 78 84 30 17 60 27 81 115 10 108 185 24 85 189 12 39 129 23 7 84 6 169 177 31 30 70 42 21 56 45 65 112 47 63 74 16 19 29 41 69 115 21 49 92 37 39 174 26 57 91 39 26 170 23 4 12 49 59 134 26 19 143 41 90 146 13 25 99 38 141 147 14 27 47 23 10 141 22 49 76 35 7...
output:
4605
result:
ok single line: '4605'
Test #18:
score: 15
Accepted
time: 1ms
memory: 3712kb
input:
200 25 94 45 143 166 19 24 103 16 133 200 43 39 45 40 100 113 35 99 189 41 127 131 33 107 136 33 34 40 22 63 66 13 43 170 47 9 160 9 7 10 40 6 168 19 45 78 39 48 133 46 94 111 41 3 183 6 84 198 45 56 160 15 110 152 17 50 53 50 75 76 43 36 143 12 83 176 44 124 174 16 26 96 33 73 118 42 48 188 8 8 139...
output:
4695
result:
ok single line: '4695'
Test #19:
score: 15
Accepted
time: 1ms
memory: 3712kb
input:
200 74 191 24 74 171 19 2 74 50 27 74 42 8 74 45 74 89 33 74 182 46 74 168 34 9 74 9 37 74 11 74 106 48 74 81 30 74 185 38 56 74 30 74 98 46 74 167 14 74 110 12 74 134 15 74 102 38 74 144 32 35 74 29 42 74 14 20 74 24 62 74 18 50 74 9 19 74 48 74 94 41 22 74 41 74 193 22 74 147 43 74 141 42 28 74 15...
output:
546
result:
ok single line: '546'
Test #20:
score: 15
Accepted
time: 1ms
memory: 3584kb
input:
200 98 200 12 12 87 47 19 98 31 9 87 14 61 87 18 98 189 24 26 98 8 42 98 42 130 192 22 34 192 7 98 167 28 105 192 24 98 188 40 37 98 17 155 192 15 63 98 28 98 139 23 14 192 10 45 98 38 98 172 44 98 146 43 75 87 35 98 116 26 98 165 41 87 171 14 87 150 9 11 192 14 46 98 29 192 198 14 98 176 25 80 192 ...
output:
233
result:
ok single line: '233'
Test #21:
score: 15
Accepted
time: 1ms
memory: 3584kb
input:
200 47 130 8 25 47 15 47 172 33 6 47 45 47 64 50 28 164 17 47 123 35 164 195 19 43 150 30 47 66 5 47 54 47 47 138 40 3 175 39 47 185 34 47 97 22 149 164 43 32 47 28 1 47 35 47 137 17 47 124 13 164 192 38 42 47 19 20 47 7 47 180 18 47 145 24 47 176 13 120 191 11 47 52 37 33 47 26 41 164 28 126 164 37...
output:
1202
result:
ok single line: '1202'
Test #22:
score: 15
Accepted
time: 1ms
memory: 3712kb
input:
200 56 174 43 28 56 22 26 112 21 56 119 44 56 139 36 29 113 25 7 63 44 10 66 32 96 182 44 56 136 17 56 103 41 9 95 48 118 138 23 18 56 39 21 177 49 8 56 32 22 69 39 25 84 10 82 146 10 19 56 42 149 153 40 80 89 12 11 56 41 140 195 11 38 56 44 122 181 47 65 150 29 56 106 18 43 75 40 84 193 44 56 127 1...
output:
3349
result:
ok single line: '3349'
Subtask #3:
score: 0
Time Limit Exceeded
Test #23:
score: 29
Accepted
time: 450ms
memory: 3968kb
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:
3848169
result:
ok single line: '3848169'
Test #24:
score: 29
Accepted
time: 445ms
memory: 4096kb
input:
5000 1926 2920 933 565 1093 993 1894 4373 930 1713 3978 916 2318 4827 999 2151 2951 984 227 1132 931 4454 4935 962 287 3086 920 1228 3395 997 2598 3601 988 2630 4017 972 489 3089 988 1120 2369 910 1965 4008 902 2799 2962 945 580 3947 965 477 1954 911 692 2334 932 1785 2113 940 2627 3908 970 3803 415...
output:
3789162
result:
ok single line: '3789162'
Test #25:
score: 29
Accepted
time: 452ms
memory: 3968kb
input:
5000 2488 4286 905 898 3460 995 3342 4660 963 38 1300 971 1608 4845 980 667 987 986 1297 3504 908 14 4515 975 1158 3753 965 2445 4021 994 3156 4046 928 848 2888 944 196 3340 955 1029 2200 929 1910 2338 967 903 4235 911 1178 3515 905 2139 2800 936 2616 4819 984 541 1777 930 1050 4535 917 421 2932 929...
output:
3818444
result:
ok single line: '3818444'
Test #26:
score: 0
Time Limit Exceeded
input:
10000 2751 6467 4918 3158 3563 4945 3261 6833 4929 2955 6313 4923 2546 5953 4970 87 4008 4926 4233 9238 4957 5579 6868 4908 1661 1837 4905 495 5863 4997 1757 7978 4995 4242 6106 4990 1531 5173 4952 7667 9651 4953 4615 8041 4950 1115 4959 4965 2536 5619 4911 8543 9472 4975 3423 8796 4915 2168 7691 49...
output:
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #32:
score: 0
Time Limit Exceeded
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 ...