Submission #1278816

#TimeUsernameProblemLanguageResultExecution timeMemory
1278816janson0109Bridges (APIO19_bridges)C++20
60 / 100
1959 ms10388 KiB
#include <bits/stdc++.h> #define F first #define S second #define lol ios::sync_with_stdio(false);cin.tie(NULL); typedef int ll; typedef long double ld; using namespace std; const ll M = 998244353; #define p3 tuple<ll,ll,ll> ll find(ll x, vector<ll> &p, vector<pair<ll,ll>> &ph, bool his) { if(p[x] == x) return x; //if(his) ph.push_back({x, p[x]}); return find(p[x], p, ph, his); //return p[x]; } bool unite(ll x, ll y, vector<ll> &p, vector<ll> &s, vector<pair<ll,ll>> &ph, vector<pair<ll,ll>> &sh, bool his) { ll xr = find(x, p, ph, his); ll yr = find(y, p, ph, his); if(xr == yr) return false; if(s[xr] < s[yr]) swap(xr,yr); if(his) { sh.push_back({xr, s[xr]}); ph.push_back({yr, p[yr]}); } s[xr]+=s[yr]; p[yr]=xr; return true; } void rollback(vector<ll> &p, vector<ll> &s, vector<pair<ll,ll>> &ph, vector<pair<ll,ll>> &sh) { while(!ph.empty()) { pair<ll,ll> pa = ph.back(); p[pa.F] = pa.S; ph.pop_back(); } while(!sh.empty()) { pair<ll,ll> pa = sh.back(); s[pa.F] = pa.S; sh.pop_back(); } } int main() { lol ll n, m; cin >> n >> m; vector<p3> e; set<p3, greater<p3>> es; for(ll i=0; i<m; i++) { ll u, v, d; cin >> u >> v >> d; u--;v--; e.push_back({d, u, v}); es.insert(e.back()); } ll q; cin >> q; vector<p3> qu(q); for(ll i=0; i<q; i++) cin >> get<0>(qu[i]) >> get<1>(qu[i]) >> get<2>(qu[i]); ll b = sqrtl(q); for(ll t=0; t < q; t+=b) { ll c = min(b, q-t); vector<bool> ch(m,0); for(ll i=0; i<c; i++) if(get<0>(qu[t+i]) == 1) ch[get<1>(qu[t+i]) - 1] = 1; vector<ll> mod; set<p3> mods; for(ll i=0; i<m; i++) if(ch[i]) mod.push_back(i); ll k = mod.size(); vector<ll> modm(m, -1); vector<ll> nd(k); for(ll i=0; i<k; i++) { nd[i] = get<0>(e[mod[i]]); modm[mod[i]] = i; mods.insert(e[mod[i]]); } vector<vector<ll>> ndm(c); for(ll i=0; i<c; i++) { if(get<0>(qu[t+i]) == 1) nd[modm[get<1>(qu[t+i]) - 1]] = get<2>(qu[t+i]); else ndm[i] = nd; } vector<p3> qus; for(ll i=0; i<c; i++) if(get<0>(qu[t+i]) == 2) qus.push_back({get<2>(qu[t+i]), get<1>(qu[t+i])-1, i}); sort(qus.begin(), qus.end(), greater<p3>()); vector<ll> p(n), s(n,1); iota(p.begin(), p.end(), 0); vector<ll> ans(c, -1); auto it = es.begin(); vector<pair<ll,ll>> ph, sh; for(auto & quer : qus) { while(it != es.end()) { if(mods.find(*it) != mods.end()) it++; else if(get<0>(quer) <= get<0>(*it)) {unite(get<1>(*it), get<2>(*it), p, s, ph, sh, 0); it++;} else break; } for(ll j=0; j<k; j++) if(get<0>(quer) <= ndm[get<2>(quer)][j]) unite(get<1>(e[mod[j]]), get<2>(e[mod[j]]), p, s, ph, sh, 1); ans[get<2>(quer)] = s[find(get<1>(quer), p, ph, 1)]; rollback(p, s, ph, sh); } for(ll i=0; i<c; i++) if(get<0>(qu[t+i]) == 1) { auto ite = es.find(e[get<1>(qu[t+i]) - 1]); if(ite == es.end()) e[1e9]; else es.erase(ite); get<0>(e[get<1>(qu[t+i]) - 1]) = get<2>(qu[t+i]); es.insert(e[get<1>(qu[t+i]) - 1]); } for(ll i=0; i<c; i++) if(ans[i] != -1) cout << ans[i] << '\n'; } return 0; }

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:100:38: warning: ignoring return value of 'constexpr std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = std::tuple<int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int> >; reference = std::tuple<int, int, int>&; size_type = long unsigned int]', declared with attribute 'nodiscard' [-Wunused-result]
  100 |             if(ite == es.end()) e[1e9];
      |                                      ^
In file included from /usr/include/c++/13/vector:66,
                 from /usr/include/c++/13/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:53,
                 from bridges.cpp:1:
/usr/include/c++/13/bits/stl_vector.h:1126:7: note: declared here
 1126 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
#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...