Submission #368000

#TimeUsernameProblemLanguageResultExecution timeMemory
368000Jarif_RahmanBridges (APIO19_bridges)C++17
0 / 100
3098 ms131164 KiB
#include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; const int N = 5e4, M = 1e5, Q = 1e5, SQ = 316; int sq; tuple<ll, int, int> edge[M]; struct dsu{ int n; vector<int> sz, p; stack<tuple<int, int, int>> st; dsu(int nn){ n = nn; sz.resize(n, 1); p.resize(n); for(int i = 0; i < n; i++) p[i] = i; } int get(int x){ if(p[x] != x) return get(p[x]); return p[x]; } int sz0(int x){ if(sz[x] == 0) return sz[p[x]]; return sz[x]; } int unite(int a, int b){ cout << a+1 << " " << b+1 << " d\n"; a = get(a), b = get(b); if(a == b) return 0; if(sz[b] > sz[a]) swap(a, b); st.push({a, b, sz[b]}); sz[a]+=sz[b]; sz[b] = 0; p[b] = a; return 1; } void rollback(){ auto [a, b, ss] = st.top(); st.pop(); sz[a]-=ss; sz[b] = ss; p[b] = b; } }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for(int i = 0; i < m; i++){ int a, b; ll w; cin >> a >> b >> w; a--, b--; edge[i] = make_tuple(w, a, b); } int q; cin >> q; vector<vector<tuple<ll, int, int>>> query; query.pb({}); for(int i = 0; i < q; i++){ int tt, s; ll w; cin >> tt >> s >> w; s--; if(query.back().size() >= SQ) query.pb({}); query.back().pb({tt, s, w}); } vector<int> ans; dsu ds(N); vector<tuple<ll, int, int>> calc; bool ucng[M]; for(int i = 0; i < m; i++) ucng[i] = 1; tuple<ll, int, int> edges[M]; vector<int> cng; for(auto qq: query){ int cnt = 0; for(auto [tt, s, w]: qq){ if(tt == 1){ ucng[s] = 0; } else{ cnt++; calc.pb({w, s, cnt-1}); } } int inx = 0; for(int i = 0; i < m; i++){ if(ucng[i]) edges[inx++] = edge[i]; else cng.pb(i); } vector<int> anss(cnt); vector<vector<pair<int, int>>> upd(cnt); cnt = 0; for(auto [tt, s, w]: qq){ if(tt == 1){ get<0>(edge[s]) = w; } else{ for(int x: cng){ auto [ww, a, b] = edge[x]; if(ww >= w) upd[cnt].pb({a, b}); } cnt++; } } sort(edges, edges+inx); sort(calc.rbegin(), calc.rend()); int bc = inx-1; for(auto [w, s, in]: calc){ while(bc >= 0 && get<0>(edges[bc]) >= w){ auto [ww, a, b] = edges[bc]; bc--; ds.unite(a, b); } int k = 0; for(auto [a, b]: upd[in]) k+=ds.unite(a, b); anss[in] = ds.sz0(0); while(k--) ds.rollback(); } ans.insert(ans.end(), anss.begin(), anss.end()); calc.clear(); for(int i = 0; i < m; i++) ucng[i] = 1; cng.clear(); while(!ds.st.empty()) ds.rollback(); } for(int x: ans) cout << x << "\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...