#include <bits/stdc++.h>
#define long long long
#define pii pair<long, long>
#define x first
#define y second
using namespace std;
const int N = 2e5+5;
struct Edge {
int v;
long c, d;
Edge(int v, long c, long d) : v(v), c(c), d(d) {}
};
int n, q, deg[N];
vector<Edge> g[N];
long dwn[N], ans[N];
void pre_process(int u, int p) {
for(Edge v : g[u]) if(v.v != p) {
pre_process(v.v, u);
dwn[u] += v.c + dwn[v.v];
}
}
void dfs(int u, int p, long sum) {
ans[1] = min(ans[1], sum);
for(Edge v : g[u]) if(v.v != p) {
sum -= v.c, sum += v.d;
dfs(v.v, u, sum);
sum += v.c, sum -= v.d;
}
}
int main() {
ans[1] = 1e18;
scanf("%d", &n);
for(int i = 1, a, b, c, d; i < n; i++) {
scanf("%d %d %d %d", &a, &b, &c, &d);
g[a].emplace_back(b, c, d);
g[b].emplace_back(a, d, c);
++deg[a], ++deg[b];
}
pre_process(1, 0), dfs(1, 0, dwn[1]);
priority_queue<pii, vector<pii>, greater<pii> > Q;
for(int i = 1; i <= n; i++) if(deg[i] == 1)
Q.emplace(g[i][0].d, i);
while(Q.size() > 2) {
pii now = Q.top(); Q.pop();
int u = now.y; long dist = now.x;
int nex = -1;
for(Edge v : g[u]) if(deg[v.v] > 1) {
--deg[v.v];
if(deg[v.v] == 1) nex = v.v;
else ans[Q.size()] = ans[Q.size() + 1] + dist;
}
if(nex != -1) for(Edge v : g[nex]) if(deg[v.v] > 1)
Q.emplace(dist + v.d, nex);
}
scanf("%d", &q);
for(int i = 1, a; i <= q; i++) {
scanf("%d", &a);
printf("%lld\n", ans[a]);
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
designated_cities.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d", &a, &b, &c, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
designated_cities.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a);
~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
7 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
5112 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
4984 KB |
Output is correct |
9 |
Correct |
8 ms |
4984 KB |
Output is correct |
10 |
Correct |
8 ms |
4984 KB |
Output is correct |
11 |
Correct |
7 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
430 ms |
22636 KB |
Output is correct |
3 |
Correct |
360 ms |
33400 KB |
Output is correct |
4 |
Correct |
402 ms |
22640 KB |
Output is correct |
5 |
Correct |
412 ms |
23456 KB |
Output is correct |
6 |
Correct |
419 ms |
24684 KB |
Output is correct |
7 |
Correct |
370 ms |
24360 KB |
Output is correct |
8 |
Correct |
384 ms |
34168 KB |
Output is correct |
9 |
Correct |
290 ms |
25624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
427 ms |
22700 KB |
Output is correct |
3 |
Correct |
356 ms |
35448 KB |
Output is correct |
4 |
Correct |
397 ms |
22640 KB |
Output is correct |
5 |
Correct |
406 ms |
23528 KB |
Output is correct |
6 |
Correct |
424 ms |
24936 KB |
Output is correct |
7 |
Correct |
314 ms |
27048 KB |
Output is correct |
8 |
Correct |
391 ms |
30440 KB |
Output is correct |
9 |
Correct |
295 ms |
25768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
7 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
5112 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
4984 KB |
Output is correct |
9 |
Correct |
8 ms |
4984 KB |
Output is correct |
10 |
Correct |
8 ms |
4984 KB |
Output is correct |
11 |
Correct |
7 ms |
4984 KB |
Output is correct |
12 |
Correct |
8 ms |
4984 KB |
Output is correct |
13 |
Correct |
11 ms |
5368 KB |
Output is correct |
14 |
Correct |
10 ms |
5368 KB |
Output is correct |
15 |
Correct |
10 ms |
5368 KB |
Output is correct |
16 |
Correct |
10 ms |
5368 KB |
Output is correct |
17 |
Correct |
10 ms |
5368 KB |
Output is correct |
18 |
Correct |
10 ms |
5240 KB |
Output is correct |
19 |
Correct |
10 ms |
5240 KB |
Output is correct |
20 |
Correct |
10 ms |
5368 KB |
Output is correct |
21 |
Correct |
11 ms |
5368 KB |
Output is correct |
22 |
Correct |
10 ms |
5368 KB |
Output is correct |
23 |
Correct |
10 ms |
5368 KB |
Output is correct |
24 |
Correct |
11 ms |
5368 KB |
Output is correct |
25 |
Correct |
10 ms |
5368 KB |
Output is correct |
26 |
Correct |
11 ms |
5368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
430 ms |
22636 KB |
Output is correct |
3 |
Correct |
360 ms |
33400 KB |
Output is correct |
4 |
Correct |
402 ms |
22640 KB |
Output is correct |
5 |
Correct |
412 ms |
23456 KB |
Output is correct |
6 |
Correct |
419 ms |
24684 KB |
Output is correct |
7 |
Correct |
370 ms |
24360 KB |
Output is correct |
8 |
Correct |
384 ms |
34168 KB |
Output is correct |
9 |
Correct |
290 ms |
25624 KB |
Output is correct |
10 |
Correct |
8 ms |
4984 KB |
Output is correct |
11 |
Correct |
427 ms |
22700 KB |
Output is correct |
12 |
Correct |
356 ms |
35448 KB |
Output is correct |
13 |
Correct |
397 ms |
22640 KB |
Output is correct |
14 |
Correct |
406 ms |
23528 KB |
Output is correct |
15 |
Correct |
424 ms |
24936 KB |
Output is correct |
16 |
Correct |
314 ms |
27048 KB |
Output is correct |
17 |
Correct |
391 ms |
30440 KB |
Output is correct |
18 |
Correct |
295 ms |
25768 KB |
Output is correct |
19 |
Correct |
8 ms |
4984 KB |
Output is correct |
20 |
Correct |
449 ms |
29016 KB |
Output is correct |
21 |
Correct |
368 ms |
41848 KB |
Output is correct |
22 |
Correct |
411 ms |
27632 KB |
Output is correct |
23 |
Correct |
447 ms |
29336 KB |
Output is correct |
24 |
Correct |
425 ms |
28140 KB |
Output is correct |
25 |
Correct |
443 ms |
29288 KB |
Output is correct |
26 |
Correct |
448 ms |
28136 KB |
Output is correct |
27 |
Correct |
428 ms |
29352 KB |
Output is correct |
28 |
Correct |
435 ms |
31084 KB |
Output is correct |
29 |
Correct |
448 ms |
29676 KB |
Output is correct |
30 |
Correct |
414 ms |
28936 KB |
Output is correct |
31 |
Correct |
361 ms |
31648 KB |
Output is correct |
32 |
Correct |
401 ms |
37360 KB |
Output is correct |
33 |
Correct |
297 ms |
32452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
7 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
5112 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
4984 KB |
Output is correct |
9 |
Correct |
8 ms |
4984 KB |
Output is correct |
10 |
Correct |
8 ms |
4984 KB |
Output is correct |
11 |
Correct |
7 ms |
4984 KB |
Output is correct |
12 |
Correct |
8 ms |
4984 KB |
Output is correct |
13 |
Correct |
430 ms |
22636 KB |
Output is correct |
14 |
Correct |
360 ms |
33400 KB |
Output is correct |
15 |
Correct |
402 ms |
22640 KB |
Output is correct |
16 |
Correct |
412 ms |
23456 KB |
Output is correct |
17 |
Correct |
419 ms |
24684 KB |
Output is correct |
18 |
Correct |
370 ms |
24360 KB |
Output is correct |
19 |
Correct |
384 ms |
34168 KB |
Output is correct |
20 |
Correct |
290 ms |
25624 KB |
Output is correct |
21 |
Correct |
8 ms |
4984 KB |
Output is correct |
22 |
Correct |
427 ms |
22700 KB |
Output is correct |
23 |
Correct |
356 ms |
35448 KB |
Output is correct |
24 |
Correct |
397 ms |
22640 KB |
Output is correct |
25 |
Correct |
406 ms |
23528 KB |
Output is correct |
26 |
Correct |
424 ms |
24936 KB |
Output is correct |
27 |
Correct |
314 ms |
27048 KB |
Output is correct |
28 |
Correct |
391 ms |
30440 KB |
Output is correct |
29 |
Correct |
295 ms |
25768 KB |
Output is correct |
30 |
Correct |
8 ms |
4984 KB |
Output is correct |
31 |
Correct |
11 ms |
5368 KB |
Output is correct |
32 |
Correct |
10 ms |
5368 KB |
Output is correct |
33 |
Correct |
10 ms |
5368 KB |
Output is correct |
34 |
Correct |
10 ms |
5368 KB |
Output is correct |
35 |
Correct |
10 ms |
5368 KB |
Output is correct |
36 |
Correct |
10 ms |
5240 KB |
Output is correct |
37 |
Correct |
10 ms |
5240 KB |
Output is correct |
38 |
Correct |
10 ms |
5368 KB |
Output is correct |
39 |
Correct |
11 ms |
5368 KB |
Output is correct |
40 |
Correct |
10 ms |
5368 KB |
Output is correct |
41 |
Correct |
10 ms |
5368 KB |
Output is correct |
42 |
Correct |
11 ms |
5368 KB |
Output is correct |
43 |
Correct |
10 ms |
5368 KB |
Output is correct |
44 |
Correct |
11 ms |
5368 KB |
Output is correct |
45 |
Correct |
8 ms |
4984 KB |
Output is correct |
46 |
Correct |
449 ms |
29016 KB |
Output is correct |
47 |
Correct |
368 ms |
41848 KB |
Output is correct |
48 |
Correct |
411 ms |
27632 KB |
Output is correct |
49 |
Correct |
447 ms |
29336 KB |
Output is correct |
50 |
Correct |
425 ms |
28140 KB |
Output is correct |
51 |
Correct |
443 ms |
29288 KB |
Output is correct |
52 |
Correct |
448 ms |
28136 KB |
Output is correct |
53 |
Correct |
428 ms |
29352 KB |
Output is correct |
54 |
Correct |
435 ms |
31084 KB |
Output is correct |
55 |
Correct |
448 ms |
29676 KB |
Output is correct |
56 |
Correct |
414 ms |
28936 KB |
Output is correct |
57 |
Correct |
361 ms |
31648 KB |
Output is correct |
58 |
Correct |
401 ms |
37360 KB |
Output is correct |
59 |
Correct |
297 ms |
32452 KB |
Output is correct |
60 |
Correct |
8 ms |
4984 KB |
Output is correct |
61 |
Correct |
485 ms |
31556 KB |
Output is correct |
62 |
Correct |
424 ms |
41464 KB |
Output is correct |
63 |
Correct |
513 ms |
30188 KB |
Output is correct |
64 |
Correct |
493 ms |
31844 KB |
Output is correct |
65 |
Correct |
466 ms |
30568 KB |
Output is correct |
66 |
Correct |
492 ms |
31972 KB |
Output is correct |
67 |
Correct |
469 ms |
30568 KB |
Output is correct |
68 |
Correct |
487 ms |
32236 KB |
Output is correct |
69 |
Correct |
483 ms |
33508 KB |
Output is correct |
70 |
Correct |
484 ms |
31716 KB |
Output is correct |
71 |
Correct |
448 ms |
31784 KB |
Output is correct |
72 |
Correct |
426 ms |
34076 KB |
Output is correct |
73 |
Correct |
444 ms |
38504 KB |
Output is correct |
74 |
Correct |
352 ms |
36248 KB |
Output is correct |