#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll N, Q, P[202020], W[202020], C[202020], D[202020], R[202020], Sum, ToRoot;
vector<pair<ll,ll>> G[202020];
vector<int> L;
void DFS(int v, int b=-1){
for(auto [i,w] : G[v]){
if(i != b) P[i] = v, W[i] += w, DFS(i, v);
else ToRoot += w;
}
}
ll Solve1(int v, int b=-1){
ll res = 0;
for(auto [i,w] : G[v]) res += i != b ? Solve1(i, v) : w;
return res;
}
void GetAnswer(){
DFS(1);
for(int i=1; i<=N; i++) R[1] = max(R[1], Solve1(i));
for(int bit=0; bit<(1<<L.size()); bit++){
int cnt = __builtin_popcount(bit);
if(cnt < 2) continue;
ll now = 0;
for(int i=0; i<L.size(); i++){
if(~bit >> i & 1) continue;
for(int v=L[i]; v!=1 && !C[v]; v=P[v]) now += W[v], C[v] = 1;
for(int v=L[i]; v!=1 && C[v]; v=P[v]) C[v] = 0;
}
R[cnt] = max(R[cnt], now + ToRoot);
}
for(int i=L.size(); i<=N; i++) R[i] = Sum;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> N; memset(R, -1, sizeof R);
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);
}
for(int i=1; i<=N; i++) sort(G[i].begin(), G[i].end());
for(int i=1; i<=N; i++) if(G[i].size() == 1) L.push_back(i);
GetAnswer();
cin >> Q;
for(int i=1; i<=Q; i++){
int t; cin >> t;
cout << Sum - R[t] << "\n";
}
}
Compilation message
designated_cities.cpp: In function 'void DFS(int, int)':
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]){
| ^
designated_cities.cpp: In function 'll Solve1(int, int)':
designated_cities.cpp:18:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
18 | for(auto [i,w] : G[v]) res += i != b ? Solve1(i, v) : w;
| ^
designated_cities.cpp: In function 'void GetAnswer()':
designated_cities.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for(int i=0; i<L.size(); i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Incorrect |
5 ms |
6620 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Execution timed out |
2069 ms |
26756 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6616 KB |
Output is correct |
2 |
Execution timed out |
2062 ms |
27020 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Incorrect |
5 ms |
6620 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Execution timed out |
2069 ms |
26756 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Incorrect |
5 ms |
6620 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |