Submission #1012617

#TimeUsernameProblemLanguageResultExecution timeMemory
1012617DaoMinhBridges (APIO19_bridges)C++17
100 / 100
1427 ms77124 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const int maxN = 1e5 + 1; const int MOD = 1e9 + 7; const int B = 1000; int n, m, q; int u[maxN], v[maxN], w[maxN]; int o[maxN], x[maxN], y[maxN]; int mark[maxN], ans[maxN]; vector<int> add[maxN]; struct TDSU { vector<pii> S; vector<int> lab; void Init(int n) { lab.clear(); lab.resize(n + 1, -1); } int Find(int u) { return lab[u] < 0 ? u : Find(lab[u]); } void Unite(int u, int v) { int f = Find(u), s = Find(v); if (f == s) return; if (lab[f] > lab[s]) swap(f, s); S.push_back({s, lab[s]}); lab[f] += lab[s]; lab[s] = f; } void rollback(int sz) { while (S.size() > sz) { int s = S.back().first; int f = lab[s]; int sz = S.back().second; S.pop_back(); lab[f] -= sz; lab[s] = sz; } } } dsu; void ReadInput() { cin >> n >> m; for (int i = 1; i <= m; i++) cin >> u[i] >> v[i] >> w[i]; cin >> q; for (int i = 1; i <= q; i++) cin >> o[i] >> x[i] >> y[i]; } void Solve() { for (int l = 1; l <= q; l += B) { int r = min(l + B - 1, q); fill(mark + 1, mark + m + 1, 0); //O(m) dsu.Init(n); vector<int> unchange, upd, calc; for (int i = l; i <= r; i++) //O(B) { if (o[i] == 1) { mark[x[i]] = 1; upd.push_back(i); } else calc.push_back(i); } for (int i = 1; i <= m; i++) //O(m) if (!mark[i]) unchange.push_back(i); for (int i = l; i <= r; i++) // O(B ^ 2) { if (o[i] == 1) w[x[i]] = y[i]; else { add[i - l].clear(); //avoid MLE for (int j : upd) if (w[x[j]] >= y[i]) add[i - l].push_back(x[j]); } } sort(calc.begin(), calc.end(), [&](int i, int j) { return y[i] > y[j]; }); //O(BlogB) sort(unchange.begin(), unchange.end(), [&](int i, int j) { return w[i] > w[j]; }); //O(mlogm) int j = 0; for (int i : calc) //(mlogn + B^2logn) { while (j < unchange.size() && w[unchange[j]] >= y[i]) { dsu.Unite(u[unchange[j]], v[unchange[j]]); j++; } int PrevSz = dsu.S.size(); for (int k : add[i - l]) dsu.Unite(u[k], v[k]); ans[i] = -dsu.lab[dsu.Find(x[i])]; dsu.rollback(PrevSz); // O(B) } } //=> O((M + Q).logN.B) (smaller than this quite much because the actual size is Q / B); for (int i = 1; i <= q; i++) if (o[i] == 2) cout << ans[i] << '\n'; } #define task "" int main() { ios_base::sync_with_stdio(0); cin.tie(0); //freopen(task".INP", "r", stdin); //freopen(task".OUT", "w", stdout); int T = 1; //cin >> T; while (T--) { ReadInput(); Solve(); } }

Compilation message (stderr)

bridges.cpp: In member function 'void TDSU::rollback(int)':
bridges.cpp:39:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   39 |         while (S.size() > sz)
      |                ~~~~~~~~~^~~~
bridges.cpp: In function 'void Solve()':
bridges.cpp:105:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |             while (j < unchange.size() && w[unchange[j]] >= y[i])
      |                    ~~^~~~~~~~~~~~~~~~~
#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...