-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjourney-to-the-moon.cpp
More file actions
63 lines (59 loc) · 1.67 KB
/
journey-to-the-moon.cpp
File metadata and controls
63 lines (59 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Author: btjanaka (Bryon Tjanaka)
// Problem: (Hackerrank) journey-to-the-moon
// Title: Journey to the Moon
// Link: https://www.hackerrank.com/challenges/journey-to-the-moon/problem
// Idea: The pairs of astronauts form a graph. Find connected components to find
// the number of astronauts from each country.
// Difficulty: medium
// Tags: graph, connected-components, breadth-first-search
#include <bits/stdc++.h>
using namespace std;
int bfs(vector<set<int>>& astro, set<int>& visited, int start) {
int size = 0;
queue<int> to_visit;
to_visit.push(start);
while (!to_visit.empty()) {
int curr = to_visit.front();
visited.insert(curr);
to_visit.pop();
for (int child : astro[curr]) {
if (visited.find(child) == visited.end()) to_visit.push(child);
visited.insert(child); // need to insert no matter what because other
// nodes in queue may have this as a child
}
++size;
}
return size;
}
vector<long long> calc_comp(vector<set<int>>& astro) {
set<int> visited;
vector<long long> sizes;
for (int i = 0; i < astro.size(); ++i) {
if (visited.find(i) == visited.end()) {
sizes.push_back(bfs(astro, visited, i));
}
}
return sizes;
}
int main() {
int n, p;
scanf("%d %d", &n, &p);
vector<set<int>> astro(n);
int a1, a2;
for (int i = 0; i < p; ++i) {
scanf("%d %d", &a1, &a2);
astro[a1].insert(a2);
astro[a2].insert(a1);
}
vector<long long> comp = calc_comp(astro);
long long pairs = 0;
long long tot = 0;
tot = comp[0];
pairs = 0;
for (int i = 1; i < comp.size(); ++i) {
pairs += comp[i] * tot;
tot += comp[i];
}
printf("%lld\n", pairs);
return 0;
}