Submission #416376

#TimeUsernameProblemLanguageResultExecution timeMemory
416376model_codeRooted MST (innopolis2021_final_E)C++17
10 / 100
303 ms9408 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...