Submission #444919

#TimeUsernameProblemLanguageResultExecution timeMemory
444919JovanBBridges (APIO19_bridges)C++17
14 / 100
2691 ms344564 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int N = 50000; const int M = 100000; const int K = 500; int ea[M+5]; int eb[M+5]; int ec[M+5]; bool changed[M+5]; int qt[M+5]; int qa[M+5]; int qb[M+5]; struct DSU{ int n; int par[N+5]; int sz[N+5]; void init(int _n){ n = _n; for(int i=1; i<=n; i++){ par[i] = i; sz[i] = 1; } } struct op{ int a, b, sza, szb; }; stack <op> st; int root(int x){ return (x == par[x] ? x : root(par[x])); } void povezi(int a, int b){ a = root(a); b = root(b); st.push({a, b, sz[a], sz[b]}); if(a == b) return; if(sz[a] < sz[b]) swap(a, b); sz[a] += sz[b]; par[b] = a; } void rollback(){ op o = st.top(); st.pop(); int a = o.a; int b = o.b; sz[a] = o.sza; sz[b] = o.szb; par[a] = a; par[b] = b; } } dsu; int sol[M+5]; const int INF = 1000000007; vector <int> ima[M+5]; int gde[M+5]; set <pair <int, int>> unerased; int main(){ ios_base::sync_with_stdio(false), cin.tie(0); cout.precision(10); cout << fixed; int n, m; cin >> n >> m; for(int i=1; i<=m; i++){ cin >> ea[i] >> eb[i] >> ec[i]; unerased.insert({ec[i], i}); } int qrs; cin >> qrs; int qq = 0; for(int ql=1; ql<=qrs; ql+=K){ int qr = min(qrs, ql+K-1); dsu.init(n); vector <pair <int, pair <int, int>>> dvec; vector <int> ch; for(int i=ql; i<=qr; i++){ cin >> qt[i] >> qa[i] >> qb[i]; if(qt[i] == 1){ if(!changed[qa[i]]){ ch.push_back(qa[i]); unerased.erase({ec[qa[i]], qa[i]}); } changed[qa[i]] = 1; } else{ qq++; gde[i] = qq; dvec.push_back({qb[i], {qq, qa[i]}}); } } sort(dvec.begin(), dvec.end()); vector <pair <int, pair <int, int>>> vec; int j = 0; for(auto c : unerased){ while(j < dvec.size() && c.first >= dvec[j].first){ vec.push_back(dvec[j]); j++; } vec.push_back({c.first, {INF, c.second}}); } while(j < dvec.size()){ vec.push_back(dvec[j]); j++; } reverse(vec.begin(), vec.end()); for(int i=ql; i<=qr; i++){ if(qt[i] == 1) ec[qa[i]] = qb[i]; else{ for(auto c : ch){ if(ec[c] >= qb[i]) ima[gde[i]].push_back(c); } } } for(auto x : vec){ if(x.second.first == INF) dsu.povezi(ea[x.second.second], eb[x.second.second]); else{ int ind = x.second.first; int p = x.second.second; for(auto c : ima[ind]) dsu.povezi(ea[c], eb[c]); sol[ind] = dsu.sz[dsu.root(p)]; for(auto c : ima[ind]) dsu.rollback(); } } for(auto c : ch){ ch[c] = 0; unerased.insert({ec[c], c}); } for(int i=ql; i<=qr; i++) if(gde[i]) ima[gde[i]].clear(); } for(int i=1; i<=qq; i++) cout << sol[i] << "\n"; return 0; }

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:105:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |             while(j < dvec.size() && c.first >= dvec[j].first){
      |                   ~~^~~~~~~~~~~~~
bridges.cpp:111:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |         while(j < dvec.size()){
      |               ~~^~~~~~~~~~~~~
bridges.cpp:131:26: warning: unused variable 'c' [-Wunused-variable]
  131 |                 for(auto c : ima[ind]) dsu.rollback();
      |                          ^
#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...