Submission #1019831

#TimeUsernameProblemLanguageResultExecution timeMemory
1019831_8_8_Bridges (APIO19_bridges)C++17
100 / 100
2585 ms10968 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 20, MOD = (int)1e9+7; int n,m; array<int,3> e[N],qr[N]; int q; int t[N],p[N],sz[N]; int bf[N]; vector<array<int,4>>rb; int vis[N],timer =0; int get(int v){ if(p[v] == v) return v; return get(p[v]); } bool merge(int a,int b){ a = get(a); b = get(b); if(a == b) return false; if(sz[a] > sz[b]){ swap(a,b); } rb.push_back({a,p[a],b,sz[b]}); p[a] = b; sz[b] += sz[a]; return 1; } void roll_back(){ array<int,4> k = rb.back(); rb.pop_back(); p[k[0]] = k[1]; sz[k[2]] = k[3]; } int res[N]; void test(){ cin >> n >> m; for(int i = 1;i <= m;i++){ cin >> e[i][0] >> e[i][1] >> e[i][2]; } cin >> q; for(int i = 1;i <= q;i++){ cin >> qr[i][0] >> qr[i][1] >> qr[i][2]; res[i] = -1; } const int b = 450; for(int i = 1;i <= q;i += b){ rb.clear(); int r = min(q,i+b-1); for(int j = 1;j <= n;j++){ p[j] = j; sz[j] = 1; } vector<pair<int,int>> un; vector<array<int,3>> c; for(int j = i;j <= r;++j){ if(qr[j][0] == 1){ t[qr[j][1]] = i; }else{ c.push_back({qr[j][2],qr[j][1],j}); } } for(int j = 1;j <= m;j++){ if(t[j] != i){ un.emplace_back(e[j][2],j); } } sort(un.rbegin(),un.rend()); sort(c.rbegin(),c.rend()); int it = 0,it1 = i; for(auto [w,s,id]:c){ while(it < (int)un.size() && un[it].first >= w){ int j = un[it].second; merge(e[j][0],e[j][1]); it++; } int l = 0; timer++; for(int j = id - 1;j >= i;j--){ if(qr[j][0] == 1){ if(vis[qr[j][1]] == timer) continue; vis[qr[j][1]] = timer; if(qr[j][2] >= w){ int k = qr[j][1]; if(merge(e[k][0],e[k][1])){ l++; } } } } for(int j = id + 1;j <= r;j++){ if(qr[j][0] == 1){ if(vis[qr[j][1]] == timer) continue; int k = qr[j][1]; vis[qr[j][1]] = timer; if(e[k][2] >= w){ if(merge(e[k][0],e[k][1])){ l++; } } } } res[id] = sz[get(s)]; while(l--){ roll_back(); } } for(int j = i;j <= r;++j){ if(qr[j][0] == 1){ e[qr[j][1]][2] = qr[j][2]; } } } for(int i = 1;i <= q;i++){ if(res[i] != -1)cout << res[i] << '\n'; } } int main() { ios_base::sync_with_stdio(false);cin.tie(0); int t = 1; // cin >> t; while(t--){ test(); } }

Compilation message (stderr)

bridges.cpp: In function 'void test()':
bridges.cpp:72:20: warning: unused variable 'it1' [-Wunused-variable]
   72 |         int it = 0,it1 = 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...