이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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));
cout << L.size() << "\n";
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) for(int v=L[i]; v!=1 && !C[v]; v=P[v]) now += W[v], C[v] = 1;
R[cnt] = max(R[cnt], now + ToRoot);
for(int i=0; i<L.size(); i++) if(bit >> i & 1) for(int v=L[i]; v!=1 && C[v]; v=P[v]) C[v] = 0;
}
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++) 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";
}
}
컴파일 시 표준 에러 (stderr) 메시지
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:30:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i=0; i<L.size(); i++) if(bit >> i & 1) for(int v=L[i]; v!=1 && !C[v]; v=P[v]) now += W[v], C[v] = 1;
| ~^~~~~~~~~
designated_cities.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i=0; i<L.size(); i++) if(bit >> i & 1) for(int v=L[i]; v!=1 && C[v]; v=P[v]) C[v] = 0;
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |