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>
#pragma GCC optimize("O3")
using namespace std;
const int N=100050,B=750;
int n,m,q,t[N],a[N],b[N],u[N],v[N],w[N],par[N],sz[N],ans[N],rw[B+10][B+10],irw[N],rrl[B+10],pt,ret;
bool ch[N],vis[N];
vector<pair<int,int>> srt[N<<1];
vector<int> adj[N],del,dis,nxt[N];
unordered_map<int,int> dd;
int find(int u){
return par[u]==u?u:par[u]=find(par[u]);
}
void onion(int u,int v){
if (find(u)==find(v)) return;
//cout<<"onion"<<v<<" to "<<u<<"\n";
nxt[find(u)].push_back(find(v));
sz[find(u)]+=sz[find(v)];
sz[find(v)]=0;
par[find(v)]=u;
}
void ddfs(int fnde,int nde,int lim,int curr);
void dfs(int nde,int lim,int curr){
//cout<<curr<<": "<<nde<<"::"<<sz[nde]<<"\n";
vis[nde]=1;
del.push_back(nde);
ret+=sz[nde];
ddfs(nde,nde,lim,curr);
}
void ddfs(int fnde,int nde,int lim,int curr){
for (int i:adj[fnde]){
int des,cst;
if (i<0) des=find(u[rrl[-i]]);
else des=find(v[rrl[i]]);
cst=rw[curr][abs(i)];
//cout<<i<<":"<<cst<<"-->"<<des<<"\n";
if (cst<lim||vis[des]) continue;
dfs(des,lim,curr);
}
for (int i:nxt[fnde]) ddfs(i,nde,lim,curr);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n>>m;
for (int i=1;i<=m;i++){
cin>>u[i]>>v[i]>>w[i];
dis.push_back(w[i]);
}
cin>>q;
for (int i=1;i<=q;i++){
cin>>t[i]>>a[i]>>b[i];
dis.push_back(b[i]);
}
sort(dis.begin(),dis.end());
dis.erase(unique(dis.begin(),dis.end()),dis.end());
for (int i=0;i<dis.size();i++) dd[dis[i]]=i;
for (int l=1;l<=q;l+=B){
int r=min(q,l+B-1);
for (int i=1;i<=m;i++) ch[i]=0,irw[i]=0;
for (int i=0;i<dis.size();i++) srt[i].clear();
pt=0;
for (int i=l;i<=r;i++){
if (t[i]==1){
ch[a[i]]=1;
if (!irw[a[i]]) irw[a[i]]=++pt;
rrl[irw[a[i]]]=a[i];
//cout<<pt<<":::"<<a[i]<<"\n";
}
}
for (int i=1;i<=m;i++) if (!ch[i]) srt[dd[w[i]]].push_back({u[i],v[i]});
for (int i=l;i<=r;i++) if (t[i]==2) srt[dd[b[i]]].push_back({INT_MAX,i});
for (int i=1;i<=n;i++) par[i]=i,sz[i]=1,nxt[i].clear();
for (int i=1;i<=pt;i++) rw[0][i]=w[rrl[i]];
for (int i=0;i<=r-l;i++){
if (i) for (int j=1;j<=pt;j++) rw[i][j]=rw[i-1][j];
if (t[l+i]==1) rw[i][irw[a[l+i]]]=b[l+i];
}
for (int i=1;i<=n;i++) adj[i].clear();
for (int i=1;i<=m;i++) if (ch[i]){
adj[find(u[i])].push_back(irw[i]);
adj[find(v[i])].push_back(-irw[i]);
}
for (int j=dis.size()-1;j>=0;j--) for (auto i:srt[j]){
if (i.first==INT_MAX){
ret=0;
for (int k:del) vis[k]=0;
dfs(find(a[i.second]),b[i.second],i.second-l);
ans[i.second]=ret;
}else{
onion(i.first,i.second);
}
}
for (int i=l;i<=r;i++) if (t[i]==1) w[a[i]]=b[i];
}
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:55:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i=0;i<dis.size();i++) dd[dis[i]]=i;
| ~^~~~~~~~~~~
bridges.cpp:59:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i=0;i<dis.size();i++) srt[i].clear();
| ~^~~~~~~~~~~
# | 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... |