Submission #166495

#TimeUsernameProblemLanguageResultExecution timeMemory
166495FeederBridges (APIO19_bridges)C++11
59 / 100
3092 ms9044 KiB
#include "bits/stdc++.h" using namespace std; typedef long long ll; int par[50001], sz[50001], rnk[50001]; int findp(int x){ return x == par[x] ? x : findp(par[x]); } void merge(int x, int y){ x = findp(x), y = findp(y); if(x == y) return; if(rnk[x] < rnk[y]) swap(x, y); par[y] = x; if(rnk[x] == rnk[y]) rnk[x]++; sz[x] += sz[y]; } int edgea[100001], edgeb[100001], warr[100001], ans[100001]; set<tuple<int, int> > edgelist; vector<tuple<int, int, int> > uvec; // query, bridge, weight vector<tuple<int, int, int> > qvec; // weight, query, island int qcnt = 0; int cbridges[100001]; void findans(int qnum){ //cout << "findans: " << qnum << endl; vector<tuple<int, int, int> > st; vector<int> vec; int qw, qi, qq; tie(qw, qq, qi) = qvec[qnum]; int aa, bb, ww; for(int i=int(uvec.size())-1; i>=0; i--){ if(get<0>(uvec[i]) > qq) continue; int bnum = get<1>(uvec[i]); ww = get<2>(uvec[i]); if(cbridges[bnum] == 2) continue; cbridges[bnum] = 2; vec.push_back(bnum); if(ww < qw) continue; aa = edgea[bnum], bb = edgeb[bnum]; aa = findp(aa), bb = findp(bb); //cout << a << ' ' << b << ' ' << w << endl; if(aa == bb) continue; if(rnk[aa] < rnk[bb]) swap(aa, bb); if(rnk[aa] == rnk[bb]){ st.emplace_back(aa, bb, sz[aa]); rnk[aa]++; } else st.emplace_back(aa, bb, -sz[aa]); par[bb] = aa; sz[aa] += sz[bb]; } for(auto i:vec) cbridges[i] = 1; ans[qq] = sz[findp(qi)]; for(int i=int(st.size())-1; i>=0; i--){ tie(aa, bb, ww) = st[i]; if(ww > 0) rnk[aa]--; else ww = -ww; par[bb] = bb; sz[aa] = ww; } } int main(){ ios::sync_with_stdio(0); cin.tie(0); int n, m, a, b, w, q, t, rt; cin >> n >> m; for(int i=1; i<=m; i++){ cin >> edgea[i] >> edgeb[i] >> w; edgelist.insert(make_tuple(-w, i)); warr[i] = w; } cin >> q; fill(ans, ans + q, -1); rt = 610; for(int i=0; i<q; i++){ cin >> t >> a >> b; if(t == 1){ uvec.emplace_back(i, a, b); if(!cbridges[a]){ cbridges[a] = 1; uvec.emplace_back(-1, a, warr[a]); } } else qvec.emplace_back(b, i, a); if(i % rt != rt - 1 && i != q - 1) continue; qcnt = int(qvec.size()) - 1; sort(qvec.begin(), qvec.end()); sort(uvec.begin(), uvec.end()); for(int j=1; j<=n; j++) par[j] = j; fill(sz + 1, sz + n + 1, 1); fill(rnk + 1, rnk + n + 1, 0); for(auto it:edgelist){ tie(w, a) = it; w = -w; while(qcnt >= 0 && get<0>(qvec[qcnt]) > w) findans(qcnt--); if(cbridges[a]) continue; b = edgeb[a]; a = edgea[a]; //cout << "merge: " << a << ' ' << b << endl; merge(a, b); } while(qcnt >= 0) findans(qcnt--); for(int j=int(uvec.size())-1; j>=0; j--){ tie(qcnt, b, w) = uvec[j]; if(qcnt < 0) break; if(cbridges[b] == 0) continue; edgelist.erase(make_tuple(-warr[b], b)); edgelist.insert(make_tuple(-w, b)); warr[b] = w; cbridges[b] = 0; } uvec.clear(); qvec.clear(); } for(int i=0; i<q; i++){ if(ans[i] != -1) cout << ans[i] << '\n'; } }
#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...