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 <algorithm>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
const int BLOCK_SIZE = 317;
struct edge {
int u, v, w;
static bool compDecrW(const edge &e1, const edge &e2) { return e1.w >= e2.w; }
};
struct update {
int e, w;
};
struct query {
int id;
int v, w;
static bool compDecrW(const query &q1, const query &q2) {
return q1.w >= q2.w;
}
};
struct rollback_dsu {
struct change {
int oldRoot, newRoot;
};
vector<int> parent;
vector<int> compSize;
stack<change> changes;
rollback_dsu(int cntNodes) {
parent.resize(cntNodes);
compSize.resize(cntNodes, 1);
for (int i = 0; i < cntNodes; i++) {
parent[i] = i;
}
}
int findRoot(int x) {
while (parent[x] != x)
x = parent[x];
return x;
}
void unite(int x, int y) {
int rootX = findRoot(x);
int rootY = findRoot(y);
if (rootX == rootY)
return;
if (compSize[rootX] <= compSize[rootY]) {
parent[rootX] = rootY;
compSize[rootY] += compSize[rootX];
changes.push(change{rootX, rootY});
} else {
parent[rootY] = rootX;
compSize[rootX] += compSize[rootY];
changes.push(change{rootY, rootX});
}
}
void markAsStable() {
while (!changes.empty())
changes.pop();
}
void rollbackToStable() {
while (!changes.empty()) {
auto [oldRoot, newRoot] = changes.top();
changes.pop();
parent[oldRoot] = oldRoot;
compSize[newRoot] -= compSize[oldRoot];
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<edge> edges(m);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
edges[i] = edge{u, v, w};
}
int q;
cin >> q;
for (int i = 0; i < q; i += BLOCK_SIZE) {
vector<int> t(BLOCK_SIZE);
vector<bool> changed(m, false);
vector<update> updates;
vector<query> queries;
vector<vector<edge>> additionalEdges(BLOCK_SIZE);
for (int j = 0; j < min(BLOCK_SIZE, q - i); j++) {
cin >> t[j];
if (t[j] == 1) {
int e, w;
cin >> e >> w;
e--;
updates.push_back(update{e, w});
changed[e] = true;
} else {
int v, w;
cin >> v >> w;
v--;
queries.push_back(query{(int)queries.size(), v, w});
additionalEdges.push_back(vector<edge>());
}
}
int queryInd = 0, updateInd = 0;
for (int j = 0; j < min(BLOCK_SIZE, q - i); j++) {
if (t[j] == 1) {
edges[updates[updateInd].e].w = updates[updateInd].w;
updateInd++;
} else {
for (auto u : updates) {
if (edges[u.e].w >= queries[queryInd].w)
additionalEdges[queryInd].push_back(edges[u.e]);
}
queryInd++;
}
}
vector<edge> unchanged;
for (int j = 0; j < m; j++) {
if (!changed[j])
unchanged.push_back(edges[j]);
}
sort(unchanged.begin(), unchanged.end(), edge::compDecrW);
sort(queries.begin(), queries.end(), query::compDecrW);
auto dsu = rollback_dsu(n);
int edgePtr = 0;
vector<int> ans(queries.size());
for (int j = 0; j < queries.size(); j++) {
while (edgePtr < unchanged.size() &&
unchanged[edgePtr].w >= queries[j].w) {
dsu.unite(unchanged[edgePtr].u, unchanged[edgePtr].v);
edgePtr++;
}
dsu.markAsStable();
for (auto e : additionalEdges[queries[j].id]) {
dsu.unite(e.u, e.v);
}
ans[queries[j].id] = dsu.compSize[dsu.findRoot(queries[j].v)];
dsu.rollbackToStable();
}
for (auto x : ans) {
cout << x << endl;
}
}
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:163:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | for (int j = 0; j < queries.size(); j++) {
| ~~^~~~~~~~~~~~~~~~
bridges.cpp:164:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
164 | while (edgePtr < unchanged.size() &&
| ~~~~~~~~^~~~~~~~~~~~~~~~~~
# | 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... |