#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define int long long
using namespace std;
typedef pair<int, int> ipair;
const int N = 2e5 + 5;
int n , q ,A , B , C , D;
ll dpD[N] , dpU[N] , cnt[N] ,was[N] , ans[N] ,dp[N];
struct Edge{
int v , c, d;
Edge(int v , int c , int d){this->v =v; this->c= c; this->d= d;}
};
int sumc , sum ;
vector<Edge> adj[N];
void dfsD(int u = 1, int p = 0){
for(Edge X : adj[u]){
int v = X.v;
if(v==p) continue;
dfsD(v , u);
dpD[u] += dpD[v] + X.c;
}
}
void dfsU(int u = 1 , int p = 0){
for(Edge X : adj[u]){
int v = X.v;
if(v==p) continue;
dpU[v] += dpU[u];
dpU[v] += dpD[u] - (dpD[v] + X.c) + X.d;
dfsU(v , u);
}
}
ipair Find(int u){
for(auto X : adj[u])if(!was[X.v]) return {X.v , X.d};
}
main (){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i = 1; i < n; ++i){
cin >> A >> B >> C >> D;
adj[A].emplace_back(Edge(B , C , D)); adj[B].emplace_back(Edge(A , D , C));
cnt[A]++; cnt[B]++;
}
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<pair<ll , int>> 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;
}
}
Compilation message
designated_cities.cpp:38:8: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
38 | main (){
| ^
designated_cities.cpp: In function 'ipair Find(long long 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 |
4992 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 |
561 ms |
30032 KB |
Output is correct |
3 |
Correct |
329 ms |
33016 KB |
Output is correct |
4 |
Correct |
479 ms |
30072 KB |
Output is correct |
5 |
Correct |
537 ms |
30624 KB |
Output is correct |
6 |
Correct |
537 ms |
31780 KB |
Output is correct |
7 |
Correct |
479 ms |
32156 KB |
Output is correct |
8 |
Correct |
329 ms |
35708 KB |
Output is correct |
9 |
Correct |
409 ms |
34740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
542 ms |
30328 KB |
Output is correct |
3 |
Correct |
350 ms |
41208 KB |
Output is correct |
4 |
Correct |
463 ms |
35064 KB |
Output is correct |
5 |
Correct |
508 ms |
37068 KB |
Output is correct |
6 |
Correct |
493 ms |
38448 KB |
Output is correct |
7 |
Correct |
424 ms |
41752 KB |
Output is correct |
8 |
Correct |
406 ms |
41640 KB |
Output is correct |
9 |
Correct |
386 ms |
40856 KB |
Output is correct |
# |
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 |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
10 ms |
5376 KB |
Output is correct |
14 |
Correct |
10 ms |
5376 KB |
Output is correct |
15 |
Correct |
10 ms |
5376 KB |
Output is correct |
16 |
Correct |
10 ms |
5376 KB |
Output is correct |
17 |
Correct |
10 ms |
5376 KB |
Output is correct |
18 |
Correct |
10 ms |
5376 KB |
Output is correct |
19 |
Correct |
10 ms |
5376 KB |
Output is correct |
20 |
Correct |
12 ms |
5376 KB |
Output is correct |
21 |
Correct |
10 ms |
5376 KB |
Output is correct |
22 |
Correct |
10 ms |
5376 KB |
Output is correct |
23 |
Correct |
10 ms |
5376 KB |
Output is correct |
24 |
Correct |
10 ms |
5504 KB |
Output is correct |
25 |
Correct |
10 ms |
5504 KB |
Output is correct |
26 |
Correct |
10 ms |
5504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
561 ms |
30032 KB |
Output is correct |
3 |
Correct |
329 ms |
33016 KB |
Output is correct |
4 |
Correct |
479 ms |
30072 KB |
Output is correct |
5 |
Correct |
537 ms |
30624 KB |
Output is correct |
6 |
Correct |
537 ms |
31780 KB |
Output is correct |
7 |
Correct |
479 ms |
32156 KB |
Output is correct |
8 |
Correct |
329 ms |
35708 KB |
Output is correct |
9 |
Correct |
409 ms |
34740 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
542 ms |
30328 KB |
Output is correct |
12 |
Correct |
350 ms |
41208 KB |
Output is correct |
13 |
Correct |
463 ms |
35064 KB |
Output is correct |
14 |
Correct |
508 ms |
37068 KB |
Output is correct |
15 |
Correct |
493 ms |
38448 KB |
Output is correct |
16 |
Correct |
424 ms |
41752 KB |
Output is correct |
17 |
Correct |
406 ms |
41640 KB |
Output is correct |
18 |
Correct |
386 ms |
40856 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
537 ms |
36472 KB |
Output is correct |
21 |
Correct |
344 ms |
41336 KB |
Output is correct |
22 |
Correct |
464 ms |
35204 KB |
Output is correct |
23 |
Correct |
530 ms |
36856 KB |
Output is correct |
24 |
Correct |
520 ms |
35680 KB |
Output is correct |
25 |
Correct |
502 ms |
36728 KB |
Output is correct |
26 |
Correct |
565 ms |
35708 KB |
Output is correct |
27 |
Correct |
523 ms |
37036 KB |
Output is correct |
28 |
Correct |
505 ms |
38136 KB |
Output is correct |
29 |
Correct |
516 ms |
36344 KB |
Output is correct |
30 |
Correct |
494 ms |
36648 KB |
Output is correct |
31 |
Correct |
447 ms |
39332 KB |
Output is correct |
32 |
Correct |
385 ms |
41976 KB |
Output is correct |
33 |
Correct |
369 ms |
41240 KB |
Output is correct |
# |
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 |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
561 ms |
30032 KB |
Output is correct |
14 |
Correct |
329 ms |
33016 KB |
Output is correct |
15 |
Correct |
479 ms |
30072 KB |
Output is correct |
16 |
Correct |
537 ms |
30624 KB |
Output is correct |
17 |
Correct |
537 ms |
31780 KB |
Output is correct |
18 |
Correct |
479 ms |
32156 KB |
Output is correct |
19 |
Correct |
329 ms |
35708 KB |
Output is correct |
20 |
Correct |
409 ms |
34740 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
542 ms |
30328 KB |
Output is correct |
23 |
Correct |
350 ms |
41208 KB |
Output is correct |
24 |
Correct |
463 ms |
35064 KB |
Output is correct |
25 |
Correct |
508 ms |
37068 KB |
Output is correct |
26 |
Correct |
493 ms |
38448 KB |
Output is correct |
27 |
Correct |
424 ms |
41752 KB |
Output is correct |
28 |
Correct |
406 ms |
41640 KB |
Output is correct |
29 |
Correct |
386 ms |
40856 KB |
Output is correct |
30 |
Correct |
3 ms |
5120 KB |
Output is correct |
31 |
Correct |
10 ms |
5376 KB |
Output is correct |
32 |
Correct |
10 ms |
5376 KB |
Output is correct |
33 |
Correct |
10 ms |
5376 KB |
Output is correct |
34 |
Correct |
10 ms |
5376 KB |
Output is correct |
35 |
Correct |
10 ms |
5376 KB |
Output is correct |
36 |
Correct |
10 ms |
5376 KB |
Output is correct |
37 |
Correct |
10 ms |
5376 KB |
Output is correct |
38 |
Correct |
12 ms |
5376 KB |
Output is correct |
39 |
Correct |
10 ms |
5376 KB |
Output is correct |
40 |
Correct |
10 ms |
5376 KB |
Output is correct |
41 |
Correct |
10 ms |
5376 KB |
Output is correct |
42 |
Correct |
10 ms |
5504 KB |
Output is correct |
43 |
Correct |
10 ms |
5504 KB |
Output is correct |
44 |
Correct |
10 ms |
5504 KB |
Output is correct |
45 |
Correct |
4 ms |
5120 KB |
Output is correct |
46 |
Correct |
537 ms |
36472 KB |
Output is correct |
47 |
Correct |
344 ms |
41336 KB |
Output is correct |
48 |
Correct |
464 ms |
35204 KB |
Output is correct |
49 |
Correct |
530 ms |
36856 KB |
Output is correct |
50 |
Correct |
520 ms |
35680 KB |
Output is correct |
51 |
Correct |
502 ms |
36728 KB |
Output is correct |
52 |
Correct |
565 ms |
35708 KB |
Output is correct |
53 |
Correct |
523 ms |
37036 KB |
Output is correct |
54 |
Correct |
505 ms |
38136 KB |
Output is correct |
55 |
Correct |
516 ms |
36344 KB |
Output is correct |
56 |
Correct |
494 ms |
36648 KB |
Output is correct |
57 |
Correct |
447 ms |
39332 KB |
Output is correct |
58 |
Correct |
385 ms |
41976 KB |
Output is correct |
59 |
Correct |
369 ms |
41240 KB |
Output is correct |
60 |
Correct |
4 ms |
5120 KB |
Output is correct |
61 |
Correct |
970 ms |
39236 KB |
Output is correct |
62 |
Correct |
807 ms |
41264 KB |
Output is correct |
63 |
Correct |
918 ms |
37884 KB |
Output is correct |
64 |
Correct |
976 ms |
39544 KB |
Output is correct |
65 |
Correct |
963 ms |
38264 KB |
Output is correct |
66 |
Correct |
999 ms |
39424 KB |
Output is correct |
67 |
Correct |
971 ms |
38300 KB |
Output is correct |
68 |
Correct |
988 ms |
39856 KB |
Output is correct |
69 |
Correct |
950 ms |
40824 KB |
Output is correct |
70 |
Correct |
946 ms |
38556 KB |
Output is correct |
71 |
Correct |
942 ms |
39592 KB |
Output is correct |
72 |
Correct |
934 ms |
42324 KB |
Output is correct |
73 |
Correct |
852 ms |
43388 KB |
Output is correct |
74 |
Correct |
851 ms |
45592 KB |
Output is correct |