#include <bits/stdc++.h>
using namespace std;
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define sz(v) (int)v.size()
template<typename T>
bool minimize(T& a, const T& b){
if(a > b){
return a = b, true;
}
return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b){
return a = b, true;
}
return false;
}
using ll = long long;
using ld = long double;
using ull = unsigned long long;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
#endif // LOCAL
int N;
cin >> N;
vector<vector<tuple<int, int, int>>> adj(N);
for(int i = 0; i < N - 1; ++i){
int u, v, c, d;
cin >> u >> v >> c >> d;
--u, --v;
adj[u].emplace_back(v, c, d);
adj[v].emplace_back(u, d, c);
}
vector<ll> f(N), g(N), up(N), down(N);
function<void(int, int)> dfs = [&](int u, int p){
for(auto [v, c, d] : adj[u]) if(v != p){
down[v] = down[u] + c;
up[v] = up[u] + d;
dfs(v, u);
f[u] += f[v] + c;
}
};
dfs(0, -1);
g[0] = f[0];
function<void(int, int)> reroot_dfs = [&](int u, int p){
for(auto [v, c, d] : adj[u]) if(v != p){
g[v] = g[u] - c + d;
reroot_dfs(v, u);
}
};
reroot_dfs(0, -1);
vector<long long> answer(N + 1, LLONG_MAX);
for(int i = 0; i < N; ++i){
minimize(answer[1], g[i]);
}
const long long inf = 1e18;
vector<pair<long long, int>> lowest(N, {-inf, -1});
tuple<long long, int, int> solution_for_two_leaves = {inf, -1, -1};
for(int u = 0; u < N; ++u){
if((int)adj[u].size() == 1){
lowest[u] = {down[u], u};
}
}
function<void(int, int)> dfs_dp = [&](int u, int p){
pair<long long, int> A = lowest[u], B = {-inf, -1};
for(auto [v, c, d] : adj[u]) if(v != p){
dfs_dp(v, u);
if(A < lowest[v]){
B = A;
A = lowest[v];
} else maximize(B, lowest[v]);
maximize(lowest[u], lowest[v]);
}
if(B.first != -inf){
long long delta = 2 * down[u] - A.first - B.first + up[u] - down[u];
int u = A.second, v = B.second;
minimize(solution_for_two_leaves, make_tuple(delta, u, v));
}
};
dfs_dp(0, -1);
long long delta;
int l1, l2;
tie(delta, l1, l2) = solution_for_two_leaves;
answer[2] = f[0] + delta;
int Q;
cin >> Q;
while(Q--){
int k;
cin >> k;
cout << answer[k] << '\n';
}
return 0;
}
Compilation message
designated_cities.cpp: In lambda function:
designated_cities.cpp:51:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
51 | for(auto [v, c, d] : adj[u]) if(v != p){
| ^
designated_cities.cpp: In lambda function:
designated_cities.cpp:63:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
63 | for(auto [v, c, d] : adj[u]) if(v != p){
| ^
designated_cities.cpp: In lambda function:
designated_cities.cpp:89:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
89 | for(auto [v, c, d] : adj[u]) if(v != p){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
121 ms |
24136 KB |
Output is correct |
3 |
Correct |
166 ms |
42572 KB |
Output is correct |
4 |
Correct |
121 ms |
24292 KB |
Output is correct |
5 |
Correct |
129 ms |
24156 KB |
Output is correct |
6 |
Correct |
145 ms |
26924 KB |
Output is correct |
7 |
Correct |
118 ms |
24060 KB |
Output is correct |
8 |
Correct |
159 ms |
43336 KB |
Output is correct |
9 |
Correct |
86 ms |
24572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
143 ms |
24288 KB |
Output is correct |
3 |
Correct |
159 ms |
52472 KB |
Output is correct |
4 |
Correct |
123 ms |
29208 KB |
Output is correct |
5 |
Correct |
126 ms |
30464 KB |
Output is correct |
6 |
Correct |
157 ms |
33864 KB |
Output is correct |
7 |
Correct |
95 ms |
30708 KB |
Output is correct |
8 |
Correct |
176 ms |
43080 KB |
Output is correct |
9 |
Correct |
90 ms |
30712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
121 ms |
24136 KB |
Output is correct |
3 |
Correct |
166 ms |
42572 KB |
Output is correct |
4 |
Correct |
121 ms |
24292 KB |
Output is correct |
5 |
Correct |
129 ms |
24156 KB |
Output is correct |
6 |
Correct |
145 ms |
26924 KB |
Output is correct |
7 |
Correct |
118 ms |
24060 KB |
Output is correct |
8 |
Correct |
159 ms |
43336 KB |
Output is correct |
9 |
Correct |
86 ms |
24572 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
143 ms |
24288 KB |
Output is correct |
12 |
Correct |
159 ms |
52472 KB |
Output is correct |
13 |
Correct |
123 ms |
29208 KB |
Output is correct |
14 |
Correct |
126 ms |
30464 KB |
Output is correct |
15 |
Correct |
157 ms |
33864 KB |
Output is correct |
16 |
Correct |
95 ms |
30708 KB |
Output is correct |
17 |
Correct |
176 ms |
43080 KB |
Output is correct |
18 |
Correct |
90 ms |
30712 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Incorrect |
134 ms |
30668 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |