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 int long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define LL_MAX LONG_LONG_MAX
#define LL_MIN LONG_LONG_MIN
#define speed ios_base::sync_with_stdio(false); cin.tie(0)
#define stMx(a,b) a = max(a,b)
#define stMn(a,b) a = min(a,b)
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef set<int> si;
typedef vector<ii> vii;
typedef set<ii> sii;
#define REP(i, a, b) for(int i = a; i < b; i++)
#define HASTC 0
#define B 1000
int par[121010], sz[121010];
int u[121010], v[121010], w[121010];
int t[B+5], f[B+5], s[B+5], ans[B+5];
vi join_edge[B+5];
bool changed[121010];
stack<int> onions;
// onion find disjoint sets (with rollbacks)
int find_set(int x) {
while(x!=par[x]) x=par[x];
return x;
}
void onion(int x, int y) {
x=find_set(x),y=find_set(y);
if(x==y) {onions.push(-1); return;}
if(sz[x]<sz[y]) swap(x,y);
onions.push(y);
par[y]=x; sz[x]+=sz[y];
}
void rollback() {
assert(!onions.empty());
if(onions.top()!=-1) {
sz[par[onions.top()]]-=sz[onions.top()];
par[onions.top()]=onions.top();
}
onions.pop();
}
void init_bucket() {
iota(par,par+121010,0);
fill(sz,sz+121010, 1);
fill(changed,changed+121010,false);
}
void solvetc(int idx) {
int n, m, q; cin >> n >> m;
REP(i,0,m) {cin>>u[i]>>v[i]>>w[i]; u[i]--,v[i]--;}
cin>>q;
for(int i=0; i<q; i+=B) {
init_bucket();
vi qry, upd, nUpd;
int j=min(q,i+B);
REP(k,i,j) {
cin>>t[k-i]>>f[k-i]>>s[k-i];
if(t[k-i]==1) {upd.pb(k-i); changed[--f[k-i]]=1;}
else qry.pb(k-i), f[k-i]--;
}
REP(k,0,m) if(!changed[k]) nUpd.pb(k);
REP(k,i,j) {
if(t[k-i]==1) w[f[k-i]]=s[k-i];
else {
join_edge[k-i].clear();
for(auto l: upd) if(w[f[l]]>=s[k-i]) join_edge[k-i].pb(f[l]);
}
}
sort(qry.begin(),qry.end(),[](int a, int b){return s[a]>s[b];});
sort(nUpd.begin(),nUpd.end(),[](int a, int b){return w[a]>w[b];});
int ptr=0;
for(auto k: qry) {
while(ptr<nUpd.size()&&w[nUpd[ptr]]>=s[k]) {
onion(u[nUpd[ptr]],v[nUpd[ptr]]);
ptr++;
}
for(auto l: join_edge[k]) onion(u[l],v[l]);
ans[k]=sz[find_set(f[k])];
for(auto l: join_edge[k]) rollback();
}
REP(k,i,j) if(t[k-i]==2) cout<<ans[k-i]<<"\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int tc;
if(HASTC) cin>>tc;
else tc=1;
for(int i=0;i<tc;i++) solvetc(i);
}
Compilation message (stderr)
bridges.cpp: In function 'void solvetc(long long int)':
bridges.cpp:77:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | while(ptr<nUpd.size()&&w[nUpd[ptr]]>=s[k]) {
| ~~~^~~~~~~~~~~~
bridges.cpp:83:22: warning: unused variable 'l' [-Wunused-variable]
83 | for(auto l: join_edge[k]) rollback();
| ^
# | 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... |