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")
#pragma GCC optimize("avx2")
#include<bits/stdc++.h>
using namespace std;
int par[100001],sz[100001];
stack<int> srb;
int findPar(int k){
if(par[k]==k) return k;
return findPar(par[k]);
}
void merg(int a, int b, bool F){
a=findPar(a);
b=findPar(b);
if(a==b) return;
if(sz[a]>sz[b]) swap(a,b);
if(F) srb.push(a);
par[a]=b;
sz[b]+=sz[a];
}
void rollBack(){
int k;
while(!srb.empty()){
k=srb.top();
sz[par[k]]-=sz[k];
par[k]=k;
srb.pop();
}
}
struct edg{
int p,q,w;
};
edg edge[100001];
bool fff2(int x, int y){
return edge[x].w>edge[y].w;
}
struct query{
int t,e,nw,s,w,indx;
};
query qr[100001];
bool fff1(int x, int y){
return qr[x].w>qr[y].w;
}
int n,m,q,ans[100001];
bool f[100001];
vector<int> changed,unchanged,cqs;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long i;
cin>>n>>m;
for(i=1;i<=m;i++) cin>>edge[i].p>>edge[i].q>>edge[i].w;
cin>>q;
int qbsz=1500,qbbr,qbStart,qbEnd,j;
qbbr=q/qbsz;
if(q%qbsz>0) qbbr++;
for(j=0;j<qbbr;j++){
qbStart=j*qbsz+1;
qbEnd=min((j+1)*qbsz,q);
changed.clear();
unchanged.clear();
cqs.clear();
for(i=1;i<=m;i++) f[i]=0;
for(i=1;i<=n;i++){par[i]=i; sz[i]=1;}
for(i=qbStart;i<=qbEnd;i++){
cin>>qr[i].t;
qr[i].indx=i;
if(qr[i].t==1){
cin>>qr[i].e>>qr[i].nw;
f[qr[i].e]=1;
}
else{
cin>>qr[i].s>>qr[i].w;
cqs.push_back(i);
}
}
for(i=1;i<=m;i++){
if(f[i]==1) changed.push_back(i);
else unchanged.push_back(i);
}
sort(cqs.begin(),cqs.end(),fff1);
sort(unchanged.begin(),unchanged.end(),fff2);
/*for(int ch : unchanged) cout<<ch<<" ";
cout<<endl;*/
int eindx=0;
for(int tq : cqs){
//cout<<tq<<endl;
if(eindx<unchanged.size()) while(edge[unchanged[eindx]].w>=qr[tq].w){
merg(edge[unchanged[eindx]].p,edge[unchanged[eindx]].q,0);
eindx++;
if(eindx==unchanged.size()) break;
}
for(i=qbStart;i<qr[tq].indx;i++){
if(qr[i].t==1) swap(qr[i].nw,edge[qr[i].e].w);
}
for(int ed : changed) if(edge[ed].w>=qr[tq].w) merg(edge[ed].p,edge[ed].q,1);
//for(i=1;i<=n;i++) cout<<i<<" "<<par[i]<<" "<<sz[i]<<endl;
//cout<<sz[findPar(qr[tq].s)]<<'\n'<<endl;;
ans[qr[tq].indx]=sz[findPar(qr[tq].s)];
rollBack();
//for(i=1;i<=n;i++) cout<<i<<" "<<par[i]<<" "<<sz[i]<<endl;
for(i=qr[tq].indx-1;i>=qbStart;i--){
if(qr[i].t==1) swap(qr[i].nw,edge[qr[i].e].w);
}
}
for(i=qbStart;i<=qbEnd;i++) if(ans[i]!=0) cout<<ans[i]<<'\n';
for(i=qbStart;i<=qbEnd;i++){
if(qr[i].t==1) swap(qr[i].nw,edge[qr[i].e].w);
}
}
return 0;
}
Compilation message (stderr)
bridges.cpp:3:28: warning: bad option '-favx2' to pragma 'optimize' [-Wpragmas]
3 | #pragma GCC optimize("avx2")
| ^
bridges.cpp:11:18: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
11 | int findPar(int k){
| ^
bridges.cpp:15:31: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
15 | void merg(int a, int b, bool F){
| ^
bridges.cpp:24:15: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
24 | void rollBack(){
| ^
bridges.cpp:39:23: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
39 | bool fff2(int x, int y){
| ^
bridges.cpp:48:23: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
48 | bool fff1(int x, int y){
| ^
bridges.cpp:58:10: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
58 | int main()
| ^
bridges.cpp: In function 'int main()':
bridges.cpp:106:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | if(eindx<unchanged.size()) while(edge[unchanged[eindx]].w>=qr[tq].w){
| ~~~~~^~~~~~~~~~~~~~~~~
bridges.cpp:109:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | if(eindx==unchanged.size()) break;
| ~~~~~^~~~~~~~~~~~~~~~~~
# | 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... |