Submission #1127188

#TimeUsernameProblemLanguageResultExecution timeMemory
1127188HossamHero7Bridges (APIO19_bridges)C++20
59 / 100
3094 ms5384 KiB
// In sha2 Allah IOI 2025 #include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' struct DSU{ vector<int> par; vector<int> sz; vector<pair<int,int>> rsz; vector<int> rpar; DSU(int n){ par.resize(n+1) , sz.resize(n+1,1); iota(par.begin(),par.end(),0); } int getP(int node){ if(node == par[node]) return node; return getP(par[node]); } bool join(int a,int b){ a = getP(a) , b = getP(b); if(a == b) return 0; if(sz[a] < sz[b]) swap(a,b); rsz.push_back({a,sz[a]}); rpar.push_back(b); par[b] = a; sz[a] += sz[b]; return 1; } void rollback(){ sz[rsz.back().first] = rsz.back().second; par[rpar.back()] = rpar.back(); rsz.pop_back(); rpar.pop_back(); } }; void solve(){ int n,m; cin>>n>>m; vector<array<int,3>> edges(m); for(auto &[a,b,c] : edges) cin>>a>>b>>c; int q; cin>>q; vector<array<int,3>> Q(q); for(auto &[t,a,b] : Q) cin>>t>>a>>b; vector<int> ans(q,-1); int changed[m]; auto cmp = [&](int a,int b){ return Q[a][2] > Q[b][2]; }; auto cmp2 = [&](int a,int b){ return edges[a][2] > edges[b][2]; }; for(int l=0;l<q;l+=320){ int r = min(q-1,l+319); for(int i=0;i<m;i++) changed[i] = 1e9; vector<int> un; vector<array<int,3>> ch_ed; vector<int> qs; for(int i=r;i>=l;i--){ if(Q[i][0] == 1) ch_ed.push_back({i,changed[Q[i][1]-1],Q[i][2]}) , changed[Q[i][1]-1] = i; else qs.push_back(i); } for(int i=0;i<m;i++){ if(changed[i] == 1e9) un.push_back(i); else ch_ed.push_back({-1,changed[i],edges[i][2]}); } sort(un.begin(),un.end(),cmp2); sort(qs.begin(),qs.end(),cmp); DSU ds(n); int pt = 0; for(auto idx : qs){ int w = Q[idx][2] , node = Q[idx][1]; while(pt < (int)un.size() && edges[un[pt]][2] >= w) ds.join(edges[un[pt]][0],edges[un[pt]][1]) , pt ++; int cnt = 0; for(auto [t,nxt,c] : ch_ed){ int e = 0; if(t == -1) e = Q[nxt][1] - 1; else e = Q[t][1] - 1; if(t < idx && nxt > idx && c >= w){ if(ds.join(edges[e][0],edges[e][1])) cnt ++; } } ans[idx] = ds.sz[ds.getP(node)]; while(cnt--) ds.rollback(); } for(auto [t,nxt,c] : ch_ed){ if(nxt == 1e9 && t != -1) edges[Q[t][1]-1][2] = Q[t][2]; } } for(int i=0;i<q;i++){ if(ans[i] == -1) continue; cout<<ans[i]<<endl; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; //cin>>t; while(t--){ solve(); } return 0; }
#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...