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 ws weights
using namespace std;
typedef long long ll;
const int N = 3e5 + 10;
const int inf = 1e9 + 20;
const ll infty = (ll) (1e18);
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) {
u = get(u), v = get(v);
dsu[u] = v;
}
int main() {
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;
auto add_edge = [&] (int a, int b, int w) {
e.push_back({w, {a, b}});
};
for (int i = 0; i < m; i++) {
int w, u, v;
cin >> u >> v >> w;
add_edge(u, v, w);
}
sort(e.begin(), e.end());
for (int i = 0; i <= n; i++) {
dsu[i] = i;
}
int ws = 2 * n;
for (auto c : e) {
if (c.first == 1) {
if (get(c.second.first) != get(c.second.second)) {
uni(c.second.first, c.second.second);
ws--;
}
}
}
int q;
cin >> q;
vector <int> mp(n + 1);
for (int i = 1; i <= n; i++) {
if (a[i] == 1) {
if (get(i) != get(0)) {
mp[get(i)]++;
if (mp[get(i)] == 1) ws--;
}
}
}
while (q--) {
int i, w;
cin >> i >> w;
bool ch = (a[i] != w);
a[i] = w;
if (ch) {
if (w == 1) {
if (get(0) != get(i)) {
mp[get(i)]++;
if (mp[get(i)] == 1) ws--;
}
} else {
if (get(0) != get(i)) {
mp[get(i)]--;
if (mp[get(i)] == 0) ws++;
}
}
}
cout << ws << '\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... |