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>
struct DSU {
std::vector<int> e;
void init(int n) {
e = std::vector<int>(n, -1);
}
int get(int x) {
return e[x] < 0 ? x : e[x] = get(e[x]);
}
bool same_set(int a, int b) {
return get(a) == get(b);
}
int size(int x) {
return -e[get(x)];
}
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) std::swap(x, y);
e[x] += e[y]; e[y] = x;
return true;
}
};
int main() {
const int B = 700;
using namespace std;
ios_base::sync_with_stdio(0);
int n, m; cin >> n >> m;
vector<vector<int>> adj(n);
vector<array<int, 3>> ed;
vector<int> id(m);
iota(id.begin(), id.end(), 0);
for (int i = 0; i < m; i++) {
int u, v, w; cin >> u >> v >> w;
u--, v--;
ed.push_back({u, v, w});
}
vector<array<int, 3>> modify;
vector<array<int, 3>> queries;
vector<array<int, 2>> ans;
vector<int> vis(n);
vector<int> mod(m);
vector<int> change(m, -1);
vector<int> rem;
vector<int> todo;
DSU D;
int q; cin >> q;
for (int i = 0; i < q; i++) {
int x, y, z; cin >> x >> y >> z;
y--;
if (x == 1)
modify.push_back({y, z, i});
else
queries.push_back({y, z, i});
if (i == q - 1 || int(modify.size()) + int(queries.size()) == B) {
sort(id.begin(), id.end(), [&ed](int& x, int & y) {
return ed[x][2] < ed[y][2];
});
sort(queries.begin(), queries.end(), [](array<int, 3>& a, array<int, 3>& b) {
return a[1] > b[1];
});
D.init(n);
for (auto& mm : modify)
mod[mm[0]] = 1;
int it = m;
for (auto& qq : queries) {
int weight = qq[1];
int ti = qq[2];
while (it && ed[id[it - 1]][2] >= weight) {
it--;
if (!mod[id[it]])
D.unite(ed[id[it]][0], ed[id[it]][1]);
}
for (auto& mm : modify) {
todo.push_back(mm[0]);
if (mm[2] < ti)
change[mm[0]] = mm[1];
}
for (auto& t : todo) {
if (change[t] == -1) {
if (ed[t][2] >= weight) {
int u = D.get(ed[t][0]);
int v = D.get(ed[t][1]);
adj[u].push_back(v);
adj[v].push_back(u);
}
} else {
if (change[t] >= weight) {
int u = D.get(ed[t][0]);
int v = D.get(ed[t][1]);
adj[u].push_back(v);
adj[v].push_back(u);
}
}
}
int res = 0;
function<void(int)> dfs = [&](int src) {
vis[src] = 1;
res += D.size(src);
rem.push_back(src);
for (int nxt : adj[src]) {
if (vis[nxt])
continue;
dfs(nxt);
}
};
dfs(D.get(qq[0]));
ans.push_back({ti, res});
for (auto& r : rem)
vis[r] = 0;
for (auto& t : todo) {
change[t] = -1;
int u = D.get(ed[t][0]);
int v = D.get(ed[t][1]);
adj[u].clear();
adj[v].clear();
}
rem.clear();
todo.clear();
}
for (auto& mm : modify)
mod[mm[0]] = 0, ed[mm[0]][2] = mm[1];
queries.clear();
modify.clear();
}
}
sort(ans.begin(), ans.end());
for (auto& a : ans)
cout << a[1] << '\n';
return 0;
}
# | 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... |