Skip to content

Commit 0521ad3

Browse files
authored
qwq
1 parent 38db918 commit 0521ad3

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

solutions/advanced/mmcc-inaho.mdx

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
id: mmcc-inaho
3+
source: MMCC
4+
title: Inaho
5+
author: Chongtian Ma
6+
---
7+
8+
<Info title="Samples">
9+
To easily test your code from samples, copy and paste the following main function to the bottom of your code.
10+
11+
<LanguageSection>
12+
13+
<CPPSection>
14+
15+
```cpp
16+
int main(){
17+
int n, q; cin >> n >> q;
18+
Init(n);
19+
while(q--){
20+
int t; cin >> t;
21+
if(t == 1){
22+
int u, v; cin >> u >> v;
23+
AddEdge(u, v);
24+
}
25+
if(t == 2){
26+
RemoveLastEdge();
27+
}
28+
if(t == 3){
29+
int u; cin >> u;
30+
cout << GetSize(u) << endl;
31+
}
32+
}
33+
}
34+
```
35+
36+
Input:
37+
```
38+
5 10
39+
1 1 2
40+
3 1
41+
1 2 3
42+
3 2
43+
1 3 4
44+
3 3
45+
1 4 5
46+
3 4
47+
2
48+
3 1
49+
```
50+
51+
Expected Output:
52+
```
53+
2
54+
3
55+
4
56+
5
57+
4
58+
```
59+
</CPPSection>
60+
61+
</LanguageSection>
62+
</Info>
63+
64+
## Implementation
65+
66+
This is a standard test of your DSU with rollback template.
67+
68+
<LanguageSection>
69+
70+
<CPPSection>
71+
72+
```cpp
73+
#include <bits/stdc++.h>
74+
using namespace std;
75+
76+
struct DSU {
77+
vector<int> p, sz;
78+
79+
// stores history
80+
vector<pair<int&, int>> psnap, szsnap;
81+
82+
DSU(int n) {
83+
p.resize(n);
84+
sz.resize(n, 1);
85+
iota(p.begin(), p.end(), 0);
86+
}
87+
88+
int get(int x) { return (p[x] == x) ? x : get(p[x]); }
89+
90+
void unite(int a, int b) {
91+
a = get(a);
92+
b = get(b);
93+
if (sz[a] < sz[b]) { swap(a, b); }
94+
95+
// add to history
96+
szsnap.push_back({sz[a], sz[a]});
97+
psnap.push_back({p[b], p[b]});
98+
99+
if (a != b) {
100+
p[b] = a;
101+
sz[a] += sz[b];
102+
}
103+
}
104+
105+
void rollback() {
106+
szsnap.back().first = szsnap.back().second;
107+
szsnap.pop_back();
108+
psnap.back().first = psnap.back().second;
109+
psnap.pop_back();
110+
}
111+
};
112+
113+
const int MAXN = 500000;
114+
DSU dsu(MAXN);
115+
116+
void Init(int n){
117+
118+
}
119+
120+
void AddEdge(int u, int v){
121+
dsu.unite(--u, --v);
122+
}
123+
124+
void RemoveLastEdge(){
125+
dsu.rollback();
126+
}
127+
128+
int GetSize(int u){
129+
return dsu.sz[dsu.get(--u)];
130+
}
131+
```
132+
133+
</CPPSection>
134+
135+
</LanguageSection>

0 commit comments

Comments
 (0)