The member states of the UN are planning to send people to the moon. They want them to be from different countries. You will be given a list of pairs of astronaut ID’s. Each pair is made of astronauts from the same country. Determine how many pairs of astronauts from different countries they can choose from.
Example
There are astronauts numbered through . Astronauts grouped by country are and . There are pairs to choose from: and .
Function Description
Complete the journeyToMoon function in the editor below.
journeyToMoon has the following parameter(s):
- int n: the number of astronauts
- int astronaut[p][2]: each element is a element array that represents the ID’s of two astronauts from the same country
Returns
– int: the number of valid pairs
Input Format
The first line contains two integers and , the number of astronauts and the number of pairs.
Each of the next lines contains space-separated integers denoting astronaut ID’s of two who share the same nationality.
Sample Input 0
5 3
0 1
2 3
0 4
Sample Output 0
6
Explanation 0
Persons numbered belong to one country, and those numbered belong to another. The UN has ways of choosing a pair:
Sample Input 1
4 1
0 2
Sample Output 1
5
Journey to the Moon HackerRank Solution in C
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int FindP(int *P, int x) { if(P[x] != x) { P[x] = FindP(P, P[x]); } return P[x]; } int MergeS(int *P, int *R, int *C, int A, int B) { if(R[A] < R[B]) { P[A] = B; C[B] += C[A]; } else if(R[A] > R[B]) { P[B] = A; C[A] += C[B]; } else { P[B] = A; R[A] += 1; C[A] += C[B]; } } int main() { int R[100000], P[100000], C[100000], i, N, I, A, B, UC; int M[100000], S[100000]; unsigned long long result; scanf("%d%d", &N, &I); for(i=0;i<N;i++) { P[i] = i; C[i] = 1; R[i] = 0; } for(i=0;i<I;i++) { scanf("%d%d", &A, &B); A = FindP(P, A); B = FindP(P, B); if(A != B) MergeS(P, R, C, A, B); } UC = 0; for(i=0;i<N;i++) { if(P[i] == i) { M[UC++] = C[i]; } } S[0] = M[0]; for(i=1;i<UC;i++) { S[i] = S[i-1] + M[i]; } result = 0; for(i=0;i<UC-1;i++) { A = M[i]; B = S[UC-1] - S[i]; result += A*B; } printf("%llu", result); return 0; }
Journey to the Moon HackerRank Solution in CPP
#include <string> #include <vector> #include <map> #include <list> #include <iterator> #include <set> #include <queue> #include <iostream> #include <sstream> #include <stack> #include <deque> #include <cmath> #include <memory.h> #include <cstdlib> #include <cstdio> #include <cctype> #include <algorithm> #include <utility> using namespace std; #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i) #define REP(i, N) FOR(i, 0, N) #define RREP(i, N) RFOR(i, N, 0) #define ALL(V) V.begin(), V.end() #define SZ(V) (int)V.size() #define PB push_back #define MP make_pair #define Pi 3.14159265358979 typedef long long Int; typedef unsigned long long UInt; typedef vector <int> VI; typedef pair <int, int> PII; VI a[1<<17]; bool was[1<<17]; int n,m; int dfs(int cur) { if (was[cur]) return 0; was[cur] = true; int res = 1; REP(i,SZ(a[cur])) { int nx = a[cur][i]; res += dfs(nx); } return res; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); scanf("%d%d",&n,&m); REP(i,m) { int x,y; scanf("%d%d",&x,&y); a[x].push_back(y); a[y].push_back(x); } VI all; REP(i,n) { if (!was[i]) { all.push_back(dfs(i)); } } Int res = 0; Int sum = 0; REP(i, SZ(all)) { res += sum * all[i]; sum += all[i]; } cout << res << endl; return 0; }
Journey to the Moon HackerRank Solution in Python
class Node: def __init__(self, data): self.data = data self.parent = None self.rank = 0 self.descendents = 1 def union(self, other): r1 = self.find() r2 = other.find() if r1 == r2: return if r1.rank < r2.rank: r1.parent = r2 r2.descendents += r1.descendents elif r1.rank > r2.rank: r2.parent = r1 r1.descendents += r2.descendents else: r2.parent = r1 r1.descendents += r2.descendents r1.rank += 1 def find(self): return self if self.parent is None else self.parent.find() def get_ans(astros, pairs): nodes = [Node(astro) for astro in range(astros)] for pair in pairs: nodes[pair[0]].union(nodes[pair[1]]) sizes = [root.descendents for root in nodes if root.find() == root] sq = lambda x : x * x return (sq(sum(sizes)) - sum(map(sq, sizes))) / 2 if __name__ == '__main__': astros, lines = map(int, raw_input().split()) pairs = [] for i in range(lines): pair = tuple(map(int, raw_input().split())) pairs.append(pair) print get_ans(astros, pairs)
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.