#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
using namespace std;
typedef pair<int, int> ipair;
const int N = 2e5 + 5;
int n , q ,A[N] , B[N] , C[N] , D[N];
ll dpD[N] , dpU[N] , cnt[N] ,was[N] , ans[N];
vector<ipair> adj[N];
void dfsD(int u = 1, int p = 0){
for(ipair X : adj[u]){
int v = X.f , id = X.s;
if(v==p) continue;
dfsD(v , u);
int W = 0; if(A[id] == u) W = C[id]; else W = D[id];
dpD[u] += dpD[v] + W;
}
}
void dfsU(int u = 1 , int p = 0){
for(ipair X : adj[u]){
int v = X.f , id = X.s;
if(v==p) continue;
dpU[v] += dpU[u];
int W = 0; if(A[id] == v) W = C[id]; else W = D[id];
int E = 0; if(C[id] == W) E = D[id]; else E = C[id];
dpU[v] += dpD[u] - (dpD[v] + E) + W;
dfsU(v , u);
}
}
ipair Find(int u){
for(ipair X : adj[u]){
int W = 0; if(A[X.s] == X.f) W = C[X.s]; else W = D[X.s];
if(!was[X.f]) return {X.f , W};
}
}
int main (){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i = 1; i < n; ++i){
cin >> A[i] >> B[i] >> C[i] >> D[i];
adj[A[i]].emplace_back(B[i] , i); adj[B[i]].emplace_back(A[i] , i);
cnt[A[i]]++; cnt[B[i]]++;
}
dfsD(); dfsU();
//cout << ans[1] << " ans 1" << endl;
ans[1] = 1e18;
for(int i = 1; i <= n; ++i) ans[1] = min(ans[1] , dpD[i] + dpU[i]);
set<ipair> D;
for(int i = 1; i <= n; ++i) if(cnt[i]==1) D.insert({Find(i).s , i});
while(D.size() > 2){
int u = D.begin()->s , w = D.begin()->f;
D.erase(D.begin());
was[u] = 1;
int p = Find(u).f;
--cnt[p];
if(cnt[p] == 1){
int x = Find(p).s;
D.insert({w + x ,p});
} else ans[D.size()] = ans[D.size() + 1] + w;
}
//for(int i = 1; i <= n; ++i) cout << dpD[i] << " "; cerr << endl;
//for(int i = 1; i <= n; ++i) cout << dpU[i] << " "; cerr << endl;
int q;
cin >> q;
while(q--){
int k; cin >> k;
cout << ans[k] << endl;
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'ipair Find(int)':
designated_cities.cpp:37:1: warning: control reaches end of non-void function [-Wreturn-type]
37 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
510 ms |
32628 KB |
Output is correct |
3 |
Correct |
326 ms |
38264 KB |
Output is correct |
4 |
Correct |
481 ms |
31352 KB |
Output is correct |
5 |
Correct |
518 ms |
33264 KB |
Output is correct |
6 |
Correct |
507 ms |
34384 KB |
Output is correct |
7 |
Correct |
495 ms |
35304 KB |
Output is correct |
8 |
Correct |
334 ms |
40952 KB |
Output is correct |
9 |
Correct |
413 ms |
38372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Incorrect |
495 ms |
32668 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Incorrect |
11 ms |
5376 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
510 ms |
32628 KB |
Output is correct |
3 |
Correct |
326 ms |
38264 KB |
Output is correct |
4 |
Correct |
481 ms |
31352 KB |
Output is correct |
5 |
Correct |
518 ms |
33264 KB |
Output is correct |
6 |
Correct |
507 ms |
34384 KB |
Output is correct |
7 |
Correct |
495 ms |
35304 KB |
Output is correct |
8 |
Correct |
334 ms |
40952 KB |
Output is correct |
9 |
Correct |
413 ms |
38372 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Incorrect |
495 ms |
32668 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
4 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
510 ms |
32628 KB |
Output is correct |
14 |
Correct |
326 ms |
38264 KB |
Output is correct |
15 |
Correct |
481 ms |
31352 KB |
Output is correct |
16 |
Correct |
518 ms |
33264 KB |
Output is correct |
17 |
Correct |
507 ms |
34384 KB |
Output is correct |
18 |
Correct |
495 ms |
35304 KB |
Output is correct |
19 |
Correct |
334 ms |
40952 KB |
Output is correct |
20 |
Correct |
413 ms |
38372 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Incorrect |
495 ms |
32668 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |