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 pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
//#pragma GCC optimize("O2,unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,sse,sse2,sse3,ssse3,sse4,popcnt")
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double db;
typedef pair<db,db> pdd;
typedef pair<ll,ll> pll;
const ll maxn=1e5+5;
const ll maxk=2e5+5;
const ll block_sz=317;
const ll inf=1e9;
const ll mod=111539786;
int n,m,q;
struct dsu_rollback
{
int par[maxn];
vector<pii> save;
void init()
{
for1(i,1,n) par[i]=-1;
save.clear();
}
int Find(int u)
{
return ((par[u]<0)?(u):Find(par[u]));
}
void Union(int u,int v)
{
// cerr<<"Union "<< u<< ' '<<v<<'\n';
if ((u=Find(u))==(v=Find(v))) return;
if (par[u]>par[v]) swap(u,v);
save.pb({v,par[v]});
par[u]+=par[v];
par[v]=u;
}
void Roll(int x)
{
// cerr<< "back "<<x<<'\n';
while (save.size()>x)
{
int v=save.back().fi;
int pv=save.back().se;
int u=par[v];
save.pop_back();;
par[u]-=pv;
par[v]=pv;
}
}
int Get(int u)
{
return par[Find(u)]*(-1);
}
}dsu;
struct Edge
{
int u,v,d,i;
bool operator <(const Edge& o) const
{
return d>o.d;
}
}e[maxn];
struct queri
{
int t,s,w;
bool operator <(const queri& o) const
{
return w>o.w;
}
}qr[maxn];
int x[maxn];
int ans[maxn];
vector<int> Lqr[maxn/block_sz +1][3];
int spe[maxn];
void sol()
{
cin >> n>> m;
for1(i,1,m)
{
int u,v,d; cin >> u>>v>>d;
e[i]={u,v,d,i};
}
sort(e+1,e+1+m);
for1(i,1,m) x[e[i].i]=i;
cin >> q;
for1(i,1,q)
{
int t,s,w; cin >> t>> s>> w;
if (t==1) s=x[s];
qr[i]={t,s,w};
Lqr[i/block_sz][t].pb(i);
// cerr<<"wtf "<< i<<'\n';
}
// for1(i,1,m)
// {
// cerr<<e[i].u<<" -> "<<e[i].v<<": "<<e[i].d<<'\n';
// }
// for1(i,1,q)
// {
// cerr<< qr[i].s<<' '<<qr[i].w<<'\n';
// }
// return;
for1(g,0,q/block_sz)
{
// sort(all(Lqr[g][1]));
sort(all(Lqr[g][2]));
for(int i: Lqr[g][1])
{
spe[qr[i].s]=1;
}
int j=1;
dsu.init();
for(int i: Lqr[g][2])
{
while (j<=m && e[j].d >= qr[i].w)
{
if (!spe[j])
{
// cerr<<"wtf "<< j<<' '<<e[j].u<<' '<<e[j].v<<'\n';
dsu.Union(e[j].u,e[j].v);
}
j++;
}
int sv=dsu.save.size();
for(int x: Lqr[g][1])
{
// if (i==3) cerr<< x<<'\n';
if (x< i){if(qr[x].w>= qr[i].w) dsu.Union(e[qr[x].s].u,e[qr[x].s].v);}
else if (e[qr[x].s].d>=qr[i].w) dsu.Union(e[qr[x].s].u,e[qr[x].s].v);
}
// if (i==3) cerr<< "here\n";
ans[i]=dsu.Get(qr[i].s);
// cerr<< g<<' '<<i<<' '<<sv<<'\n';
dsu.Roll(sv);
}
for(int i:Lqr[g][1])
{
e[qr[i].s].d=qr[i].w;
spe[qr[i].s]=0;
}
}
for1(i,1,q) if (qr[i].t==2) cout << ans[i]<<'\n';
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("RECTANGLE.inp","r",stdin);
// freopen("RECTANGLE.out","w",stdout);
int t=1;//cin >> t;
while (t--)
{
sol();
}
}
/*
C[i-1][i+j-1]*C[sz[u]-i][sz[u]+sz[v]-i]
*/
Compilation message (stderr)
bridges.cpp: In member function 'void dsu_rollback::Roll(long long int)':
bridges.cpp:53:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
53 | while (save.size()>x)
| ~~~~~~~~~~~^~
# | 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... |