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;
#pragma GCC optimize("O3")
#pragma GCC target ("sse4")
#define X first
#define Y second
#define pb push_back
set<pair<int,int> > s;
vector<pair<int,int> > g;
vector<tuple<int,int,int> > answer;
stack<pair<int,int> > stk;
int n,m,num=1200,t,u[100005],v[100005],w[100005],e[100005];
int op[100005],a[100005],b[100005],mx=-1,group[100005],it1,it2;
int p[50005],sz[50005],e2[100005],ans[100005],hsh[100005];
int l,r,pasin,nathan,bothtoq,uaytaq;
int f(int a)
{
while(p[a]!=a)a=p[a];
return a;
}
stack<int> sack;
void un(int b,int c)
{
if(b>c)swap(b,c);
sz[c]+=sz[b];
p[b]=c;
}
void rollback(int x) {
while (sack.size() > x) {
int k = sack.top();
sack.pop();
sz[p[k]] -= sz[k];
p[k] = k;
}
}
void iterate()
{
for(auto it=s.begin();it!=s.end();it++)
{
printf("krit=%d %d\n",(*it).X,(*it).Y);
}
printf("\n");
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u[i],&v[i],&w[i]);
e[i]=w[i];
s.insert({e[i],i});
}
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
scanf("%d%d%d",&op[i],&a[i],&b[i]);
group[i]=(i-1)/num+1;
mx=max(mx,group[i]);
}
for(int i=1;i<=mx;i++)
{
l=(i-1)*num+1;
r=min(t,i*num);
g.clear();
answer.clear();
if(s.size()!=0)
for(auto it=s.begin();it!=s.end();it++)
{
stk.push({(*it).X,(*it).Y});
}
while(!stk.empty())
{
g.pb({stk.top().X,stk.top().Y});
stk.pop();
}
for(int j=1;j<=m;j++)hsh[j]=0;
for(int j=l;j<=r;j++)
{
if(op[j]==2)
{
answer.pb({b[j],a[j],j});
}else ++hsh[a[j]];
}
sort(answer.rbegin(),answer.rend());
it1=0,it2=0;
for(int j=1;j<=n;j++)p[j]=j,sz[j]=1;
for(int j=1;j<=m;j++)e2[j]=e[j];
while(it2<(int)answer.size())
{
auto [w2,node,idx]=answer[it2];
if(it1<(int)g.size())
{
int w=g[it1].X,edge=g[it1].Y;
if(w>=w2)
{
///////////
bothtoq=f(u[edge]);
uaytaq=f(v[edge]);
if(bothtoq!=uaytaq&&hsh[edge]==0)
{
un(bothtoq,uaytaq);
}
//////////
++it1;
continue;
}
}
for(int j=l;j<=r;j++)
{
if(op[j]==1&&j<idx)
{
e2[a[j]]=b[j];
}
}
for(int j=l;j<=r;j++)
{
if(op[j]==1)
{
if(e2[a[j]]>=b[idx])
{
pasin=f(u[a[j]]);
nathan=f(v[a[j]]);
if(pasin!=nathan)
{
if(sz[pasin]>sz[nathan])swap(pasin,nathan);
sack.push(pasin);
sz[nathan]+=sz[pasin];
p[pasin]=nathan;
}
}
}
}
ans[idx]=sz[f(node)];
for(int j=l;j<=r;j++)
{
if(op[j]==1&&j<idx)e2[a[j]]=e[a[j]];
}
while(!sack.empty())
{
sz[p[sack.top()]]-=sz[sack.top()];
p[sack.top()]=sack.top();
sack.pop();
}
++it2;
}
for(int j=l;j<=r;j++)
{
if(op[j]==1)
{
if(s.find({e[a[j]],a[j]})!=s.end())
{
s.erase(s.find({e[a[j]],a[j]}));
}
e[a[j]]=b[j];
s.insert({e[a[j]],a[j]});
}
}
}
for(int i=1;i<=t;i++)
{
if(op[i]==2)printf("%d\n",ans[i]);
}
}
Compilation message (stderr)
bridges.cpp: In function 'void rollback(int)':
bridges.cpp:29:21: warning: comparison of integer expressions of different signedness: 'std::stack<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
29 | while (sack.size() > x) {
| ~~~~~~~~~~~~^~~
bridges.cpp: In function 'int main()':
bridges.cpp:91:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
91 | auto [w2,node,idx]=answer[it2];
| ^
bridges.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
bridges.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d%d%d",&u[i],&v[i],&w[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d",&t);
| ~~~~~^~~~~~~~~
bridges.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d%d",&op[i],&a[i],&b[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |