Submission #684821

#TimeUsernameProblemLanguageResultExecution timeMemory
684821vovamrBridges (APIO19_bridges)C++17
100 / 100
1210 ms13604 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #pragma GCC target ("avx2") #pragma GCC optimize ("O3") #pragma GCC optimize ("unroll-loops") #define fi first #define se second #define ll long long #define ld long double #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define pb push_back #define mpp make_pair #define ve vector using namespace std; using namespace __gnu_pbds; template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; const ll inf = 1e18; const int iinf = 1e9; typedef pair<ll, ll> pll; typedef pair<int, int> pii; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); } template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); } const int N = 5e4 + 100; int pr[N], rnk[N]; inline void build(int n) { for (int i = 0; i < n; ++i) { pr[i] = i, rnk[i] = 1; } } inline int par(int v) { if (pr[v] == v) return v; return (pr[v] = par(pr[v])); } inline void uni(int a, int b) { a = par(a), b = par(b); if (a == b) return; if (rnk[a] > rnk[b]) swap(a, b); pr[a] = b, rnk[b] += rnk[a]; } int bg = 0, en = 0; int Qe[N]; int used[N]; ve<int> gr[N]; inline int bfs(int v) { int ans = 0; used[v] = 1; bg = en = 0; Qe[en++] = v; while (bg != en) { int v = Qe[bg++]; ans += rnk[v]; for (const auto &to : gr[v]) { if (used[to]) continue; used[to] = 1; Qe[en++] = to; } } return ans; } constexpr int b = 350; const int M = 1e5 + 10; const int Q = 1e5 + 10; array<int,3> op[Q]; array<int,3> e[M]; array<int,3> que[Q]; int pq = 0; ve<array<int,3>> edges[Q]; array<int,4> sorted_e[M], nch[M]; int p = 0; bool used_in_block[M]; int ch[M]; int pc = 0; array<int,4> nw[M]; int pn = 0; array<int,4> result[M]; int prr = 0; inline void upd() { pn = 0; for (int i = 0; i < pc; ++i) { const auto &[v, u, w] = e[ch[i]]; nw[pn++] = {w, ch[i], v, u}; } sort(nw, nw + pn); int pa = 0, pb = 0; prr = 0; while (pa < p || pb < pn) { if (pa == p) result[prr++] = nw[pb++]; else if (pb == pn) result[prr++] = nch[pa++]; else result[prr++] = (nw[pb] < nch[pa] ? nw[pb++] : nch[pa++]); } for (int i = 0; i < prr; ++i) sorted_e[i] = result[i]; } inline void solve() { int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int v, u, w; cin >> v >> u >> w, --v, --u; e[i] = {v, u, -w}; sorted_e[i] = {-w, i, v, u}; } sort(sorted_e, sorted_e + m); int q; cin >> q; for (int i = 0; i < q; ++i) { int t; cin >> t; if (t == 1) { int id, w; cin >> id >> w; op[i] = {1, --id, -w}; } else { int v, w; cin >> v >> w; op[i] = {2, --v, -w}; } } ve<int> answer(q, -1); for (int l = 0; l < q; l += b) { build(n); int r = min(q - 1, l + b - 1); for (int i = l; i <= r; ++i) { const auto &[type, a, b] = op[i]; if (type == 1) used_in_block[a] = 1; } pc = p = 0; for (int i = 0; i < m; ++i) { if (used_in_block[i]) ch[pc++] = i; if (!used_in_block[sorted_e[i][1]]) { nch[p++] = sorted_e[i]; } } pq = 0; for (int i = l; i <= r; ++i) { const auto &[type, a, b] = op[i]; if (type == 2) { que[pq++] = {b, a, i}; for (int j = 0; j < pc; ++j) if (e[ch[j]][2] <= b) edges[i].pb(e[ch[j]]); } else { e[a][2] = b; } } sort(que, que + pq); int ptr = 0; for (int id = 0; id < pq; ++id) { const auto &[w, v, i] = que[id]; while (ptr < m && sorted_e[ptr][0] <= w) { if (!used_in_block[sorted_e[ptr][1]]) uni(sorted_e[ptr][2], sorted_e[ptr][3]); ++ptr; } for (auto &[a, b, W] : edges[i]) { a = par(a), b = par(b); gr[a].pb(b); gr[b].pb(a); } answer[i] = bfs(par(v)); used[par(v)] = 0; for (auto &[a, b, w] : edges[i]) { gr[a].clear(); gr[b].clear(); used[a] = used[b] = 0; } } for (int i = l; i <= r; ++i) { const auto &[type, a, b] = op[i]; edges[i].clear(); edges[i].shrink_to_fit(); if (type == 1) used_in_block[a] = 0; } upd(); } for (int i = 0; i < q; ++i) if (~answer[i]) cout << answer[i] << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int q = 1; // cin >> q; while (q--) solve(); cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl; }
#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...