Submission #639731

#TimeUsernameProblemLanguageResultExecution timeMemory
639731KiriLL1caBridges (APIO19_bridges)C++14
100 / 100
2523 ms9992 KiB
#include <iostream> #include <vector> #include <array> #include <algorithm> #include <numeric> #define fr first #define sc second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pw(x) (1ll << x) #define sz(x) (int)((x).size()) #define pb push_back #define endl '\n' using namespace std; typedef long long ll; typedef long double ld; typedef pair <int, int> pii; typedef pair <ll, ll> pll; template <typename T> inline bool umin (T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> inline bool umax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } const int N = 1e5 + 100; const int buben = 800; struct dsu { vector <int> p, rk; vector <array <int, 6>> st; dsu () {} dsu (int n) : p(n), rk(n, 1), st() { iota(all(p), 0); } inline int par (int x) { while (p[x] != x) x = p[x]; return x; } inline bool unite (int x, int y) { x = par(x), y = par(y); if (x == y) return 0; if (rk[x] < rk[y]) swap(x, y); st.pb({x, y, p[x], p[y], rk[x], rk[y]}); rk[x] += rk[y]; p[y] = x; return 1; } inline void rollback () { if (!sz(st)) { cout << "ebis"; exit(0); } auto [x, y, px, py, rkx, rky] = st.back(); st.pop_back(); p[x] = px; p[y] = py; rk[x] = rkx; rk[y] = rky; } inline bool same (int x, int y) { return par(x) == par(y); } inline int size (int x) { return rk[par(x)]; } }; inline void solve () { int n, m; cin >> n >> m; vector <array <int, 3>> edge (m); for (auto &[i, j, k] : edge) cin >> i >> j >> k, --i, --j; int q; cin >> q; vector <array <int, 3>> qry (q); for (auto &[t, i, j] : qry) cin >> t >> i >> j, --i; vector <int> ans (q, -1); for (int l = 0; l < q; l += buben) { int r = min(q, l + buben); vector <bool> chg (m); vector <array <int, 3>> ask, unchg; vector <int> upd; for (int i = l; i < r; ++i) { if (qry[i][0] == 1) { chg[qry[i][1]] = 1; upd.pb(qry[i][1]); } else { ask.pb({qry[i][2], qry[i][1], i}); } } for (int i = 0; i < m; ++i) if (!chg[i]) unchg.pb({edge[i][2], edge[i][0], edge[i][1]}); vector <vector <array <int, 3>>> add (buben); for (int i = l; i < r; ++i) { if (qry[i][0] == 1) { edge[qry[i][1]][2] = qry[i][2]; } else { for (auto j : upd) if (edge[j][2] >= qry[i][2]) add[i - l].pb(edge[j]); } } sort(rall(ask)); sort(rall(unchg)); int uk = 0; dsu D (n); for (auto [d, v, id] : ask) { while (uk < sz(unchg) && unchg[uk][0] >= d) { D.unite(unchg[uk][1], unchg[uk][2]); ++uk; } int cnt = 0; for (auto [u, v, w] : add[id - l]) { cnt += D.unite(u, v); } ans[id] = D.size(v); while (cnt--) D.rollback(); } } for (auto i : ans) if (~i) cout << i << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) solve(); return 0; }

Compilation message (stderr)

bridges.cpp: In member function 'void dsu::rollback()':
bridges.cpp:43:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   43 |                 auto [x, y, px, py, rkx, rky] = st.back(); st.pop_back();
      |                      ^
bridges.cpp: In function 'void solve()':
bridges.cpp:54:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   54 |         for (auto &[i, j, k] : edge) cin >> i >> j >> k, --i, --j;
      |                    ^
bridges.cpp:57:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   57 |         for (auto &[t, i, j] : qry) cin >> t >> i >> j, --i;
      |                    ^
bridges.cpp:90:27: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   90 |                 for (auto [d, v, id] : ask) {
      |                           ^
bridges.cpp:96:35: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   96 |                         for (auto [u, v, w] : add[id - l]) {
      |                                   ^
#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...