#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define em emplace
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int MAX = 101010;
const int INF = 1e9;
const ll LINF = 1e18;
vector <pair <int, pll> > g[2*MAX];
ll d[2*MAX], sum, ans[2*MAX], up[2*MAX];
bool chk[2*MAX];
int deg[2*MAX];
void dfs(int x, int pa) {
for(auto i : g[x]) {
if(i.fi == pa) continue;
d[i.fi] = d[x] + i.se.fi;
up[i.fi] = up[x] + i.se.se;
sum += i.se.fi;
dfs(i.fi, x);
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n;
cin >> n;
for(int i = 1; i < n; i++) {
int u, v; ll c, d;
cin >> u >> v >> c >> d;
g[u].eb(v, make_pair(c, d)), g[v].eb(u, make_pair(d, c));
deg[u]++, deg[v]++;
}
priority_queue <pll, vector <pll>, greater <pll> > pq;
for(int i = 1; i <= n; i++) {
if(deg[i] == 1) pq.em(g[i][0].se.se, i);
}
while(pq.size() > 2) {
pll cur = pq.top();
pq.pop();
chk[cur.se] = true;
int u;
for(auto i : g[cur.se]) {
if(!chk[i.fi]) {
u = i.fi;
break;
}
}
deg[u]--;
if(deg[u] == 1) {
for(auto i : g[u]) {
if(!chk[i.fi]) {
pq.em(cur.fi + i.se.se, u);
break;
}
}
continue;
}
ans[pq.size()] = ans[pq.size()+1] + cur.fi;
}
dfs(1, 0);
ans[1] = LINF;
for(int i = 1; i <= n; i++) {
ans[1] = min(ans[1], sum - d[i] + up[i]);
}
int q;
cin >> q;
while(q--) {
int u; cin >> u;
cout << ans[u] << '\n';
}
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:51:13: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
int u;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
9 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
1 ms |
5240 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5144 KB |
Output is correct |
8 |
Correct |
4 ms |
5112 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5084 KB |
Output is correct |
2 |
Correct |
514 ms |
24540 KB |
Output is correct |
3 |
Correct |
383 ms |
36248 KB |
Output is correct |
4 |
Correct |
416 ms |
29480 KB |
Output is correct |
5 |
Correct |
426 ms |
31844 KB |
Output is correct |
6 |
Correct |
450 ms |
32268 KB |
Output is correct |
7 |
Correct |
506 ms |
32800 KB |
Output is correct |
8 |
Correct |
381 ms |
37240 KB |
Output is correct |
9 |
Correct |
351 ms |
35552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
427 ms |
24516 KB |
Output is correct |
3 |
Correct |
328 ms |
31352 KB |
Output is correct |
4 |
Correct |
417 ms |
24552 KB |
Output is correct |
5 |
Correct |
501 ms |
25652 KB |
Output is correct |
6 |
Correct |
534 ms |
26168 KB |
Output is correct |
7 |
Correct |
401 ms |
29088 KB |
Output is correct |
8 |
Correct |
448 ms |
29608 KB |
Output is correct |
9 |
Correct |
372 ms |
29208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
9 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
1 ms |
5240 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5144 KB |
Output is correct |
8 |
Correct |
4 ms |
5112 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
7 ms |
5112 KB |
Output is correct |
13 |
Correct |
10 ms |
5372 KB |
Output is correct |
14 |
Correct |
9 ms |
5368 KB |
Output is correct |
15 |
Correct |
11 ms |
5496 KB |
Output is correct |
16 |
Correct |
10 ms |
5392 KB |
Output is correct |
17 |
Correct |
10 ms |
5368 KB |
Output is correct |
18 |
Correct |
10 ms |
5496 KB |
Output is correct |
19 |
Correct |
9 ms |
5368 KB |
Output is correct |
20 |
Correct |
11 ms |
5496 KB |
Output is correct |
21 |
Correct |
9 ms |
5368 KB |
Output is correct |
22 |
Correct |
7 ms |
5368 KB |
Output is correct |
23 |
Correct |
9 ms |
5496 KB |
Output is correct |
24 |
Correct |
9 ms |
5496 KB |
Output is correct |
25 |
Correct |
9 ms |
5496 KB |
Output is correct |
26 |
Correct |
9 ms |
5496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5084 KB |
Output is correct |
2 |
Correct |
514 ms |
24540 KB |
Output is correct |
3 |
Correct |
383 ms |
36248 KB |
Output is correct |
4 |
Correct |
416 ms |
29480 KB |
Output is correct |
5 |
Correct |
426 ms |
31844 KB |
Output is correct |
6 |
Correct |
450 ms |
32268 KB |
Output is correct |
7 |
Correct |
506 ms |
32800 KB |
Output is correct |
8 |
Correct |
381 ms |
37240 KB |
Output is correct |
9 |
Correct |
351 ms |
35552 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
427 ms |
24516 KB |
Output is correct |
12 |
Correct |
328 ms |
31352 KB |
Output is correct |
13 |
Correct |
417 ms |
24552 KB |
Output is correct |
14 |
Correct |
501 ms |
25652 KB |
Output is correct |
15 |
Correct |
534 ms |
26168 KB |
Output is correct |
16 |
Correct |
401 ms |
29088 KB |
Output is correct |
17 |
Correct |
448 ms |
29608 KB |
Output is correct |
18 |
Correct |
372 ms |
29208 KB |
Output is correct |
19 |
Correct |
7 ms |
5112 KB |
Output is correct |
20 |
Correct |
483 ms |
30884 KB |
Output is correct |
21 |
Correct |
339 ms |
37640 KB |
Output is correct |
22 |
Correct |
399 ms |
29440 KB |
Output is correct |
23 |
Correct |
537 ms |
31196 KB |
Output is correct |
24 |
Correct |
439 ms |
29912 KB |
Output is correct |
25 |
Correct |
514 ms |
30960 KB |
Output is correct |
26 |
Correct |
435 ms |
29908 KB |
Output is correct |
27 |
Correct |
422 ms |
31272 KB |
Output is correct |
28 |
Correct |
416 ms |
32000 KB |
Output is correct |
29 |
Correct |
428 ms |
31192 KB |
Output is correct |
30 |
Correct |
411 ms |
30948 KB |
Output is correct |
31 |
Correct |
458 ms |
32700 KB |
Output is correct |
32 |
Correct |
478 ms |
36012 KB |
Output is correct |
33 |
Correct |
341 ms |
35612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
9 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
1 ms |
5240 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5144 KB |
Output is correct |
8 |
Correct |
4 ms |
5112 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
5 ms |
5084 KB |
Output is correct |
13 |
Correct |
514 ms |
24540 KB |
Output is correct |
14 |
Correct |
383 ms |
36248 KB |
Output is correct |
15 |
Correct |
416 ms |
29480 KB |
Output is correct |
16 |
Correct |
426 ms |
31844 KB |
Output is correct |
17 |
Correct |
450 ms |
32268 KB |
Output is correct |
18 |
Correct |
506 ms |
32800 KB |
Output is correct |
19 |
Correct |
381 ms |
37240 KB |
Output is correct |
20 |
Correct |
351 ms |
35552 KB |
Output is correct |
21 |
Correct |
7 ms |
5112 KB |
Output is correct |
22 |
Correct |
427 ms |
24516 KB |
Output is correct |
23 |
Correct |
328 ms |
31352 KB |
Output is correct |
24 |
Correct |
417 ms |
24552 KB |
Output is correct |
25 |
Correct |
501 ms |
25652 KB |
Output is correct |
26 |
Correct |
534 ms |
26168 KB |
Output is correct |
27 |
Correct |
401 ms |
29088 KB |
Output is correct |
28 |
Correct |
448 ms |
29608 KB |
Output is correct |
29 |
Correct |
372 ms |
29208 KB |
Output is correct |
30 |
Correct |
7 ms |
5112 KB |
Output is correct |
31 |
Correct |
10 ms |
5372 KB |
Output is correct |
32 |
Correct |
9 ms |
5368 KB |
Output is correct |
33 |
Correct |
11 ms |
5496 KB |
Output is correct |
34 |
Correct |
10 ms |
5392 KB |
Output is correct |
35 |
Correct |
10 ms |
5368 KB |
Output is correct |
36 |
Correct |
10 ms |
5496 KB |
Output is correct |
37 |
Correct |
9 ms |
5368 KB |
Output is correct |
38 |
Correct |
11 ms |
5496 KB |
Output is correct |
39 |
Correct |
9 ms |
5368 KB |
Output is correct |
40 |
Correct |
7 ms |
5368 KB |
Output is correct |
41 |
Correct |
9 ms |
5496 KB |
Output is correct |
42 |
Correct |
9 ms |
5496 KB |
Output is correct |
43 |
Correct |
9 ms |
5496 KB |
Output is correct |
44 |
Correct |
9 ms |
5496 KB |
Output is correct |
45 |
Correct |
7 ms |
5112 KB |
Output is correct |
46 |
Correct |
483 ms |
30884 KB |
Output is correct |
47 |
Correct |
339 ms |
37640 KB |
Output is correct |
48 |
Correct |
399 ms |
29440 KB |
Output is correct |
49 |
Correct |
537 ms |
31196 KB |
Output is correct |
50 |
Correct |
439 ms |
29912 KB |
Output is correct |
51 |
Correct |
514 ms |
30960 KB |
Output is correct |
52 |
Correct |
435 ms |
29908 KB |
Output is correct |
53 |
Correct |
422 ms |
31272 KB |
Output is correct |
54 |
Correct |
416 ms |
32000 KB |
Output is correct |
55 |
Correct |
428 ms |
31192 KB |
Output is correct |
56 |
Correct |
411 ms |
30948 KB |
Output is correct |
57 |
Correct |
458 ms |
32700 KB |
Output is correct |
58 |
Correct |
478 ms |
36012 KB |
Output is correct |
59 |
Correct |
341 ms |
35612 KB |
Output is correct |
60 |
Correct |
7 ms |
5112 KB |
Output is correct |
61 |
Correct |
480 ms |
33548 KB |
Output is correct |
62 |
Correct |
362 ms |
38060 KB |
Output is correct |
63 |
Correct |
481 ms |
32112 KB |
Output is correct |
64 |
Correct |
577 ms |
33740 KB |
Output is correct |
65 |
Correct |
544 ms |
32312 KB |
Output is correct |
66 |
Correct |
566 ms |
33712 KB |
Output is correct |
67 |
Correct |
562 ms |
32424 KB |
Output is correct |
68 |
Correct |
591 ms |
34104 KB |
Output is correct |
69 |
Correct |
475 ms |
34608 KB |
Output is correct |
70 |
Correct |
473 ms |
33568 KB |
Output is correct |
71 |
Correct |
463 ms |
33616 KB |
Output is correct |
72 |
Correct |
428 ms |
35868 KB |
Output is correct |
73 |
Correct |
412 ms |
37432 KB |
Output is correct |
74 |
Correct |
377 ms |
39728 KB |
Output is correct |