Skip to content

Commit 7b165d9

Browse files
authored
Create DSU with size of union,num of union
1 parent a10aae4 commit 7b165d9

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

DSU with size of union,num of union

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
Author: Fuadul Hasan([email protected])
3+
BSMRSTU,Gopalganj
4+
*/
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
void print(int n) {printf("%d ", n);}
8+
void printl(long long n) {printf("%I64d ", n);}
9+
long long readline(){long long x;scanf("%I64d", &x);return x;}
10+
/*
11+
1.#define gcd(a,b) __gcd(a,b) 2. #define lcm(a,b) (a*b)/gcd(a,b) 3. #define PI acos(-1.0) 4.#define sqr(x) ((x)*(x))
12+
5.#define min3(a,b,c) min(a,min(b,c))6.#define min4(a,b,c,d) min(d,min(a,min(b,c)))7.#define max3(a,b,c) max(a,max(b,c))
13+
8.#define max4(a,b,c,d) max(d,max(a,max(b,c)))
14+
*/
15+
//.............ignore it................//
16+
/*....... scanf.......*/
17+
template <typename T> inline void readline(T &n) {
18+
n = 0; int f = 1; register int ch = getchar();
19+
for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;
20+
for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';n = n * f;}
21+
template <typename T, typename TT> inline void readline(T &n, TT &m) { readline(n); readline(m); }
22+
template <typename T, typename TT, typename TTT> inline void readline(T &n, TT &m, TTT &l) { readline(n, m); readline(l);}
23+
/*..........error........*/
24+
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
25+
vector<string> split(const string &s, char c) {
26+
vector<string>v; stringstream ss(s); string x;
27+
while (getline(ss, x, c))v.emplace_back(x); return move(v);
28+
} void err(vector<string>::iterator it) {}
29+
template<typename T, typename... Args>void err(vector<string>::iterator it, T a, Args...args) {
30+
cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " "; err(++it, args...);}
31+
//............ignore it..................//
32+
typedef pair<int, int> ii;
33+
typedef std::vector<ii> vii;
34+
#define vi std::vector<int>
35+
#define vll std::vector<ll>
36+
#define all(n) n.begin(),n.end()
37+
#define mpsi std::map<string, int>
38+
#define pb push_back
39+
#define ll long long int
40+
#define nl printf("\n");
41+
#define happy cin.tie(0);
42+
#define coding ios::sync_with_stdio(false);
43+
44+
#define max3(a,b,c) max(a,max(b,c))
45+
46+
47+
int dx[8]= {1,0,-1,0,-1,-1,1,1};
48+
int dy[8]= {0,1,0,-1,-1,1,-1,1};
49+
const int mod = 1e9 + 7;
50+
const int inf = (int)2e9 + 5;
51+
const ll Inf = (ll)2e18 + 5;
52+
const int N = 4e5 + 5;
53+
const int N1 = 1e5 + 5;
54+
bool vis[N1];
55+
int a[N1];
56+
vector<int>g[N1];
57+
int par[N1],sz[N1];
58+
59+
int find(int u){
60+
if(par[u] == u){
61+
return u;
62+
}else{
63+
return find(par[u]);
64+
}
65+
}
66+
67+
void Union(int u, int v){
68+
u = find(u);
69+
v = find(v);
70+
if(u == v) return ;
71+
if(sz[u]<sz[v]) swap(u,v);
72+
par[v] = u;
73+
sz[u] += sz[v];
74+
}
75+
76+
int solve()
77+
{
78+
//happy coding
79+
80+
int n,m;
81+
readline(n,m);
82+
for(int i=1;i<=n;i++){
83+
par[i] = i;sz[i] = 1;
84+
}
85+
86+
for(int i=0;i<m;i++){
87+
int a,b;
88+
readline(a,b);
89+
Union(a,b);
90+
}
91+
set<int > root;
92+
for(int i=1;i<=n;i++){
93+
root.insert(find(i));
94+
}
95+
for(int s : root){
96+
cout<<sz[s];
97+
}nl;
98+
cout<<root.size()<<endl;
99+
100+
101+
return 0;
102+
103+
//error();
104+
}
105+
106+
int main(){
107+
108+
int test = 1, tc = 0;
109+
//scanf("%d", &test);//(void)getchar();
110+
while (test--){
111+
solve();
112+
}
113+
114+
115+
return 0;
116+
}

0 commit comments

Comments
 (0)