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;
#define ms(x, a) memset(x, a, sizeof x)
typedef long long ll;
const int mod = 1e9 + 7, inf = 0x3f3f3f3f;
const int MN = 5e4 + 1, MM = 1e5 + 1, MQ = 1e5 + 1;
const int SZ = 300;
struct DisjointSet{
vector<int> p, sz;
stack<pair<int,int>> hist;
DisjointSet(int N){
p = vector<int>(N + 1);
sz = vector<int>(N + 1);
hist = stack<pair<int,int>>();
reset();
}
int Find(int x){ return p[x] == x ? x : Find(p[x]); }
void Union(int a, int b){
a = Find(a), b = Find(b);
if (a == b) a = -1, b = -1;
else{
if (sz[b] > sz[a]) swap(a, b);
p[b] = a, sz[a] += sz[b];
}
hist.push({a, b});
}
void undo(){
assert(!hist.empty());
auto [a, b] = hist.top(); hist.pop();
if (a == 0) return;
sz[a] -= sz[b], p[b] = b;
}
int getsz(int v){ return sz[Find(v)]; }
void reset(){
while (!hist.empty()) hist.pop();
iota(p.begin(), p.end(), 0);
fill(sz.begin(), sz.end(), 1);
}
} dsu(MN);
struct Edge{ int a, b, w; } all_edges[MM];
struct Query{ int v, w, t; };
int ans[MQ];
int main(){
cin.tie(0)->sync_with_stdio(0);
int N, M; cin >> N >> M;
for (int i = 1; i <= M; ++i){
int a, b, w; cin >> a >> b >> w;
all_edges[i] = {a, b, w};
}
int Q; cin >> Q;
vector<Query> qs1, qs2;
unordered_map<int, int> changes;
for (int _ = 1; _ <= Q; ++_){
int op, v, w; cin >> op >> v >> w;
if (op == 1) qs1.push_back({v, w, _}), all_edges[v].w = -abs(all_edges[v].w);
if (op == 2) qs2.push_back({v, w, _});
if ((Q - _) % SZ) continue;
vector<Edge> edges;
dsu.reset();
for (int i = 1; i <= M; ++i){
if (all_edges[i].w > 0) edges.push_back(all_edges[i]);
}
sort(edges.begin(), edges.end(), [](Edge a, Edge b){ return a.w > b.w; });
sort(qs2.begin(), qs2.end(), [](Query a, Query b){ return a.w > b.w; });
int pl = 0;
for (auto [v, w, t] : qs2){
while (pl != edges.size() && edges[pl].w >= w) dsu.Union(edges[pl].a, edges[pl].b), ++pl;
for (auto [ev, ew, et] : qs1) changes[ev] = -all_edges[ev].w;
for (auto [ev, ew, et] : qs1){
if (et <= t) changes[ev] = ew;
else break;
}
int T = 0;
for (auto [i, ew] : changes){
if (ew >= w){
dsu.Union(all_edges[i].a, all_edges[i].b);
++T;
}
}
ans[t] = dsu.getsz(v);
for (int i = 1; i <= T; ++i) dsu.undo();
}
for (auto [v, w, t] : qs1) all_edges[v].w = w;
for (int i = 1; i <= M; ++i) all_edges[i].w = abs(all_edges[i].w);
qs1.clear(), qs2.clear(); changes.clear();
}
for (int i = 1; i <= Q; ++i){
if (ans[i]) cout << ans[i] << '\n';
}
}
Compilation message (stderr)
bridges.cpp: In function 'int main()':
bridges.cpp:71:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | while (pl != edges.size() && edges[pl].w >= w) dsu.Union(edges[pl].a, edges[pl].b), ++pl;
| ~~~^~~~~~~~~~~~~~~
# | 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... |