#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll N, Q, Sum, CostTo1, C[202020], R[202020];
vector<pair<ll,ll>> G[202020];
int S[202020], U[202020];
void TreeDP(int v, int b=-1, ll up=0, ll dw=0){
for(auto [i,w] : G[v]) if(i == b) CostTo1 += w, up += w;
C[v] = dw - up;
for(auto [i,w] : G[v]) if(i != b) TreeDP(i, v, up, dw+w);
}
ll CostToRoot(int root){
return CostTo1 + C[root];
}
vector<ll> CostFromRoot(int root){
priority_queue<ll> pq;
function<pair<ll,ll>(int,int)> dfs = [&](int v, int b) -> pair<ll,ll> {
vector<pair<ll,ll>> ch;
for(auto [i,w] : G[v]){
if(i == b || U[i]) continue;
auto [t1,t2] = dfs(i, v);
ch.emplace_back(t1 + w, i);
if(t2 > 0) ch.emplace_back(t2 + w, i);
}
if(ch.empty()) return {0LL, 0LL};
sort(ch.begin(), ch.end(), greater<>());
int mx2 = -1;
for(int i=1; i<ch.size(); i++) if(ch[i].second != ch[0].second) { mx2 = i; break; }
for(int i=1; i<ch.size(); i++) if(i != mx2) pq.push(ch[i].first);
return {ch[0].first, mx2 != -1 ? ch[mx2].first : 0LL};
};
auto mx = dfs(root, -1);
vector<ll> res = {mx.first, mx.second};
while(!pq.empty()) res.push_back(pq.top()), pq.pop();
return res;
}
int GetSize(int v, int b=-1){
S[v] = 1;
for(auto [i,w] : G[v]) if(i != b && !U[i]) S[v] += GetSize(i, v);
return S[v];
}
int GetCent(int v, int tot, int b=-1){
for(auto [i,w] : G[v]) if(i != b && !U[i] && S[i]*2 > tot) return GetCent(i, tot, v);
return v;
}
void GetAnswer(int v=1){
v = GetCent(v, GetSize(v));
auto vec = CostFromRoot(v); U[v] = 1;
if(vec[1] > 0){
ll now = CostToRoot(v) + vec[0];
for(int k=2; k<=N; k++){
if(k-1 < vec.size()) now += vec[k-1];
R[k] = min(R[k], Sum - now);
}
}
sort(vec.begin(), vec.end());
ll now = CostToRoot(v);
for(int k=2; k<=N; k++){
if(k-2 < vec.size()) now += vec[k-2];
R[k] = min(R[k], Sum - now);
}
for(auto [i,w] : G[v]) if(!U[i]) GetAnswer(i);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> N;
for(int i=1; i<N; i++){
int a, b, c, d; cin >> a >> b >> c >> d; Sum += c + d;
G[a].emplace_back(b, c); G[b].emplace_back(a, d);
}
TreeDP(1);
memset(R, 0x3f, sizeof R);
R[1] = Sum - CostTo1 - *max_element(C+1, C+N+1);
GetAnswer();
cin >> Q;
for(int i=1; i<=Q; i++){
int t; cin >> t;
cout << R[t] << "\n";
}
}
Compilation message
designated_cities.cpp: In function 'void TreeDP(int, int, ll, ll)':
designated_cities.cpp:10:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
10 | for(auto [i,w] : G[v]) if(i == b) CostTo1 += w, up += w;
| ^
designated_cities.cpp:12:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
12 | for(auto [i,w] : G[v]) if(i != b) TreeDP(i, v, up, dw+w);
| ^
designated_cities.cpp: In lambda function:
designated_cities.cpp:23:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
23 | for(auto [i,w] : G[v]){
| ^
designated_cities.cpp:25:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
25 | auto [t1,t2] = dfs(i, v);
| ^
designated_cities.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i=1; i<ch.size(); i++) if(ch[i].second != ch[0].second) { mx2 = i; break; }
| ~^~~~~~~~~~
designated_cities.cpp:33:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i=1; i<ch.size(); i++) if(i != mx2) pq.push(ch[i].first);
| ~^~~~~~~~~~
designated_cities.cpp: In function 'int GetSize(int, int)':
designated_cities.cpp:44:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | for(auto [i,w] : G[v]) if(i != b && !U[i]) S[v] += GetSize(i, v);
| ^
designated_cities.cpp: In function 'int GetCent(int, int, int)':
designated_cities.cpp:49:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
49 | for(auto [i,w] : G[v]) if(i != b && !U[i] && S[i]*2 > tot) return GetCent(i, tot, v);
| ^
designated_cities.cpp: In function 'void GetAnswer(int)':
designated_cities.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | if(k-1 < vec.size()) now += vec[k-1];
| ~~~~^~~~~~~~~~~~
designated_cities.cpp:66:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | if(k-2 < vec.size()) now += vec[k-2];
| ~~~~^~~~~~~~~~~~
designated_cities.cpp:69:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
69 | for(auto [i,w] : G[v]) if(!U[i]) GetAnswer(i);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Execution timed out |
2041 ms |
21376 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Execution timed out |
2041 ms |
21376 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
6612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |