QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#933348 | #2169. 'S No Problem | std_abs# | WA | 1ms | 6148kb | C++17 | 2.2kb | 2025-03-13 15:54:25 | 2025-03-13 15:54:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
#ifdef ABS
void db() {}
template <typename T>
ostream& operator << (ostream &o, vector <T> vec) {
o << "{"; int f = 0;
for (T i : vec) o << (f++ ? " " : "") << i;
return o << "}";
}
template <typename T, typename ...U> void db(T i, U ...j) { cerr << i << " ", db(j...); }
#define ppr(c, x...) cerr << "\e[1;" << c << "m" , db(__LINE__, "[" + string(#x) + "]", x), cerr << "\e[0m" << endl
#define bug(x...) ppr(32, x)
#define bugv(x...) ppr(36, vector(x))
#define safe ppr(33, "safe")
#else
#define bug(x...) void(0)
#define bugv(x...) void(0)
#define safe void(0)
#endif
const int mod = 998244353, N = 100007;
vector<pii> g[N];
int dp[N];
void dfs(int i, int p) {
dp[i] = 0;
for (auto [j, w] : g[i]) if (j != p) {
dfs(j, i);
dp[i] = max(dp[i], dp[j] + w);
}
}
int ans;
void dfs2(int i, int p, int mx, int mx2) {
array<int, 4> best({mx, 0, 0, 0});
for (auto [j, w] : g[i]) if (j != p) {
for (int _i = 0; _i < 4; ++_i) if (best[_i] < w + dp[j]) {
for (int _j = 3; _j > _i; --_j)
best[_j] = best[_j - 1];
best[_i] = w + dp[j];
break;
}
}
ans = max(ans, best[0] + best[1] + best[2] + best[3]);
ans = max(ans, (mx >= best[1] ? best[0] + best[1] + best[2] - mx : best[0] + best[1]) + mx2);
for (auto [j, w] : g[i]) if (j != p) {
int tmp = w + dp[j] == best[0] ? best[1] : best[0];
int tmp2 = max(mx2, w + dp[j] >= best[1] ? best[0] + best[1] + best[2] - w - dp[j] : best[0] + best[1]);
dfs2(j, i, tmp + w, tmp2);
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
int tot = 0;
for (int i = 0; i < n - 1; ++i) {
int x, y, z;
cin >> x >> y >> z;
--x, --y;
g[x].emplace_back(y, z), g[y].emplace_back(x, z);
tot += z;
}
dfs(0, -1);
//safe;
dfs2(0, -1, 0, 0);
cout << 2 * tot - ans << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 5948kb
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: 0ms
memory: 6144kb
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: 0ms
memory: 6016kb
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: 6148kb
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: 6016kb
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: 0ms
memory: 6048kb
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: 6144kb
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: 0ms
memory: 6088kb
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: 0ms
memory: 6016kb
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: 1ms
memory: 6016kb
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'