Submission #771475

#TimeUsernameProblemLanguageResultExecution timeMemory
771475siewjhBridges (APIO19_bridges)C++17
100 / 100
2220 ms9816 KiB
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; const int MAXN = 50005; const int MAXE = 100005; int par[MAXN], sz[MAXN]; bool ch[MAXE]; vector<pair<int, int>> jst; int root(int x){ if (par[x] == -1) return x; else return root(par[x]); } void join(int a, int b){ int ra = root(a), rb = root(b); if (ra == rb) return; if (sz[ra] < sz[rb]) swap(ra, rb); // ra larger par[rb] = ra; sz[ra] += sz[rb]; jst.push_back({ra, rb}); } void rollback(){ while (!jst.empty()){ int ra, rb; tie(ra, rb) = jst.back(); jst.pop_back(); par[rb] = -1; sz[ra] -= sz[rb]; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int nodes, edges; cin >> nodes >> edges; vector<int> ea(edges + 1), eb(edges + 1), ew(edges + 1); for (int i = 1; i <= edges; i++){ int a, b, w; cin >> a >> b >> w; ea[i] = a; eb[i] = b; ew[i] = w; } int queries; cin >> queries; vector<int> qt(queries), qx(queries), qw(queries); for (int i = 0; i < queries; i++){ int typ, ind, w; cin >> typ >> ind >> w; qt[i] = typ; qx[i] = ind; qw[i] = w; } vector<int> ans(queries, -1); const int bsz = 600; vector<int> dyna[bsz]; bool ch[MAXE]; for (int l = 0; l < queries; l += bsz){ int r = min(l + bsz, queries) - 1; fill(par + 1, par + nodes + 1, -1); fill(sz + 1, sz + nodes + 1, 1); vector<int> qv, stat, chv; for (int i = l; i <= r; i++){ if (qt[i] == 1) ch[qx[i]] = 1; } for (int i = 1; i <= edges; i++){ if (ch[i]) chv.push_back(i); else stat.push_back(i); } for (int i = l; i <= r; i++){ int x = qx[i], w = qw[i]; if (qt[i] == 1) ew[x] = w; else{ qv.push_back(i); dyna[i - l].clear(); for (int eind : chv) if (ew[eind] >= w) dyna[i - l].push_back(eind); } } sort(stat.begin(), stat.end(), [&](int a, int b){return ew[a] > ew[b];}); sort(qv.begin(), qv.end(), [&](int a, int b){return qw[a] > qw[b];}); int stind = 0; for (int qind : qv){ int w = qw[qind], x = qx[qind]; while (stind != stat.size() && ew[stat[stind]] >= w){ int eind = stat[stind]; join(ea[eind], eb[eind]); stind++; } jst.clear(); for (int eind : dyna[qind - l]) join(ea[eind], eb[eind]); ans[qind] = sz[root(x)]; rollback(); } for (int x : chv) ch[x] = 0; } for (int i = 0; i < queries; i++) if (ans[i] != -1) cout << ans[i] << '\n'; return 0; }

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:76:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |    while (stind != stat.size() && ew[stat[stind]] >= w){
      |           ~~~~~~^~~~~~~~~~~~~~
#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...