-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDsu with rollback
124 lines (111 loc) · 2.7 KB
/
Dsu with rollback
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//// offline query...
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define nn '\n'
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define UNIQUE(vec) vec.resize(distance(vec.begin(),unique(vec.begin(),vec.end()))) ;
#define all(vec) vec.begin(),vec.end()
#define int long long
#define pii pair<int,int>
#define pdd pair<double,double>
#define ff first
#define ss second
#define yes cout<<"YES\n" ;
#define no cout<<"NO\n" ;
#define ll long long
#define pll pii
using namespace std ;
typedef long long LL ;
const int MOD=1e9+7,Base=998244353 ;
const int SIZE=1e6+7 , N = 1e6+7 ;
const int oo=1LL*1000*1000*1000*1000*1000*1000+7LL ;
const double pie=acos(-1.0) ;
const double EPS=1e-9 ;
vector<pii> seg[8*N];
vector<array<int,4>>trace;
int n,m,k,par[N],sz[N],ans[N],cnt;
map<pii,int>mp;
int root(int x){
if(x==par[x])return x ;
return root(par[x]);
}
bool unite(int x,int y){
x=root(x) , y=root(y);
if(x!=y){
if(sz[x]<sz[y])swap(x,y);
trace.push_back({x,y,par[y],sz[x]});
par[y]=x,sz[x]+=sz[y]; cnt--; return true ;
}
return false;
}
void add(int id,int tl,int tr,int l,int r,pii e){
if(r<tl || tr<l)return;
if(l<=tl && tr<=r){
seg[id].push_back(e); return;
}
int m=(tl+tr)/2;
add(id*2,tl,m,l,r,e);
add(id*2+1,m+1,tr,l,r,e);
}
void rollback(){
array<int,4>x=trace.back();
trace.pop_back(),cnt++;
par[x[1]]=x[2];
sz[x[0]]=x[3];
}
void query(int id,int l,int r){
int t=0;
for(pii x:seg[id]){
if(unite(x.ff,x.ss))++t;
}
if(l==r){
ans[l]=cnt;
}else {
int m=(l+r)/2;
query(id*2,l,m);
query(id*2+1,m+1,r);
}
while(t--){
rollback();
}
}
int32_t main()
{
IOS
cin>>n>>m>>k;
cnt=n;
for(int i=1;i<=n;i++)par[i]=i , sz[i]=1;
for(int i=1;i<=m;i++){
int x,y;cin>>x>>y;
if(x>y)
swap(x,y);
mp[{x,y}]=0;
}
for(int i=1;i<=k;i++){
int type,x,y; cin>>type>>x>>y;
if(x>y)swap(x,y);
if(type==1){
mp[{x,y}]=i;
}
else{
add(1,0,k,mp[{x,y}],i-1,{x,y});
mp[{x,y}]=-1;
}
}
for(pair<pii,int>x:mp){
if(x.ss>=0)
add(1,0,k,x.ss,k,x.ff) ;
}
query(1,0,k);
for(int i=0;i<=k;i++){
cout<<ans[i]<<' ' ;
}
return 0 ;
}