// Problem: A. DSU with rollback // Contest: Codeforces - ITMO Academy: pilot course - Disjoint Sets Union - Step 3 // Memory Limit: 256 MB // Time Limit: 2000 ms // Date / Time: 2022-04-20 22:42:16 // Author: cosenza // всё что ни делается - всё к лучшему // check list -> long long, special cases, array size, mod (a*b%p*c%p not a*b*c%p , (a-b+p)%p not a-b ) // // Powered by CP Editor (https://cpeditor.org) //Слава Україні, Героям слава 🇺🇦 #include using namespace std; const int N = 2e5 + 7; int par[N], cnt; vector> v; vector tmp; int find(int a) { return par[a] == a ? a : find(par[a]); } void unite(int u, int v) { if((u = find(u)) == (v = find(v))) { return; } par[u] = v; cnt--; tmp.push_back(u); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); for(int i = 0; i < N; i++) { par[i] = i; } int n, q; cin >> n >> q; cnt = n; string s; while(q--) { cin >> s; if(s == "union") { int u, v; cin >> u >> v; unite(u, v); cout << cnt << "\n"; } else if(s == "persist") { v.push_back(tmp); tmp.clear(); } else { for(auto e : tmp) { par[e] = e; cnt++; } tmp = v.back(); v.pop_back(); cout << cnt << "\n"; } } return 0; }