QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#94028 | #2169. 'S No Problem | _skb_ | WA | 5ms | 8496kb | C++17 | 3.0kb | 2023-04-05 02:25:32 | 2023-04-05 02:25:36 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
struct debug {
#define contPrint { *this << "["; \
int f = 0; for(auto it : x) { *this << (f?", ":""); *this << it; f = 1;} \
*this << "]"; return *this;}
~debug(){cerr << endl;}
template<class c> debug& operator<<(c x) {cerr << x; return *this;}
template<class c, class d>
debug& operator<<(pair<c, d> x) {*this << "(" << x.first << ", " << x.second << ")";
return *this;}
template<class c> debug& operator<<(vector<c> x) contPrint;
#undef contPrint
};
#define dbg(x) "[" << #x << ": " << x << "] "
#define Wa() cerr << "[LINE: " << __LINE__ << "] -> "; debug() <<
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);
const int N = 1e5 + 5;
vector<pair<int, int>> G[N];
vector<array<int, 3>> W[N];
int dfs1(int cur, int p)
{
W[cur].push_back({0, 0});
W[cur].push_back({0, 0});
W[cur].push_back({0, 0});
W[cur].push_back({0, 0});
for(auto it : G[cur]) {
if(it.first != p) {
int w = dfs1(it.first, cur) + it.second;
W[cur].push_back({w, it.first, it.second});
}
}
sort(W[cur].rbegin(), W[cur].rend());
if(W[cur].size()) return (*W[cur].begin())[0];
return 0;
}
int ans = 0;
void dfs2(int cur, int one, int two)
{
for(auto [w, to, to_w] : W[cur]) {
if(to != 0) {
int next_one = one;
auto it = W[cur].begin();
if((*it)[1] == to) {
it++;
next_one = max(next_one, (*it)[0]);
} else {
next_one = max(next_one, (*it)[0]);
}
int next_two = two;
it = W[cur].begin();
if((*it)[1] == to) {
++it;
next_two = max(next_two, one + (*it)[0]);
} else {
next_two = max(next_two, one + (*it)[0]);
}
int sum = 0;
int c = 0;
it = W[cur].begin();
while(c < 2) {
if((*it)[1] != to) {
sum += (*it)[0];
c++;
}
it++;
}
next_two = max(next_two, sum);
dfs2(to, next_one + to_w, next_two);
}
}
auto it = W[cur].begin();
int sum = (*it)[0];
it++;
sum += (*it)[0];
ans = max({ans, sum + two, sum + one});
it++;
sum += (*it)[0];
ans = max(ans, sum + one);
it++;
sum += (*it)[0];
ans = max(ans, sum);
// Wa() dbg(cur) dbg(sum) dbg(one) dbg(two);
}
int main()
{
int tot = 0;
int n;
scanf("%d", &n);
for(int i = 1; i < n; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
G[u].push_back({v, w});
G[v].push_back({u, w});
tot += 2 * w;
}
dfs1(1, 1);
dfs2(1, 0, 0);
printf("%d\n", tot - ans);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 8228kb
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: 8316kb
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: 8312kb
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: 5ms
memory: 8240kb
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: 2ms
memory: 8248kb
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: 5ms
memory: 8280kb
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: 1ms
memory: 8484kb
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: 8496kb
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: 5ms
memory: 8484kb
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: 8312kb
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'