QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#376662 | #2169. 'S No Problem | willow# | WA | 1ms | 7952kb | C++17 | 2.0kb | 2024-04-04 14:47:22 | 2024-04-04 14:47:24 |
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);
}
cmax(dp[u][1], dp[u][0]);
cmax(dp[u][2], dp[u][1]);
}
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 7952kb
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: 6516kb
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: 0
Accepted
time: 1ms
memory: 6972kb
input:
6 1 3 1 3 2 1 3 4 10 5 4 1 4 6 1
output:
15
result:
ok single line: '15'
Test #4:
score: 0
Accepted
time: 0ms
memory: 6580kb
input:
6 1 3 1 2 3 1 3 5 1 4 5 1 5 6 1
output:
6
result:
ok single line: '6'
Test #5:
score: 0
Accepted
time: 1ms
memory: 6316kb
input:
6 1 2 5 3 2 3 2 4 1 4 5 2 4 6 4
output:
16
result:
ok single line: '16'
Test #6:
score: 0
Accepted
time: 1ms
memory: 6700kb
input:
6 1 6 8 2 6 2 3 6 3 4 6 5 5 6 1
output:
20
result:
ok single line: '20'
Test #7:
score: 0
Accepted
time: 0ms
memory: 7056kb
input:
7 6 4 60 4 2 463 6 7 165 6 3 81 6 1 30 4 5 214
output:
1103
result:
ok single line: '1103'
Test #8:
score: 0
Accepted
time: 1ms
memory: 6392kb
input:
7 1 3 463 1 5 214 7 2 165 7 4 81 7 1 60 7 6 30
output:
1103
result:
ok single line: '1103'
Test #9:
score: 0
Accepted
time: 1ms
memory: 6864kb
input:
8 8 7 10 1 3 1 3 2 1 6 5 2 5 4 1 3 7 4 7 5 3
output:
24
result:
ok single line: '24'
Test #10:
score: -100
Wrong Answer
time: 0ms
memory: 6884kb
input:
8 1 2 1 2 3 1 3 4 10 3 5 10 2 6 1 6 7 10 6 8 10
output:
54
result:
wrong answer 1st lines differ - expected: '46', found: '54'