Submission #556931

#TimeUsernameProblemLanguageResultExecution timeMemory
556931JomnoiBridges (APIO19_bridges)C++17
100 / 100
2108 ms12372 KiB
#include <bits/stdc++.h> #define DEBUG false using namespace std; const int MAX_N = 5e4 + 10; const int MAX_M = 1e5 + 10; const int MAX_Q = 1e5 + 10; const int BLK = 1000; int parent[MAX_N], sz[MAX_N]; int U[MAX_M], V[MAX_M], D[MAX_M], changed[MAX_M]; int T[MAX_Q], X[MAX_Q], Y[MAX_Q], ans[MAX_Q]; vector <int> unchanged, update, query, tojoin[BLK]; stack <int> stk; int root(int u) { if(u == parent[u]) { return u; } return root(parent[u]); } void merge(int u, int v) { u = root(u), v = root(v); if(u == v) { return; } if(sz[u] < sz[v]) { swap(u, v); } sz[u] += sz[v]; parent[v] = u; stk.push(v); } void rollback(int timer) { while(stk.size() > timer) { int v = stk.top(); stk.pop(); sz[root(v)] -= sz[v]; parent[v] = v; } } int main() { cin.tie(nullptr)->sync_with_stdio(false); int N, M; cin >> N >> M; for(int i = 1; i <= M; i++) { cin >> U[i] >> V[i] >> D[i]; } int Q; cin >> Q; for(int i = 1; i <= Q; i++) { cin >> T[i] >> X[i] >> Y[i]; if(T[i] == 1) { changed[X[i]] = true; update.push_back(i); } else { query.push_back(i); } } for(int l = 1; l <= Q; l += BLK) { int r = min(Q, l + BLK - 1); iota(parent + 1, parent + N + 1, 1); fill(sz + 1, sz + N + 1, 1); unchanged.clear(), update.clear(), query.clear(); fill(changed + 1, changed + M + 1, false); while(!stk.empty()) { stk.pop(); } for(int i = l; i <= r; i++) { if(T[i] == 1) { changed[X[i]] = true; update.push_back(i); } else { query.push_back(i); } } for(int i = 1; i <= M; i++) { if(changed[i] == false) { unchanged.push_back(i); } } for(int i = l; i <= r; i++) { if(T[i] == 1) { D[X[i]] = Y[i]; } else { tojoin[i - l].clear(); for(auto u : update) { if(D[X[u]] >= Y[i]) { tojoin[i - l].push_back(X[u]); } } } } sort(query.begin(), query.end(), [&](const int &a, const int &b) { return Y[a] > Y[b]; }); sort(unchanged.begin(), unchanged.end(), [&](const int &a, const int &b) { return D[a] > D[b]; }); int j = 0; for(auto i : query) { while(j < unchanged.size() and D[unchanged[j]] >= Y[i]) { merge(U[unchanged[j]], V[unchanged[j]]); j++; } int timer = stk.size(); for(auto e : tojoin[i - l]) { merge(U[e], V[e]); } ans[i] = sz[root(X[i])]; rollback(timer); } } for(int i = 1; i <= Q; i++) { if(T[i] == 2) { cout << ans[i] << '\n'; } } return 0; }

Compilation message (stderr)

bridges.cpp: In function 'void rollback(int)':
bridges.cpp:38:22: warning: comparison of integer expressions of different signedness: 'std::stack<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   38 |     while(stk.size() > timer) {
      |           ~~~~~~~~~~~^~~~~~~
bridges.cpp: In function 'int main()':
bridges.cpp:119:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |             while(j < unchanged.size() and D[unchanged[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...