Skip to content

Commit 35b75d5

Browse files
Merge pull request matthewsamuel95#600 from anirudherabelly/master
added disjoint set application problems.
2 parents 8197e25 + af701a6 commit 35b75d5

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
You are given number of nodes N, and number of edges M of a undirected connected graph.
3+
After adding each edge, print the size of all the connected components (in increasing order).
4+
5+
Input:
6+
First line contains two integers N and M ,number of nodes and number of edges.
7+
Next M lines contains two integers each,
8+
X and Y, denoting that there is an edge between X and Y.
9+
10+
Output:
11+
For each edge, print the size of all the connected components (in increasing order) after adding that edge.
12+
13+
Constraints:
14+
1<=N<=10^3
15+
1<=M<=N-1
16+
1<=X,Y<=N-1
17+
18+
sample input:
19+
5 4
20+
1 2
21+
3 4
22+
4 5
23+
1 3
24+
25+
sample output:
26+
1 1 1 2
27+
1 2 2
28+
2 3
29+
5
30+
*/
31+
using System;
32+
using System.Numerics;
33+
class MyClass {
34+
static void Main(string[] args) {
35+
int[] nm = Array.ConvertAll(Console.ReadLine().Split(' '),Int32.Parse);
36+
int n = nm[0];
37+
int m = nm[1];
38+
int[] arr = new int[n];
39+
int[] size = new int[n];
40+
Intialize(arr,size,n);
41+
42+
for(int i = 0; i < m; i++)
43+
{
44+
int[] ver= Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
45+
WeightedUnion(arr,size,ver[0],ver[1]);
46+
DisplayConnected(size,n);
47+
}
48+
}
49+
50+
private static void DisplayConnected(int[] size, int n)
51+
{
52+
int[] temp=new int[n];
53+
Array.Copy(size,temp,n);
54+
Array.Sort(temp);
55+
for (int j=0; j < n; j++)
56+
{
57+
if(temp[j]>0)Console.Write(temp[j]+" ");
58+
}
59+
Console.WriteLine();
60+
}
61+
62+
private static void WeightedUnion(int[] arr, int[] size, int a, int b)
63+
{
64+
int root_a = root(a,arr);
65+
int root_b = root(b,arr);
66+
if (arr[root_a] == arr[root_b]) return;
67+
else if (size[root_a] < size[root_b])
68+
{
69+
arr[root_a] = arr[root_b];
70+
size[root_b] += size[root_a];
71+
size[root_a]=0;
72+
}
73+
else
74+
{
75+
arr[root_b] = arr[root_a];
76+
size[root_a] += size[root_b];
77+
size[root_b]=0;
78+
}
79+
}
80+
81+
private static int root(int a,int[] arr)
82+
{
83+
int i = a - 1;
84+
while (arr[i] != i)
85+
{
86+
i = arr[i];
87+
}
88+
return i;
89+
}
90+
91+
private static void Intialize(int[] arr,int[] size,int n)
92+
{
93+
for(int i = 0; i < n; i++)
94+
{
95+
arr[i] = i;
96+
size[i] = 1;
97+
}
98+
}
99+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Monk is having a hard time teaching the 2nd standard students.
3+
He wants to divide the students into small groups so that he can conduct
4+
some fun-filled activities for them. But students also want their friends in the same group. So, if student
5+
A is a friend of student B, and student B is a friend of student C, then the students
6+
A,B and C must be in the same group, otherwise they will start crying.
7+
After dividing the students, he will choose a leader from each group who will lead their respective groups.
8+
Now he wants to know the number of ways he can choose the group leaders from all the groups.
9+
Print this answer modulo 10^9+7.
10+
11+
Note: Two ways A and B will be considered different if at least 1 person is a leader in group A,
12+
and is not a leader in group B, or vice-versa.
13+
14+
Input:
15+
The first line consists of two integers N and M denoting the number of students and the number of relationships respectively.
16+
The next M lines consists of two integers
17+
u and v denoting that student u and student v are friends.
18+
u and v can never be equal and relationships are not repeated.
19+
20+
output:
21+
Print the answer modulo 10^9+7 in a single line.
22+
*/
23+
using System;
24+
using System.Numerics;
25+
class MyClass {
26+
static void Main(string[] args)
27+
{
28+
int[] _nm= Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
29+
int n = _nm[0];
30+
int m = _nm[1];
31+
32+
int[] students = new int[n];
33+
long[] size = new long[n];
34+
Initialize(students,size,n);
35+
for(int i = 0; i < m; i++)
36+
{
37+
int[] ver= Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
38+
WeightedUnion(students,size,ver[0]-1,ver[1]-1);
39+
}
40+
long product = 1;
41+
for(int i = 0; i < n; i++)
42+
{
43+
if (size[i] != 0) product *= size[i];
44+
product %= (long)(Math.Pow(10, 9) + 7);
45+
}
46+
Console.WriteLine(product);
47+
}
48+
private static void WeightedUnion(int[] students, long[] size, int a, int b)
49+
{
50+
int root_a = root(a,students);
51+
int root_b = root(b,students);
52+
if (students[root_a] == students[root_b]) return;
53+
else if (size[root_a] < size[root_b])
54+
{
55+
students[root_a] = students[root_b];
56+
size[root_b] += size[root_a];
57+
size[root_a]=0;
58+
}
59+
else
60+
{
61+
students[root_b] = students[root_a];
62+
size[root_a] += size[root_b];
63+
size[root_b] = 0;
64+
}
65+
}
66+
private static int root(int v, int[] students)
67+
{
68+
int i = v;
69+
while (students[i] != i)
70+
{
71+
i = students[i];
72+
}
73+
return i;
74+
}
75+
private static void Initialize(int[] students, long[] size, int n)
76+
{
77+
for(int i = 0; i < n; i++)
78+
{
79+
students[i] = i;
80+
size[i] = 1;
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)