Submission #948594

#TimeUsernameProblemLanguageResultExecution timeMemory
948594damwuanBridges (APIO19_bridges)C++17
100 / 100
987 ms6416 KiB
#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=1000; 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) { 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) { 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,s1; 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]; bool cc[maxn]; void sol() { cin >> n>> m; for1(i,1,m) { int u,v,d; cin >> u>>v>>d; e[i]={u,v,d,i}; } cin >> q; for1(i,1,q) { int t,s1,s,w; cin >> t>> s1>> w; if (t==1) s=x[s1]; else s=s1; qr[i]={t,s,w,s1}; Lqr[i/block_sz][t].pb(i); } for1(g,0,q/block_sz) { sort(e+1,e+1+m); for1(i,1,m) {x[e[i].i]=i;} sort(all(Lqr[g][2]),[](int x,int y) { return qr[x].w>qr[y].w; }); for(int i: Lqr[g][1]) { qr[i].s=x[qr[i].s1]; 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]) { dsu.Union(e[j].u,e[j].v); } j++; } int sv=dsu.save.size(); int moc=upper_bound(all(Lqr[g][1]),i)-Lqr[g][1].begin(); for2(xx,moc-1,0) { int x=Lqr[g][1][xx]; if (cc[qr[x].s]) continue; cc[qr[x].s]=1; if(qr[x].w>= qr[i].w) { dsu.Union(e[qr[x].s].u,e[qr[x].s].v); } } for1(xx,moc,(int)Lqr[g][1].size()-1) { int x=Lqr[g][1][xx]; if (cc[qr[x].s]) continue; cc[qr[x].s]=1; if (e[qr[x].s].d>=qr[i].w) dsu.Union(e[qr[x].s].u,e[qr[x].s].v); } for(int x: Lqr[g][1]) cc[qr[x].s]=0; ans[i]=dsu.Get(qr[i].s); 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("F.inp","r",stdin); // freopen("F.out","w",stdout); int t=1;//cin >> t; while (t--) { sol(); } } /* */

Compilation message (stderr)

bridges.cpp: In member function 'void dsu_rollback::Roll(int)':
bridges.cpp:49:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   49 |         while (save.size()>x)
      |                ~~~~~~~~~~~^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...