이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
typedef long long ll;
const int MAXN = 1e3 + 1;
array <int, 3> edges[MAXN];
int sze[MAXN], par[MAXN];
inline void init (int n) {
for (int i = 1; i <= 500; i++) {
sze[i] = 1; par[i] = i;
}
}
int find (int x) {
return par[x] == x ? x : par[x] = find(par[x]);
}
ll sum; int x;
inline void merge (int a, int b, int c) {
a = find(a); b = find(b);
if (a == b) return;
sum += abs(c - x);
if (sze[a] > sze[b]) swap(a, b);
sze[b] += sze[a]; par[a] = b;
}
int n, m;
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
edges[i] = {c, a, b};
}
sort(edges + 1, edges + m + 1);
int ptr = 1;
int q;
cin >> q;
while (q--) {
cin >> x;
if (m == 1) {
cout << abs(x - edges[1][0]) << '\n';
continue;
}
init(n); sum = 0;
while (ptr + 1 <= m && abs(edges[ptr + 1][0] - x) <= abs(edges[ptr][0] - x)) ptr++;
int l = ptr, r = ptr;
while (l >= 1 || r <= m) {
if (l >= 1 && (r > m || abs(x - edges[l][0]) < abs(x - edges[r][0]))) {
merge(edges[l][1], edges[l][2], edges[l][0]);
l--;
} else {
merge(edges[r][1], edges[r][2], edges[r][0]);
r++;
}
}
cout << sum << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |