QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#155388 | #2169. 'S No Problem | warwolf# | WA | 3ms | 6424kb | C++23 | 2.2kb | 2023-09-01 16:20:56 | 2023-09-01 16:20:58 |
Judging History
answer
#include <bits/stdc++.h>
#define maxn 100086
using namespace std;
int n;
vector<pair<int, int> > v[maxn];
int ans, f[maxn], g[maxn], h[maxn];
void dfs(int i, int fa){
int mx = 0, id = 0, sx = 0;
for(auto it : v[i]){
int to = it.first, co = it.second;
if(to == fa) continue;
dfs(to, i);
if(g[to] >= mx) sx = mx, mx = g[to], id = to;
else sx = max(sx, g[to]);
}
f[i] = mx;
int fmx = 0, hmx = 0, gmx = 0;
for(auto it : v[i]){
int to = it.first, co = it.second;
if(to == fa) continue;
ans = max(ans, g[i] + g[to]);
g[i] = max(g[i], g[to]);
int fval = f[to] + co;
int hval = h[to] + co;
ans = max(ans, fval + hmx);
ans = max(ans, fmx + hval);
gmx = max(gmx, hmx + hval);
fmx = max(fmx, fval);
hmx = max(hmx, hval);
}
g[i] = max(g[i], gmx);
for(auto it : v[i]){
int to = it.first, co = it.second;
if(to == fa) continue;
int fval = f[to] + co + (id == to ? sx : mx);
int hval = h[to] + co;
f[i] = max(f[i], fval);
h[i] = max(h[i], hval);
}
vector<pair<int, int> > a, b;
for(auto it : v[i]){
int to = it.first, co = it.second;
if(to == fa) continue;
h[to] += co;
a.push_back({h[to], to});
b.push_back({g[to], to});
}
sort(a.begin(), a.end(), greater<pair<int, int> >());
sort(b.begin(), b.end(), greater<pair<int, int> >());
set<int> st;
vector<int> w;
for(int j = 0;j < min((int)a.size(), 6);j++) st.insert(a[j].second);
for(int j = 0;j < min((int)b.size(), 6);j++) st.insert(b[j].second);
for(auto x : st) w.push_back(x);
for(int i = 0;i < w.size();i++) for(int j = 0;j < i;j++) for(int k = 0;k < j;k++){
ans = max(ans, h[w[i]] + h[w[j]] + g[w[k]]);
ans = max(ans, h[w[i]] + g[w[j]] + h[w[k]]);
ans = max(ans, g[w[i]] + h[w[j]] + h[w[k]]);
}
// printf("%d %d %d %d--\n", i, f[i], g[i], h[i]);
}
int main(){
scanf("%d", &n);
for(int i = 1;i < n;i++){
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
v[x].push_back({y, z});
v[y].push_back({x, z});
}
dfs(1, 0);
ans = max(ans, g[1]);
// printf("%d--\n", ans);
ans = -ans;
for(int i = 1;i <= n;i++) for(auto it : v[i]) ans += it.second;
printf("%d", ans);
}
/*
6
1 2 1
1 3 1
3 4 1
3 5 1
1 6 1
*/
詳細信息
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 6424kb
input:
5 1 2 1 1 3 2 1 4 3 1 5 4
output:
13
result:
wrong answer 1st lines differ - expected: '10', found: '13'