This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
// #define int long long
#define all(v) v.begin(),v.end()
using namespace std;
mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1e5;
int n,m,q,u[N+5],v[N+5],d[N+5],ty[N+5],x[N+5],y[N+5],ans[N+5],tr[N+5];
bool ok,use[N+5];
void Update(int i, int j){
for (int k=i;k<=j;++k)
if (ty[k]==1) d[x[k]]=y[k];
}
struct DSU_rollback{
int ds[N+5];
vector <pair<int,int>> history;
void Reset(){
for (int i=1;i<=n;++i) ds[i]=-1;
history.clear();
}
int Get(int idx){
return ds[idx]<0 ? idx : Get(ds[idx]);
}
void Join(int u, int v){
u=Get(u);
v=Get(v);
if (u==v) return;
if (ds[u]>ds[v]) swap(u,v);
if (ok) history.push_back({v,ds[v]});
ds[u]+=ds[v];
ds[v]=u;
}
void RollBack(int sz){
while (history.size()>sz){
ds[ds[history.back().first]]-=history.back().second;
ds[history.back().first]=history.back().second;
history.pop_back();
}
}
} dsu;
void Sol(int l, int r){
vector <int> before_edge,now_edge,query,qr1;
for (int i=l;i<=r;++i)
if (ty[i]==1){
use[x[i]]=true;
qr1.push_back(i);
}
else query.push_back(i);
for (int i=1;i<=m;++i)
if (!use[i]) before_edge.push_back(i);
else now_edge.push_back(i);
sort(all(before_edge),[&](int x, int y){return d[x]>d[y];});
sort(all(query),[&](int a, int b){return y[a]>y[b];});
dsu.Reset();
int pointer=0;
for (int i=l;i<=r;++i) tr[x[i]]=d[x[i]];
for (int pos : query){
while (pointer<before_edge.size() && d[before_edge[pointer]]>=y[pos]){
ok=false;
dsu.Join(u[before_edge[pointer]],v[before_edge[pointer]]);
++pointer;
}
ok=true;
int size_before=dsu.history.size();
for (int i : qr1)
if (i<pos)
tr[x[i]]=y[i];
else break;
for (int i : now_edge)
if (tr[i]>=y[pos])
dsu.Join(u[i],v[i]);
for (int i : qr1)
if (i<pos)
tr[x[i]]=d[x[i]];
else break;
ans[pos]=-dsu.ds[dsu.Get(x[pos])];
dsu.RollBack(size_before);
}
for (int x : now_edge) use[x]=false;
}
void Solve(){
cin>>n>>m;
for (int i=1;i<=m;++i) cin>>u[i]>>v[i]>>d[i];
cin>>q;
for (int i=1;i<=q;++i) cin>>ty[i]>>x[i]>>y[i];
int bl=500;
for (int i=1;i<=q;i+=bl){
int j=min(q,i+bl-1);
Sol(i,j);
Update(i,j);
}
for (int i=1;i<=q;++i)
if (ty[i]==2) cout<<ans[i]<<"\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen ("FILE.INP","r",stdin);
// freopen ("FILE.OUT","w",stdout);
int t=1;
while (t--) Solve();
cerr<<"\nTIME: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
}
Compilation message (stderr)
bridges.cpp: In member function 'void DSU_rollback::RollBack(int)':
bridges.cpp:35:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
35 | while (history.size()>sz){
| ~~~~~~~~~~~~~~^~~
bridges.cpp: In function 'void Sol(int, int)':
bridges.cpp:59:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | while (pointer<before_edge.size() && d[before_edge[pointer]]>=y[pos]){
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |