|
| 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