Submission #385544

#TimeUsernameProblemLanguageResultExecution timeMemory
385544ntabc05101Bridges (APIO19_bridges)C++14
59 / 100
3078 ms70632 KiB
#include<bits/stdc++.h> using namespace std; const int block = 320; const int mx = 100000; int parent[5 + mx], sz[5 + mx]; stack<int> st; void init(int n) { iota(parent, parent + n, 0); fill(sz, sz + n, 1); } int find(int x) { return (x == parent[x] ? x: find(parent[x])); } void unite_set(int x, int y) { x = find(x), y = find(y); if (x == y) { return ; } if (sz[x] < sz[y]) { swap(x, y); } st.emplace(y); parent[y] = x; sz[x] += sz[y]; } void roll_back(int x) { while (st.size() > x) { int y = st.top(); st.pop(); sz[parent[y]] -= sz[y]; parent[y] = y; } } int n, m, q; int u[5 + mx], v[5 + mx], w[5 + mx]; int t[5 + mx], x[5 + mx], y[5 + mx]; bool changed[5 + mx]; vector<int> to_join[block]; int result[5 + mx]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> u[i] >> v[i] >> w[i]; u[i]--, v[i]--; } cin >> q; for (int i = 0; i < q; i++) { cin >> t[i] >> x[i] >> y[i]; x[i]--; } fill(changed, changed + m, false); for (int l = 0; l < q; l += block) { int r = min(l + block, q); init(n); vector<int> ask, upd, unchanged; for (int i = l; i < r; i++) { if (t[i] == 1) { changed[x[i]] = true; upd.emplace_back(i); } else { ask.emplace_back(i); } } for (int i = 0; i < m; i++) { if (!changed[i]) { unchanged.emplace_back(i); } else { changed[i] = false; } } for (int i = l; i < r; i++) { if (t[i] == 1) { w[x[i]] = y[i]; } else { to_join[i - l].clear(); for (auto& u: upd) { if (w[x[u]] >= y[i]) { to_join[i - l].emplace_back(x[u]); } } } } sort(begin(ask), end(ask), [&](auto const& a, auto const& b) { return y[a] > y[b]; }); sort(begin(unchanged), end(unchanged), [&](auto const& a, auto const& b) { return w[a] > w[b]; }); int p = 0; for (auto& i: ask) { while (p < unchanged.size() && w[unchanged[p]] >= y[i]) { unite_set(u[unchanged[p]], v[unchanged[p]]); p++; } int pre_sz = st.size(); for (auto& j: to_join[i - l]) { unite_set(u[j], v[j]); } result[i] = sz[find(x[i])]; roll_back(pre_sz); } } for (int i = 0; i < q; i++) { if (t[i] == 2) { cout << result[i] << "\n"; } } return 0; }

Compilation message (stderr)

bridges.cpp: In function 'void roll_back(int)':
bridges.cpp:34:26: warning: comparison of integer expressions of different signedness: 'std::stack<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   34 |         while (st.size() > x) {
      |                ~~~~~~~~~~^~~
bridges.cpp: In function 'int main()':
bridges.cpp:112:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |                         while (p < unchanged.size() && w[unchanged[p]] >= 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...