#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 |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
186 ms |
396 KB |
Output is correct |
5 |
Correct |
828 ms |
452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4027 ms |
18684 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4064 ms |
10956 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4046 ms |
12412 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4046 ms |
12412 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4056 ms |
23716 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
186 ms |
396 KB |
Output is correct |
5 |
Correct |
828 ms |
452 KB |
Output is correct |
6 |
Execution timed out |
4102 ms |
6400 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
186 ms |
396 KB |
Output is correct |
5 |
Correct |
828 ms |
452 KB |
Output is correct |
6 |
Execution timed out |
4027 ms |
18684 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |