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 <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
using namespace std;
typedef long long ll;
#ifdef iq
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
const int N = 3e5 + 20;
int dsu[N];
int get(int v) {
if (v == dsu[v]) return v;
else return dsu[v] = get(dsu[v]);
}
void uni(int u, int v) {
dsu[get(u)] = get(v);
}
int main() {
#ifdef iq
freopen("a.in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector <int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector <pair <int, pair <int, int> > > e;
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
e.push_back({w, {u, v}});
}
auto solve = [&] () {
auto f = e;
for (int i = 1; i <= n; i++) {
f.push_back({a[i], {0, i}});
}
sort(f.begin(), f.end());
for (int i = 0; i <= n; i++) dsu[i] = i;
ll sum = 0;
for (auto c : f) {
auto t = c.second;
if (get(t.first) != get(t.second)) {
sum += c.first;
uni(t.first, t.second);
}
}
return sum;
};
int q;
cin >> q;
while (q--) {
int i, w;
cin >> i >> w;
a[i] = w;
cout << solve() << '\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |