Submission #979477

#TimeUsernameProblemLanguageResultExecution timeMemory
979477nninBridges (APIO19_bridges)C++17
100 / 100
1490 ms18500 KiB
#include<bits/stdc++.h> using namespace std; #define pii pair<int,int> #define pip pair<int,pii> #define f first #define s second int n, m, q; vector<pip> edges, query; vector<pii> bridge[1005]; int ans[100005], part = 1000; bool change[100005]; vector<pii> ord; int dsu[50005], sz[50005]; int par(int x) { if(dsu[x]==x) return x; return par(dsu[x]); } void init() { for(int i=1;i<=n;i++) { dsu[i] = i; sz[i] = 1; } for(int i=0;i<m;i++) { change[i] = 0; } ord.clear(); } void join(int a, int b) { int pa = par(a); int pb = par(b); if(pa==pb) return; if(sz[pa]<sz[pb]) swap(pa, pb); ord.push_back({pa, pb}); dsu[pb] = pa; sz[pa] += sz[pb]; } void undo(int szz) { while(ord.size()>szz) { auto [pa, pb] = ord.back(); ord.pop_back(); sz[pa] -= sz[pb]; dsu[pb] = pb; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n>>m; for(int i=0;i<m;i++) { int u, v, w; cin>>u>>v>>w; edges.push_back({w, {u, v}}); } cin>>q; for(int i=0;i<q;i++) { int t, x, y; cin>>t>>x>>y; if(t==1) x--; query.push_back({t, {x, y}}); } for(int st=0;st<q;st+=part) { int en = min(st+part, q); init(); vector<pip> qu, ed; vector<int> one; for(int i=st;i<en;i++) { if(query[i].f==1) { change[query[i].s.f] = 1; one.push_back(i); } else { qu.push_back({query[i].s.s, {query[i].s.f, i}}); } } for(int i=0;i<m;i++) { if(!change[i]) { ed.push_back(edges[i]); } } cout<<'\n'; for(int i=st;i<en;i++) { if(query[i].f==1) { edges[query[i].s.f].f = query[i].s.s; } else { bridge[i-st].clear(); for(int q:one) { if(edges[query[q].s.f].f>=query[i].s.s) bridge[i-st].push_back(edges[query[q].s.f].s); } } } sort(qu.begin(), qu.end()); sort(ed.begin(), ed.end()); int e = ed.size()-1; for(int i=qu.size()-1;i>=0;i--) { int w = qu[i].f; int land = qu[i].s.f; int idx = qu[i].s.s; while(e>=0 && ed[e].f>=w) { join(ed[e].s.f, ed[e].s.s); e--; } int szz = ord.size(); for(pii p:bridge[idx-st]) { join(p.f, p.s); } ans[idx] = sz[par(land)]; undo(szz); } } for(int i=0;i<q;i++) if(query[i].f==2) cout<<ans[i]<<'\n'; }

Compilation message (stderr)

bridges.cpp: In function 'void undo(int)':
bridges.cpp:40:21: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   40 |     while(ord.size()>szz) {
      |           ~~~~~~~~~~^~~~
#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...