# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
748268 | nguyentunglam | Reconstruction Project (JOI22_reconstruction) | C++17 | 5065 ms | 6260 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 01;
int a[N], b[N], w[N], lab[N];
int n, m;
int root(int v) {
return lab[v] < 0 ? v : lab[v] = root(lab[v]);
}
bool join(int u, int v) {
u = root(u); v = root(v);
if (u == v) return 0;
if (lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return 1;
}
long long calc(int x) {
for(int i = 1; i <= n; i++) lab[i] = -1;
vector<tuple<int, int, int> > lst;
for(int i = 1; i <= m; i++) lst.emplace_back(abs(x - w[i]), a[i], b[i]);
sort(lst.begin(), lst.end());
long long ret = 0;
for(auto &[cost, x, y] : lst) {
if (join(x, y)) ret += cost;
}
return ret;
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n >> m;
for(int i = 1; i <= m; i++) cin >> a[i] >> b[i] >> w[i];
int q; cin >> q;
while (q--) {
int x; cin >> x;
cout << calc(x) << endl;
}
}
Compilation message (stderr)
# | 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... |