Submission #444917

#TimeUsernameProblemLanguageResultExecution timeMemory
444917JovanBBridges (APIO19_bridges)C++17
59 / 100
3090 ms406324 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int N = 50000; const int M = 100000; const int K = 500; int ea[M+5]; int eb[M+5]; int ec[M+5]; bool changed[M+5]; int qt[M+5]; int qa[M+5]; int qb[M+5]; struct DSU{ int n; int par[N+5]; int rnk[N+5]; int sz[N+5]; void init(int _n){ n = _n; for(int i=1; i<=n; i++){ par[i] = i; rnk[i] = 0; sz[i] = 1; } } struct op{ int a, b, rnka, rnkb, sza, szb; }; stack <op> st; int root(int x){ return (x == par[x] ? x : root(par[x])); } void povezi(int a, int b){ a = root(a); b = root(b); st.push({a, b, rnk[a], rnk[b], sz[a], sz[b]}); if(a == b) return; if(rnk[a] < rnk[b]) swap(a, b); if(rnk[a] == rnk[b]) rnk[a]++; sz[a] += sz[b]; par[b] = a; } void rollback(){ op o = st.top(); st.pop(); int a = o.a; int b = o.b; rnk[a] = o.rnka; rnk[b] = o.rnkb; sz[a] = o.sza; sz[b] = o.szb; par[a] = a; par[b] = b; } } dsu; int sol[M+5]; const int INF = 1000000007; vector <int> ima[M+5]; int gde[M+5]; int main(){ ios_base::sync_with_stdio(false), cin.tie(0); cout.precision(10); cout << fixed; int n, m; cin >> n >> m; for(int i=1; i<=m; i++){ cin >> ea[i] >> eb[i] >> ec[i]; } int qrs; cin >> qrs; int qq = 0; for(int ql=1; ql<=qrs; ql+=K){ int qr = min(qrs, ql+K-1); dsu.init(n); vector <pair <int, pair <int, int>>> vec; vector <int> ch; for(int i=ql; i<=qr; i++){ cin >> qt[i] >> qa[i] >> qb[i]; if(qt[i] == 1){ if(!changed[qa[i]]) ch.push_back(qa[i]); changed[qa[i]] = 1; } else{ qq++; gde[i] = qq; vec.push_back({qb[i], {qq, qa[i]}}); } } for(int i=1; i<=m; i++){ if(changed[i]) continue; vec.push_back({ec[i], {INF, i}}); } sort(vec.begin(), vec.end()); reverse(vec.begin(), vec.end()); for(int i=ql; i<=qr; i++){ if(qt[i] == 1) ec[qa[i]] = qb[i]; else{ for(auto c : ch){ if(ec[c] >= qb[i]) ima[gde[i]].push_back(c); } } } for(auto x : vec){ if(x.second.first == INF) dsu.povezi(ea[x.second.second], eb[x.second.second]); else{ int ind = x.second.first; int p = x.second.second; for(auto c : ima[ind]) dsu.povezi(ea[c], eb[c]); sol[ind] = dsu.sz[dsu.root(p)]; for(auto c : ima[ind]) dsu.rollback(); } } for(auto c : ch) changed[c] = 0; for(int i=ql; i<=qr; i++) if(gde[i]) ima[gde[i]].clear(); } for(int i=1; i<=qq; i++) cout << sol[i] << "\n"; return 0; }

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:121:26: warning: unused variable 'c' [-Wunused-variable]
  121 |                 for(auto c : ima[ind]) dsu.rollback();
      |                          ^
#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...