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>
using namespace std;
struct DSU {
vector<int> rt, sz;
vector<int> stk;
DSU(int n){
rt.resize(n);
iota(rt.begin(), rt.end(), 0);
sz.resize(n, 1);
}
int find(int u){
if (u == rt[u]) return u;
return find(rt[u]);
}
bool same(int u, int v){
return find(u) == find(v);
}
void unite(int u, int v){
u = find(u), v = find(v);
if (u == v) return;
if (sz[u] > sz[v])
swap(u, v);
rt[u] = v;
sz[v] += sz[u];
stk.push_back(u);
}
void undo(){
int u = stk.back(), v = rt[u];
stk.pop_back();
sz[v] -= sz[u];
rt[u] = u;
}
void rollback(){
while (stk.size())
undo();
}
void checkpoint(){
stk.clear();
}
int size(int u){
return sz[find(u)];
}
};
struct Edge {
int u, v, w, id;
Edge() = default;
Edge(int _u, int _v, int _w, int _i): u(_u), v(_v), w(_w), id(_i){}
};
struct Event {
int i, w, id;
Event() = default;
Event(int _i, int _w, int _id): i(_i), w(_w), id(_id){}
};
struct Query {
int u, w, id;
Query() = default;
Query(int _u, int _w, int _i): u(_u), w(_w), id(_i){}
};
const int B = 320;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n, m; cin >> n >> m;
vector<Edge> ed(m);
for (int i = 0; i < m; i++){
int u, v, w; cin >> u >> v >> w;
u--, v--;
ed[i] = Edge(u, v, w, i);
}
int q; cin >> q;
vector<int> ans(q);
for (int qq = 0; qq < q; ){
int R = min(q, qq + B);
vector<Event> evnt;
vector<Query> qry;
vector<int> vis(m);
for (; qq < R; qq++){
int t; cin >> t;
if (t == 1){
int i, w; cin >> i >> w;
i--;
evnt.emplace_back(i, w, qq);
vis[i] = 1;
}else{
int u, w; cin >> u >> w;
u--;
qry.emplace_back(u, w, qq);
}
}
reverse(evnt.begin(), evnt.end());
// cerr << endl << evnt.size() << '\n';
// for (auto &[i, w, id]: evnt)
// cerr << i << ' ' << w << ' ' << id << endl;
vector<Edge> eed;
for (int i = 0; i < m; i++){
if (vis[i])
evnt.emplace_back(i, ed[i].w, -1);
else
eed.push_back(ed[i]);
}
// cerr << endl << evnt.size() << ' ' << eed.size() << '\n';
// for (auto &[i, w, id]: evnt)
// cerr << i << ' ' << w << ' ' << id << endl;
// return 0;
sort(eed.begin(), eed.end(), [&](auto x, auto y){
return x.w > y.w;
});
sort(qry.begin(), qry.end(), [&](auto x, auto y){
return x.w > y.w;
});
DSU dsu(n);
int i = 0;
for (auto &[u, w, id]: qry){
while (i < m && eed[i].w >= w){
dsu.unite(eed[i].u, eed[i].v);
i++;
}
dsu.checkpoint();
for (auto &[ii, ww, j]: evnt){
if (j < id){
if (vis[ii] != -1 && ww >= w)
dsu.unite(ed[ii].u, ed[ii].v);
vis[ii] = -1;
}
}
// cerr << "--- " << dsu.same(0, 1) << ' ' << dsu.same(1, 2) << endl;
// cerr << "::: " << cnt << endl;
// for (int ii = 0; ii < m; ii++)
// cerr << vis[ii] << ' ';
// cerr << endl;
ans[id] = dsu.size(u);
// cerr << id << ' ' << ans[id] << endl;
for (auto &[ii, ww, j]: evnt)
vis[ii] = 1;
dsu.rollback();
// cerr << "OK" << endl;
}
for (auto [i, w, id]: evnt)
if (id != -1)
ed[i].w = w;
}
for (auto &x: ans)
if (x != 0)
cout << x << '\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... |