Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- class Solution {
- //finding root of an element.
- static int root(ref int[] Arr,int i){
- while(Arr[ i ] != i)
- {
- Arr[ i ] = Arr[ Arr[ i ] ] ;
- i = Arr[ i ];
- }
- return i;
- }
- /*modified union function where we connect the elements by changing the root of one of the element */
- static void weightedUnion(ref int[] Arr,ref int[] size,int A,int B)
- {
- int root_A = root(ref Arr,A);
- int root_B = root(ref Arr,B);
- if(root_A==root_B) return;
- if(size[root_A] < size[root_B ])
- {
- Arr[ root_A ] = Arr[root_B];
- size[root_B] += size[root_A];
- }
- else
- {
- Arr[ root_B ] = Arr[root_A];
- size[root_A] += size[root_B];
- }
- }
- static bool find(ref int[] Arr, int A,int B){
- if( root(ref Arr,A)==root(ref Arr,B) ) //if A and B have same root,means they are connected.
- return true;
- else
- return false;
- }
- static void PrintArray(int[] arr,int n){
- for(int i=0;i<n;i++){
- Console.Write(arr[i]+",");
- }
- Console.WriteLine();
- }
- static Int64 journeyToMoon(int n, int[][] astronaut) {
- // Complete this function
- int totalCombinations=0;
- int[] countryArray = new int[n];
- int[] sizeArray = new int[n];
- for(int i=0;i<n;i++){
- countryArray[i] = i;
- sizeArray[i] = 1;
- }
- for(int i=0;i<astronaut.Length;i++){
- //if(!find(ref countryArray,astronaut[i][0],astronaut[i][1])){
- weightedUnion(ref countryArray,ref sizeArray,astronaut[i][0],astronaut[i][1]);
- PrintArray(countryArray,n);
- }
- Dictionary<int,List<int>> uniqueCountries = new Dictionary<int,List<int>>();
- for(int i=0;i<n;i++){
- if(!uniqueCountries.ContainsKey(countryArray[i])){
- List<int> tempList = new List<int>();
- tempList.Add(i);
- uniqueCountries.Add(countryArray[i],tempList);
- }else{
- List<int> tempList = uniqueCountries[countryArray[i]];
- tempList.Add(i);
- }
- }
- PrintArray(countryArray,n);
- PrintArray(sizeArray,n);
- //Console.WriteLine(countriesWithLength.Count);
- /*
- Int64 prevTotal = 0;
- foreach(var key in allCountries.Keys){
- if(!countries.ContainsKey(allCountries[key])){
- countries.Add(allCountries[key],allCountries[key].Count);
- if(prevTotal == 0){
- prevTotal = allCountries[key].Count;
- }else{
- totalCombinations+=(allCountries[key].Count*prevTotal);
- prevTotal += allCountries[key].Count;
- }
- }
- }
- Console.WriteLine("Countries: " + countries.Count);
- */
- return totalCombinations;
- }
- static void Main(String[] args) {
- string[] tokens_n = Console.ReadLine().Split(' ');
- int n = Convert.ToInt32(tokens_n[0]);
- int p = Convert.ToInt32(tokens_n[1]);
- int[][] astronaut = new int[p][];
- for(int astronaut_i = 0; astronaut_i < p; astronaut_i++){
- string[] astronaut_temp = Console.ReadLine().Split(' ');
- astronaut[astronaut_i] = Array.ConvertAll(astronaut_temp,Int32.Parse);
- }
- Int64 result = journeyToMoon(n, astronaut);
- Console.WriteLine(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement