# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
854889 | JustInCase | 다리 (APIO19_bridges) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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) {
assert(updateInd < updates.size());
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;
}
}
}