Advertisement
HashZayed

Disjoint set

Aug 13th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. class Solution {
  6.  
  7. //finding root of an element.
  8. static int root(ref int[] Arr,int i){
  9. while(Arr[ i ] != i)
  10. {
  11. Arr[ i ] = Arr[ Arr[ i ] ] ;
  12. i = Arr[ i ];
  13. }
  14. return i;
  15. }
  16.  
  17. /*modified union function where we connect the elements by changing the root of one of the element */
  18.  
  19. static void weightedUnion(ref int[] Arr,ref int[] size,int A,int B)
  20. {
  21. int root_A = root(ref Arr,A);
  22. int root_B = root(ref Arr,B);
  23. if(root_A==root_B) return;
  24. if(size[root_A] < size[root_B ])
  25. {
  26. Arr[ root_A ] = Arr[root_B];
  27. size[root_B] += size[root_A];
  28. }
  29. else
  30. {
  31. Arr[ root_B ] = Arr[root_A];
  32. size[root_A] += size[root_B];
  33. }
  34.  
  35. }
  36. static bool find(ref int[] Arr, int A,int B){
  37. if( root(ref Arr,A)==root(ref Arr,B) ) //if A and B have same root,means they are connected.
  38. return true;
  39. else
  40. return false;
  41. }
  42. static void PrintArray(int[] arr,int n){
  43. for(int i=0;i<n;i++){
  44. Console.Write(arr[i]+",");
  45. }
  46. Console.WriteLine();
  47. }
  48. static Int64 journeyToMoon(int n, int[][] astronaut) {
  49. // Complete this function
  50. int totalCombinations=0;
  51. int[] countryArray = new int[n];
  52. int[] sizeArray = new int[n];
  53. for(int i=0;i<n;i++){
  54. countryArray[i] = i;
  55. sizeArray[i] = 1;
  56. }
  57. for(int i=0;i<astronaut.Length;i++){
  58. //if(!find(ref countryArray,astronaut[i][0],astronaut[i][1])){
  59. weightedUnion(ref countryArray,ref sizeArray,astronaut[i][0],astronaut[i][1]);
  60. PrintArray(countryArray,n);
  61.  
  62. }
  63.  
  64. Dictionary<int,List<int>> uniqueCountries = new Dictionary<int,List<int>>();
  65. for(int i=0;i<n;i++){
  66. if(!uniqueCountries.ContainsKey(countryArray[i])){
  67. List<int> tempList = new List<int>();
  68. tempList.Add(i);
  69. uniqueCountries.Add(countryArray[i],tempList);
  70. }else{
  71. List<int> tempList = uniqueCountries[countryArray[i]];
  72. tempList.Add(i);
  73. }
  74. }
  75.  
  76. PrintArray(countryArray,n);
  77. PrintArray(sizeArray,n);
  78. //Console.WriteLine(countriesWithLength.Count);
  79.  
  80. /*
  81. Int64 prevTotal = 0;
  82. foreach(var key in allCountries.Keys){
  83. if(!countries.ContainsKey(allCountries[key])){
  84. countries.Add(allCountries[key],allCountries[key].Count);
  85. if(prevTotal == 0){
  86. prevTotal = allCountries[key].Count;
  87. }else{
  88. totalCombinations+=(allCountries[key].Count*prevTotal);
  89. prevTotal += allCountries[key].Count;
  90. }
  91. }
  92. }
  93. Console.WriteLine("Countries: " + countries.Count);
  94. */
  95. return totalCombinations;
  96. }
  97.  
  98. static void Main(String[] args) {
  99. string[] tokens_n = Console.ReadLine().Split(' ');
  100. int n = Convert.ToInt32(tokens_n[0]);
  101. int p = Convert.ToInt32(tokens_n[1]);
  102. int[][] astronaut = new int[p][];
  103. for(int astronaut_i = 0; astronaut_i < p; astronaut_i++){
  104. string[] astronaut_temp = Console.ReadLine().Split(' ');
  105. astronaut[astronaut_i] = Array.ConvertAll(astronaut_temp,Int32.Parse);
  106. }
  107. Int64 result = journeyToMoon(n, astronaut);
  108. Console.WriteLine(result);
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement