#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];
int deg[N];
struct edge{
int v,c,d;
};
vector<edge> g[N];
void dfs(int u,int p){
for (auto i : g[u]) if (i.v != p){
dfs(i.v,u);
sum[u] += sum[i.v] + i.c;
}
}
void dfs1(int u,int 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++){
int 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:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5024 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
329 ms |
18044 KB |
Output is correct |
3 |
Correct |
208 ms |
26572 KB |
Output is correct |
4 |
Correct |
256 ms |
18004 KB |
Output is correct |
5 |
Correct |
278 ms |
18396 KB |
Output is correct |
6 |
Correct |
245 ms |
19668 KB |
Output is correct |
7 |
Correct |
253 ms |
19640 KB |
Output is correct |
8 |
Correct |
226 ms |
27496 KB |
Output is correct |
9 |
Correct |
191 ms |
21352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
276 ms |
17996 KB |
Output is correct |
3 |
Correct |
229 ms |
28660 KB |
Output is correct |
4 |
Correct |
265 ms |
18052 KB |
Output is correct |
5 |
Correct |
248 ms |
18400 KB |
Output is correct |
6 |
Correct |
270 ms |
19968 KB |
Output is correct |
7 |
Correct |
231 ms |
22248 KB |
Output is correct |
8 |
Correct |
254 ms |
24936 KB |
Output is correct |
9 |
Correct |
217 ms |
21380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5024 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
5 ms |
5164 KB |
Output is correct |
14 |
Correct |
4 ms |
5196 KB |
Output is correct |
15 |
Correct |
4 ms |
5196 KB |
Output is correct |
16 |
Correct |
5 ms |
5164 KB |
Output is correct |
17 |
Correct |
5 ms |
5196 KB |
Output is correct |
18 |
Correct |
5 ms |
5164 KB |
Output is correct |
19 |
Correct |
5 ms |
5236 KB |
Output is correct |
20 |
Correct |
7 ms |
5248 KB |
Output is correct |
21 |
Correct |
4 ms |
5160 KB |
Output is correct |
22 |
Correct |
6 ms |
5196 KB |
Output is correct |
23 |
Correct |
7 ms |
5196 KB |
Output is correct |
24 |
Correct |
6 ms |
5196 KB |
Output is correct |
25 |
Correct |
6 ms |
5292 KB |
Output is correct |
26 |
Correct |
5 ms |
5196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
329 ms |
18044 KB |
Output is correct |
3 |
Correct |
208 ms |
26572 KB |
Output is correct |
4 |
Correct |
256 ms |
18004 KB |
Output is correct |
5 |
Correct |
278 ms |
18396 KB |
Output is correct |
6 |
Correct |
245 ms |
19668 KB |
Output is correct |
7 |
Correct |
253 ms |
19640 KB |
Output is correct |
8 |
Correct |
226 ms |
27496 KB |
Output is correct |
9 |
Correct |
191 ms |
21352 KB |
Output is correct |
10 |
Correct |
3 ms |
5020 KB |
Output is correct |
11 |
Correct |
276 ms |
17996 KB |
Output is correct |
12 |
Correct |
229 ms |
28660 KB |
Output is correct |
13 |
Correct |
265 ms |
18052 KB |
Output is correct |
14 |
Correct |
248 ms |
18400 KB |
Output is correct |
15 |
Correct |
270 ms |
19968 KB |
Output is correct |
16 |
Correct |
231 ms |
22248 KB |
Output is correct |
17 |
Correct |
254 ms |
24936 KB |
Output is correct |
18 |
Correct |
217 ms |
21380 KB |
Output is correct |
19 |
Correct |
3 ms |
5020 KB |
Output is correct |
20 |
Correct |
269 ms |
18360 KB |
Output is correct |
21 |
Correct |
272 ms |
29248 KB |
Output is correct |
22 |
Correct |
266 ms |
18456 KB |
Output is correct |
23 |
Correct |
336 ms |
18452 KB |
Output is correct |
24 |
Correct |
340 ms |
18432 KB |
Output is correct |
25 |
Correct |
313 ms |
18420 KB |
Output is correct |
26 |
Correct |
365 ms |
18428 KB |
Output is correct |
27 |
Correct |
319 ms |
18428 KB |
Output is correct |
28 |
Correct |
284 ms |
20048 KB |
Output is correct |
29 |
Correct |
302 ms |
18256 KB |
Output is correct |
30 |
Correct |
257 ms |
19136 KB |
Output is correct |
31 |
Correct |
254 ms |
21144 KB |
Output is correct |
32 |
Correct |
253 ms |
25688 KB |
Output is correct |
33 |
Correct |
211 ms |
21784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5024 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
329 ms |
18044 KB |
Output is correct |
14 |
Correct |
208 ms |
26572 KB |
Output is correct |
15 |
Correct |
256 ms |
18004 KB |
Output is correct |
16 |
Correct |
278 ms |
18396 KB |
Output is correct |
17 |
Correct |
245 ms |
19668 KB |
Output is correct |
18 |
Correct |
253 ms |
19640 KB |
Output is correct |
19 |
Correct |
226 ms |
27496 KB |
Output is correct |
20 |
Correct |
191 ms |
21352 KB |
Output is correct |
21 |
Correct |
3 ms |
5020 KB |
Output is correct |
22 |
Correct |
276 ms |
17996 KB |
Output is correct |
23 |
Correct |
229 ms |
28660 KB |
Output is correct |
24 |
Correct |
265 ms |
18052 KB |
Output is correct |
25 |
Correct |
248 ms |
18400 KB |
Output is correct |
26 |
Correct |
270 ms |
19968 KB |
Output is correct |
27 |
Correct |
231 ms |
22248 KB |
Output is correct |
28 |
Correct |
254 ms |
24936 KB |
Output is correct |
29 |
Correct |
217 ms |
21380 KB |
Output is correct |
30 |
Correct |
3 ms |
4940 KB |
Output is correct |
31 |
Correct |
5 ms |
5164 KB |
Output is correct |
32 |
Correct |
4 ms |
5196 KB |
Output is correct |
33 |
Correct |
4 ms |
5196 KB |
Output is correct |
34 |
Correct |
5 ms |
5164 KB |
Output is correct |
35 |
Correct |
5 ms |
5196 KB |
Output is correct |
36 |
Correct |
5 ms |
5164 KB |
Output is correct |
37 |
Correct |
5 ms |
5236 KB |
Output is correct |
38 |
Correct |
7 ms |
5248 KB |
Output is correct |
39 |
Correct |
4 ms |
5160 KB |
Output is correct |
40 |
Correct |
6 ms |
5196 KB |
Output is correct |
41 |
Correct |
7 ms |
5196 KB |
Output is correct |
42 |
Correct |
6 ms |
5196 KB |
Output is correct |
43 |
Correct |
6 ms |
5292 KB |
Output is correct |
44 |
Correct |
5 ms |
5196 KB |
Output is correct |
45 |
Correct |
3 ms |
5020 KB |
Output is correct |
46 |
Correct |
269 ms |
18360 KB |
Output is correct |
47 |
Correct |
272 ms |
29248 KB |
Output is correct |
48 |
Correct |
266 ms |
18456 KB |
Output is correct |
49 |
Correct |
336 ms |
18452 KB |
Output is correct |
50 |
Correct |
340 ms |
18432 KB |
Output is correct |
51 |
Correct |
313 ms |
18420 KB |
Output is correct |
52 |
Correct |
365 ms |
18428 KB |
Output is correct |
53 |
Correct |
319 ms |
18428 KB |
Output is correct |
54 |
Correct |
284 ms |
20048 KB |
Output is correct |
55 |
Correct |
302 ms |
18256 KB |
Output is correct |
56 |
Correct |
257 ms |
19136 KB |
Output is correct |
57 |
Correct |
254 ms |
21144 KB |
Output is correct |
58 |
Correct |
253 ms |
25688 KB |
Output is correct |
59 |
Correct |
211 ms |
21784 KB |
Output is correct |
60 |
Correct |
4 ms |
5024 KB |
Output is correct |
61 |
Correct |
381 ms |
19668 KB |
Output is correct |
62 |
Correct |
275 ms |
27440 KB |
Output is correct |
63 |
Correct |
325 ms |
19612 KB |
Output is correct |
64 |
Correct |
289 ms |
19684 KB |
Output is correct |
65 |
Correct |
363 ms |
19456 KB |
Output is correct |
66 |
Correct |
300 ms |
19612 KB |
Output is correct |
67 |
Correct |
281 ms |
19564 KB |
Output is correct |
68 |
Correct |
323 ms |
19920 KB |
Output is correct |
69 |
Correct |
348 ms |
21084 KB |
Output is correct |
70 |
Correct |
273 ms |
18916 KB |
Output is correct |
71 |
Correct |
276 ms |
20724 KB |
Output is correct |
72 |
Correct |
275 ms |
22136 KB |
Output is correct |
73 |
Correct |
255 ms |
25552 KB |
Output is correct |
74 |
Correct |
231 ms |
24784 KB |
Output is correct |