MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / main

Function main

Hackerrank_problems/Journey to the moon/solution.cpp:30–61  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28 return cc;
29}
30int main(){
31 fastio // for faster input/output
32
33 ll n,e,u,v;
34 cin>>n>>e;
35
36 // initialize total number of pairs possible as nC2
37 ll ans=n*(n-1)/2;
38 vector<ll>* graph = new vector<ll>[n]();
39
40 // Represent graph using an adjacency list
41 for(ll i=0;i<e;i++)
42 {
43 cin>>u>>v;
44 graph[u].push_back(v);
45 graph[v].push_back(u);
46 }
47 // Visited array to mark nodes that are visited
48 bool* visited = new bool[n]();
49
50 for(ll i=0;i<n;i++){
51 // for each non visited node call dfs to find the number of nodes in the component
52 if(!visited[i]){
53 ll x = DFS(i,visited,graph);
54 // remove the number of pairs formed by considering astronauts from the current component
55 ans=ans-(x*(x-1)/2);
56 }
57 }
58 // finally print answer
59 cout<<ans<<endl;
60 return 0;
61}

Callers

nothing calls this directly

Calls 1

DFSFunction · 0.85

Tested by

no test coverage detected