-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicDsu.cpp
121 lines (118 loc) · 2.26 KB
/
dynamicDsu.cpp
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
struct edge{
int a,b,l,r;
edge(int a,int b,int l,int r)
{
this->a=a;
this->b=b;
this->l=l;
this->r=r;
}
};
struct DynamicDsu{
struct change{
int u,v,ru,rv;
change(int _u,int _v,int _ru,int _rv) :u(_u),v(_v),ru(_ru),rv(_rv){}
};
int sz;
int cc;
vi par;
vi rnk;
stack<change> op;
DynamicDsu(){}
DynamicDsu(int n)
{
sz =n;
cc = n;
par.resize(n);
rnk.resize(n);
REP(i,0,n)
{
par[i]=i;
rnk[i]=0;
}
}
int find(int x)
{
return par[x] ==x?x:find(par[x]);
}
bool merge(int a, int b)
{
int x=find(a);
int y=find(b);
if(x == y)
return false;
cc--;
if(rnk[y]>rnk[x])
swap(y,x);
op.push(change(y,x,rnk[y],rnk[x]));
if (rnk[x]==rnk[y])
rnk[x]++;
return true;
}
void rollback()
{
if (op.empty())
return;
change did = op.top();
op.pop();
cc++;
par[did.u]=did.u;
par[did.v]=did.v;
rnk[did.u]=did.ru;
rnk[did.v]=did.rv;
}
};
struct event{
int u,v;
bool good;
event(int _u,int _v):u(_u),v(_v){}
};
struct QueryTree{
vector<vector<event> > tree;
int time;
vi ans;
DynamicDsu dsu;
QueryTree(int T,int n)
{
dsu = DynamicDsu(n);
time =T;
tree.resize(4*T);
ans.resize(T);
ans[0]=n;
}
void addIt(int id,int l,int r, int el,int er, event &e)
{
if (l<=el&&er<=r)
{
tree[id].PB(e);
return ;
}
if (l >er ||r < el)
return;
int mid=(l+r)/2;
addIt(2*id,l,mid,el,er,e);
addIt(2*id+1,mid+1,r,el,er,e);
}
void addEvent(int l, int r, event e)
{
addIt(1,0,time-1,l,r,e);
}
void dfs(int id,int l,int r)
{
for(event &e :tree[id])
e.good=dsu.merge(e.u,e.v);
if (l==r)
ans[l]=dsu.cc;
else
{
int mid=(l+r)/2;
dfs(2*id,l,mid);
dfs(2*id+1,mid+1,r);
}
}
vi solve()
{
dfs(1,0,time-1);
return ans;
}
};