#include <bits/stdc++.h>
#define ll long long
#define ld double
#define fs first
#define sc second
#define debug(y) cout<<y,exit(0)
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 2e5 + 9;
const ll inf = 1e18 + 7;
ll n,sum[N],res[N],deg[N];
struct edge{
ll v,c,d;
};
vector<edge> g[N];
void dfs(ll u,ll p){
for (auto i : g[u]) if (i.v != p){
dfs(i.v,u);
sum[u] += sum[i.v] + i.c;
}
}
void dfs1(ll u,ll p,ll s){
res[1] = min(res[1],s);
for (auto i : g[u])
if (i.v != p) dfs1(i.v,u,s - i.c + i.d);
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define task "tst"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
//freopen(task".out","w",stdout);
}
cin>>n;
for (ll i = 1;i < n;i++){
ll u,v,c,d; cin>>u>>v>>c>>d;
g[u].push_back({v,c,d}); g[v].push_back({u,d,c});
deg[u]++; deg[v]++;
}
res[1] = inf;
dfs(1,0); dfs1(1,0,sum[1]);
priority_queue<LL,vector<LL>,greater<LL>> pq;
for (ll i = 1;i <= n;i++)
if (g[i].size() == 1) pq.push({g[i][0].d,i});
//return 0;
while(pq.size() > 2){
LL now = pq.top(); pq.pop();
ll nxt = -1;
for (auto i : g[now.sc]){
if (deg[i.v] > 1){
deg[i.v]--;
if (deg[i.v] == 1) nxt = i.v;
else res[pq.size()] = res[pq.size() + 1] + now.fs;
}
}
if (nxt > 0){
for (auto i : g[nxt])
if (deg[i.v] > 1) pq.push({now.fs + i.d,nxt});
}
}
ll Q; cin>>Q;
while(Q--){
ll x; cin>>x; cout<<res[x]<<"\n";
}
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5016 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
278 ms |
29700 KB |
Output is correct |
3 |
Correct |
254 ms |
40400 KB |
Output is correct |
4 |
Correct |
314 ms |
28408 KB |
Output is correct |
5 |
Correct |
284 ms |
30604 KB |
Output is correct |
6 |
Correct |
292 ms |
31740 KB |
Output is correct |
7 |
Correct |
267 ms |
31392 KB |
Output is correct |
8 |
Correct |
250 ms |
41276 KB |
Output is correct |
9 |
Correct |
225 ms |
32844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5028 KB |
Output is correct |
2 |
Correct |
294 ms |
29760 KB |
Output is correct |
3 |
Correct |
277 ms |
42488 KB |
Output is correct |
4 |
Correct |
306 ms |
28448 KB |
Output is correct |
5 |
Correct |
305 ms |
30580 KB |
Output is correct |
6 |
Correct |
326 ms |
32060 KB |
Output is correct |
7 |
Correct |
206 ms |
33900 KB |
Output is correct |
8 |
Correct |
262 ms |
37592 KB |
Output is correct |
9 |
Correct |
189 ms |
32388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5016 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5028 KB |
Output is correct |
13 |
Correct |
5 ms |
5296 KB |
Output is correct |
14 |
Correct |
4 ms |
5296 KB |
Output is correct |
15 |
Correct |
4 ms |
5196 KB |
Output is correct |
16 |
Correct |
4 ms |
5324 KB |
Output is correct |
17 |
Correct |
6 ms |
5304 KB |
Output is correct |
18 |
Correct |
4 ms |
5324 KB |
Output is correct |
19 |
Correct |
4 ms |
5288 KB |
Output is correct |
20 |
Correct |
4 ms |
5300 KB |
Output is correct |
21 |
Correct |
5 ms |
5332 KB |
Output is correct |
22 |
Correct |
4 ms |
5204 KB |
Output is correct |
23 |
Correct |
4 ms |
5328 KB |
Output is correct |
24 |
Correct |
4 ms |
5324 KB |
Output is correct |
25 |
Correct |
4 ms |
5324 KB |
Output is correct |
26 |
Correct |
4 ms |
5296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
278 ms |
29700 KB |
Output is correct |
3 |
Correct |
254 ms |
40400 KB |
Output is correct |
4 |
Correct |
314 ms |
28408 KB |
Output is correct |
5 |
Correct |
284 ms |
30604 KB |
Output is correct |
6 |
Correct |
292 ms |
31740 KB |
Output is correct |
7 |
Correct |
267 ms |
31392 KB |
Output is correct |
8 |
Correct |
250 ms |
41276 KB |
Output is correct |
9 |
Correct |
225 ms |
32844 KB |
Output is correct |
10 |
Correct |
2 ms |
5028 KB |
Output is correct |
11 |
Correct |
294 ms |
29760 KB |
Output is correct |
12 |
Correct |
277 ms |
42488 KB |
Output is correct |
13 |
Correct |
306 ms |
28448 KB |
Output is correct |
14 |
Correct |
305 ms |
30580 KB |
Output is correct |
15 |
Correct |
326 ms |
32060 KB |
Output is correct |
16 |
Correct |
206 ms |
33900 KB |
Output is correct |
17 |
Correct |
262 ms |
37592 KB |
Output is correct |
18 |
Correct |
189 ms |
32388 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
292 ms |
29792 KB |
Output is correct |
21 |
Correct |
266 ms |
42680 KB |
Output is correct |
22 |
Correct |
266 ms |
28416 KB |
Output is correct |
23 |
Correct |
284 ms |
30140 KB |
Output is correct |
24 |
Correct |
288 ms |
28912 KB |
Output is correct |
25 |
Correct |
289 ms |
30016 KB |
Output is correct |
26 |
Correct |
286 ms |
28876 KB |
Output is correct |
27 |
Correct |
285 ms |
30124 KB |
Output is correct |
28 |
Correct |
278 ms |
31652 KB |
Output is correct |
29 |
Correct |
322 ms |
30400 KB |
Output is correct |
30 |
Correct |
267 ms |
29684 KB |
Output is correct |
31 |
Correct |
247 ms |
32352 KB |
Output is correct |
32 |
Correct |
262 ms |
38072 KB |
Output is correct |
33 |
Correct |
189 ms |
32744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5016 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
278 ms |
29700 KB |
Output is correct |
14 |
Correct |
254 ms |
40400 KB |
Output is correct |
15 |
Correct |
314 ms |
28408 KB |
Output is correct |
16 |
Correct |
284 ms |
30604 KB |
Output is correct |
17 |
Correct |
292 ms |
31740 KB |
Output is correct |
18 |
Correct |
267 ms |
31392 KB |
Output is correct |
19 |
Correct |
250 ms |
41276 KB |
Output is correct |
20 |
Correct |
225 ms |
32844 KB |
Output is correct |
21 |
Correct |
2 ms |
5028 KB |
Output is correct |
22 |
Correct |
294 ms |
29760 KB |
Output is correct |
23 |
Correct |
277 ms |
42488 KB |
Output is correct |
24 |
Correct |
306 ms |
28448 KB |
Output is correct |
25 |
Correct |
305 ms |
30580 KB |
Output is correct |
26 |
Correct |
326 ms |
32060 KB |
Output is correct |
27 |
Correct |
206 ms |
33900 KB |
Output is correct |
28 |
Correct |
262 ms |
37592 KB |
Output is correct |
29 |
Correct |
189 ms |
32388 KB |
Output is correct |
30 |
Correct |
3 ms |
5028 KB |
Output is correct |
31 |
Correct |
5 ms |
5296 KB |
Output is correct |
32 |
Correct |
4 ms |
5296 KB |
Output is correct |
33 |
Correct |
4 ms |
5196 KB |
Output is correct |
34 |
Correct |
4 ms |
5324 KB |
Output is correct |
35 |
Correct |
6 ms |
5304 KB |
Output is correct |
36 |
Correct |
4 ms |
5324 KB |
Output is correct |
37 |
Correct |
4 ms |
5288 KB |
Output is correct |
38 |
Correct |
4 ms |
5300 KB |
Output is correct |
39 |
Correct |
5 ms |
5332 KB |
Output is correct |
40 |
Correct |
4 ms |
5204 KB |
Output is correct |
41 |
Correct |
4 ms |
5328 KB |
Output is correct |
42 |
Correct |
4 ms |
5324 KB |
Output is correct |
43 |
Correct |
4 ms |
5324 KB |
Output is correct |
44 |
Correct |
4 ms |
5296 KB |
Output is correct |
45 |
Correct |
2 ms |
4940 KB |
Output is correct |
46 |
Correct |
292 ms |
29792 KB |
Output is correct |
47 |
Correct |
266 ms |
42680 KB |
Output is correct |
48 |
Correct |
266 ms |
28416 KB |
Output is correct |
49 |
Correct |
284 ms |
30140 KB |
Output is correct |
50 |
Correct |
288 ms |
28912 KB |
Output is correct |
51 |
Correct |
289 ms |
30016 KB |
Output is correct |
52 |
Correct |
286 ms |
28876 KB |
Output is correct |
53 |
Correct |
285 ms |
30124 KB |
Output is correct |
54 |
Correct |
278 ms |
31652 KB |
Output is correct |
55 |
Correct |
322 ms |
30400 KB |
Output is correct |
56 |
Correct |
267 ms |
29684 KB |
Output is correct |
57 |
Correct |
247 ms |
32352 KB |
Output is correct |
58 |
Correct |
262 ms |
38072 KB |
Output is correct |
59 |
Correct |
189 ms |
32744 KB |
Output is correct |
60 |
Correct |
2 ms |
4940 KB |
Output is correct |
61 |
Correct |
319 ms |
32384 KB |
Output is correct |
62 |
Correct |
273 ms |
42092 KB |
Output is correct |
63 |
Correct |
305 ms |
30956 KB |
Output is correct |
64 |
Correct |
313 ms |
32596 KB |
Output is correct |
65 |
Correct |
303 ms |
31148 KB |
Output is correct |
66 |
Correct |
295 ms |
32592 KB |
Output is correct |
67 |
Correct |
303 ms |
31160 KB |
Output is correct |
68 |
Correct |
311 ms |
32836 KB |
Output is correct |
69 |
Correct |
302 ms |
34000 KB |
Output is correct |
70 |
Correct |
324 ms |
32364 KB |
Output is correct |
71 |
Correct |
299 ms |
32416 KB |
Output is correct |
72 |
Correct |
267 ms |
34848 KB |
Output is correct |
73 |
Correct |
284 ms |
39240 KB |
Output is correct |
74 |
Correct |
225 ms |
36964 KB |
Output is correct |