#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> ii;
struct edge { int to, cost, rev; };
vector<edge> adj[200005];
int degree[200005];
int flipDist[200005];
int total = 0;
bool gone[200005];
int ans[200005];
void dfs(int u, int p){
for(edge e : adj[u]){
if(e.to == p) continue;
total += e.cost;
flipDist[e.to] = flipDist[u] - e.cost + e.rev;
dfs(e.to, u);
}
}
ii findParent(int u){
for(edge e : adj[u]){
if(!gone[u]) return ii(e.rev, e.to);
}
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n;
for(int i = 1;i < n;i++){
int a, b, c, d; cin >> a >> b >> c >> d;
adj[a].push_back({b,c,d});
adj[b].push_back({a,d,c});
degree[a]++; degree[b]++;
}
///solve the E = 1 case
dfs(1,0);
ans[1] = total + *min_element(flipDist + 1, flipDist + n+1);
///Other cases
priority_queue<ii, vector<ii>, greater<ii> > leaves;
for(int i = 1;i <= n;i++){
if(degree[i] == 1) leaves.push(findParent(i));
}
while(leaves.size() > 2){
int u = leaves.top().second; int cost = leaves.top().first;
leaves.pop();
int p = findParent(u).second;
degree[p]--;
gone[u] = true;
if(degree[p] == 1){
leaves.push(ii(findParent(p).first + cost, p));
}
else{
ans[leaves.size()] = ans[leaves.size()+1] + cost;
}
}
int Q; cin >> Q;
while(Q--){
int x; cin >> x; cout << ans[x] << "\n";
}
}
Compilation message
designated_cities.cpp: In function 'ii findParent(long long int)':
designated_cities.cpp:28:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Incorrect |
7 ms |
5120 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
302 ms |
23656 KB |
Output is correct |
3 |
Correct |
269 ms |
29176 KB |
Output is correct |
4 |
Correct |
268 ms |
23528 KB |
Output is correct |
5 |
Correct |
284 ms |
24360 KB |
Output is correct |
6 |
Correct |
305 ms |
24812 KB |
Output is correct |
7 |
Correct |
250 ms |
25252 KB |
Output is correct |
8 |
Correct |
280 ms |
30072 KB |
Output is correct |
9 |
Correct |
207 ms |
27928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Incorrect |
290 ms |
23528 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Incorrect |
7 ms |
5120 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
302 ms |
23656 KB |
Output is correct |
3 |
Correct |
269 ms |
29176 KB |
Output is correct |
4 |
Correct |
268 ms |
23528 KB |
Output is correct |
5 |
Correct |
284 ms |
24360 KB |
Output is correct |
6 |
Correct |
305 ms |
24812 KB |
Output is correct |
7 |
Correct |
250 ms |
25252 KB |
Output is correct |
8 |
Correct |
280 ms |
30072 KB |
Output is correct |
9 |
Correct |
207 ms |
27928 KB |
Output is correct |
10 |
Correct |
7 ms |
4992 KB |
Output is correct |
11 |
Incorrect |
290 ms |
23528 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Incorrect |
7 ms |
5120 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |