Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
gzshawnliang committed Jan 24, 2019
1 parent 0ed2833 commit a57c6f9
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 1 deletion.
80 changes: 80 additions & 0 deletions Lib/UnionFindDisjointSets/unionfind_ds.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <bits/stdc++.h>
using namespace std;

struct UnionFind
{
private:
vector<int> p, rank, setSize;
int numSets;

public:
UnionFind(int N)
{
setSize.assign(N, 1);
numSets = N;
rank.assign(N, 0);
p.assign(N, 0);
for (int i = 0; i < N; i++)
p[i] = i;
}
int findSet(int i)
{
return (p[i] == i) ? i : (p[i] = findSet(p[i]));
}
bool isSameSet(int i, int j)
{
return findSet(i) == findSet(j);
}
void unionSet(int i, int j)
{
if (!isSameSet(i, j))
{
numSets--;
int x = findSet(i), y = findSet(j);
// rank is used to keep the tree short
if (rank[x] > rank[y])
{
p[y] = x;
setSize[x] += setSize[y];
}
else
{
p[x] = y;
setSize[y] += setSize[x];
if (rank[x] == rank[y])
rank[y]++;
}
}
}

int numDisjointSets()
{
return numSets;
}
int sizeOfSet(int i)
{
return setSize[findSet(i)];
}
};

int main()
{
printf("Assume that there are 5 disjoint sets initially\n");
UnionFind UF(5); // create 5 disjoint sets
printf("%d\n", UF.numDisjointSets()); // 5
UF.unionSet(0, 1);
printf("%d\n", UF.numDisjointSets()); // 4
UF.unionSet(2, 3);
printf("%d\n", UF.numDisjointSets()); // 3
UF.unionSet(4, 3);
printf("%d\n", UF.numDisjointSets()); // 2
printf("isSameSet(0, 3) = %d\n", UF.isSameSet(0, 3)); // will return 0 (false)
printf("isSameSet(4, 3) = %d\n", UF.isSameSet(4, 3)); // will return 1 (true)
for (int i = 0; i < 5; i++) // findSet will return 1 for {0, 1} and 3 for {2, 3, 4}
printf("findSet(%d) = %d, sizeOfSet(%d) = %d\n", i, UF.findSet(i), i, UF.sizeOfSet(i));
UF.unionSet(0, 3);
printf("%d\n", UF.numDisjointSets()); // 1
for (int i = 0; i < 5; i++) // findSet will return 3 for {0, 1, 2, 3, 4}
printf("findSet(%d) = %d, sizeOfSet(%d) = %d\n", i, UF.findSet(i), i, UF.sizeOfSet(i));
return 0;
}
2 changes: 1 addition & 1 deletion myCpps/!-OJcreater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using namespace std;

//***************************************
const string CPPfile = "throughTheDesert_UVA11935"; //*
const string CPPfile = "Friends_UVA10608"; //*
//***************************************

ifstream fin(CPPfile + ".cpp");
Expand Down
68 changes: 68 additions & 0 deletions myCpps/Friends_UVA10608.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <set>
#include <map>

using namespace std;

ifstream fin("Friends_UVA10608.in");
ofstream fout("Friends_UVA10608.out");

int _find(int u, vector<int> & p)
{
if (p[u] == u)
{
return u;
}
else
{
int ans = _find(p[u], p);

p[u] = ans;

return ans;
}
}

int main()
{
int testCaseTotal; fin >> testCaseTotal;

for (int t = 1; t <= testCaseTotal; ++t)
{
int n, m; fin >> n >> m;

vector<int> p(n + 1), g(n + 1, 0);
for (int i = 1; i <= n; ++i)
{
p[i] = i;
}

for (int c = 1; c <= m; ++c)
{
int u, v; fin >> u >> v;

p[_find(v, p)] = p[_find(u, p)];
}

int ans = 0;
for (int i = 1; i <= n; ++i)
{
++g[_find(i, p)];

ans = max(ans, g[p[i]]);
}

fout << ans << '\n';
}

return 0;
}
17 changes: 17 additions & 0 deletions myCpps/Friends_UVA10608.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
2
3 2
1 2
2 3
10 12
1 2
3 1
3 4
5 4
3 5
4 6
5 2
2 1
7 1
1 2
9 10
8 9
2 changes: 2 additions & 0 deletions myCpps/Friends_UVA10608.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
3
7
70 changes: 70 additions & 0 deletions myCpps/Friends_UVA10608OJ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <set>
#include <map>

using namespace std;


int _find(int u, vector<int> & p)
{
if (p[u] == u)
{
return u;
}
else
{
int ans = _find(p[u], p);

p[u] = ans;

return ans;
}
}

int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int testCaseTotal; cin >> testCaseTotal;

for (int t = 1; t <= testCaseTotal; ++t)
{
int n, m; cin >> n >> m;

vector<int> p(n + 1), g(n + 1, 0);
for (int i = 1; i <= n; ++i)
{
p[i] = i;
}

for (int c = 1; c <= m; ++c)
{
int u, v; cin >> u >> v;

p[_find(v, p)] = p[_find(u, p)];
}

int ans = 0;
for (int i = 1; i <= n; ++i)
{
++g[_find(i, p)];

ans = max(ans, g[p[i]]);
}

cout << ans << '\n';
}

cout.flush();
return 0;
}

69 changes: 69 additions & 0 deletions myCpps/ubiquitousReligions_UVA10583.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <set>
#include <map>

using namespace std;

ifstream fin("ubiquitousReligions_UVA10583.in");
ofstream fout("ubiquitousReligions_UVA10583.out");

int _find(int u, vector<int> & p)
{
if (p[u] == u)
{
return u;
}
else
{
int ans = _find(p[u], p);

p[u] = ans;

return ans;
}
}

int main()
{
int testCase = 0;
while (true)
{
++testCase;

int n, m; fin >> n >> m;

if (n + m == 0) break;

vector<int> p(n + 1);
for (int i = 1; i <= n; ++i)
{
p[i] = i;
}

for (int c = 1; c <= m; ++c)
{
int u, v; fin >> u >> v;

p[_find(v, p)] = p[_find(u, p)];
}

set<int> s;
for (int i = 1; i <= n; ++i)
{
s.insert(_find(i, p));
}

fout << "Case " << testCase << ": " << s.size() << '\n';
}

return 0;
}
49 changes: 49 additions & 0 deletions myCpps/ubiquitousReligions_UVA10583.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
3 3
1 1
2 2
3 3
3 3
1 1
1 1
1 1
20 20
12 4
6 11
17 7
10 7
7 17
10 16
14 16
19 15
9 17
10 12
9 19
9 15
5 15
19 11
4 3
10 13
18 2
8 18
17 8
12 17
4 3
1 2
3 4
1 3
0 0
6 changes: 6 additions & 0 deletions myCpps/ubiquitousReligions_UVA10583.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Case 1: 1
Case 2: 7
Case 3: 3
Case 4: 3
Case 5: 3
Case 6: 1
Loading

0 comments on commit a57c6f9

Please sign in to comment.