#include<bits/stdc++.h>
using namespace std;
#define rall(s) s.rbegin() s.rend()
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define s second
#define f first
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int N = 1000;
int pr[N], sz[N];
int get(int x) {
if (pr[x] == x) return x;
return pr[x] = get(pr[x]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<array<int, 3>> g;
while (m--) {
int a, b, w;
cin >> a >> b >> w;
g.push_back({w, a, b});
}
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
if (m == n - 1) {
ll ans = 0;
for (auto [w, a, b]: g) ans += abs(w - x);
cout << ans << '\n';
continue;
}
for (int i = 1; i <= n; i++) {
pr[i] = i, sz[i] = 1;
}
vector<array<int, 3>> gr;
for (auto [w, a, b]: g) {
gr.push_back({abs(w - x), a, b});
}
sort(all(gr));
ll ans = 0;
for (auto [w, a, b]: gr) {
a = get(a), b = get(b);
if (a == b) continue;
if (sz[a] > sz[b]) swap(a, b);
ans += w, pr[a] = b, sz[b] += sz[a];
}
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
464 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
464 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
117 ms |
6040 KB |
Output is correct |
21 |
Correct |
112 ms |
6068 KB |
Output is correct |
22 |
Correct |
120 ms |
6048 KB |
Output is correct |
23 |
Correct |
136 ms |
6044 KB |
Output is correct |
24 |
Correct |
117 ms |
6040 KB |
Output is correct |
25 |
Correct |
115 ms |
6072 KB |
Output is correct |
26 |
Correct |
115 ms |
6040 KB |
Output is correct |
27 |
Correct |
119 ms |
6076 KB |
Output is correct |
28 |
Correct |
141 ms |
6056 KB |
Output is correct |
29 |
Correct |
143 ms |
6076 KB |
Output is correct |
30 |
Correct |
113 ms |
6080 KB |
Output is correct |
31 |
Correct |
114 ms |
6076 KB |
Output is correct |
32 |
Correct |
118 ms |
6040 KB |
Output is correct |
33 |
Correct |
117 ms |
6040 KB |
Output is correct |
34 |
Correct |
149 ms |
6156 KB |
Output is correct |
35 |
Correct |
116 ms |
6048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5090 ms |
6380 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
464 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
460 KB |
Output is correct |
20 |
Execution timed out |
5085 ms |
4172 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
464 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
117 ms |
6040 KB |
Output is correct |
21 |
Correct |
112 ms |
6068 KB |
Output is correct |
22 |
Correct |
120 ms |
6048 KB |
Output is correct |
23 |
Correct |
136 ms |
6044 KB |
Output is correct |
24 |
Correct |
117 ms |
6040 KB |
Output is correct |
25 |
Correct |
115 ms |
6072 KB |
Output is correct |
26 |
Correct |
115 ms |
6040 KB |
Output is correct |
27 |
Correct |
119 ms |
6076 KB |
Output is correct |
28 |
Correct |
141 ms |
6056 KB |
Output is correct |
29 |
Correct |
143 ms |
6076 KB |
Output is correct |
30 |
Correct |
113 ms |
6080 KB |
Output is correct |
31 |
Correct |
114 ms |
6076 KB |
Output is correct |
32 |
Correct |
118 ms |
6040 KB |
Output is correct |
33 |
Correct |
117 ms |
6040 KB |
Output is correct |
34 |
Correct |
149 ms |
6156 KB |
Output is correct |
35 |
Correct |
116 ms |
6048 KB |
Output is correct |
36 |
Execution timed out |
5058 ms |
6268 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
464 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
117 ms |
6040 KB |
Output is correct |
21 |
Correct |
112 ms |
6068 KB |
Output is correct |
22 |
Correct |
120 ms |
6048 KB |
Output is correct |
23 |
Correct |
136 ms |
6044 KB |
Output is correct |
24 |
Correct |
117 ms |
6040 KB |
Output is correct |
25 |
Correct |
115 ms |
6072 KB |
Output is correct |
26 |
Correct |
115 ms |
6040 KB |
Output is correct |
27 |
Correct |
119 ms |
6076 KB |
Output is correct |
28 |
Correct |
141 ms |
6056 KB |
Output is correct |
29 |
Correct |
143 ms |
6076 KB |
Output is correct |
30 |
Correct |
113 ms |
6080 KB |
Output is correct |
31 |
Correct |
114 ms |
6076 KB |
Output is correct |
32 |
Correct |
118 ms |
6040 KB |
Output is correct |
33 |
Correct |
117 ms |
6040 KB |
Output is correct |
34 |
Correct |
149 ms |
6156 KB |
Output is correct |
35 |
Correct |
116 ms |
6048 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
5090 ms |
6380 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |