Sort by

recency

|

515 Discussions

|

  • + 0 comments

    Test case 11 does not pass. All others pass.

    Solution using DFS.

    public static int journeyToMoon(int n, List<List<Integer>> astronaut) {
            // Write your code here
            Map<Integer, List<Integer>> adj = buildAdjList(n, astronaut);
    
            Set<Integer> visited = new HashSet<>();
            Set<Set<Integer>> countries = new HashSet<>();
            for (int i=0;i<n;i++) {
                if (!visited.contains(i)) {
                    Set<Integer> astronautInCountry = new HashSet<>();
                    
                    dfsVisit(adj, i, visited, astronautInCountry);
                    
                    countries.add(astronautInCountry);
                }
            }
            long result = 0;
            for (Set<Integer> country : countries) {
    				// If choose 1st atronaut from country, there are (n - country.size()) astronaut to choose 2nd.
                result += (country.size() * (n - country.size()));
            }
            return (int)(result / 2);
        }
        
        private static void dfsVisit(final Map<Integer, List<Integer>> adj, int node, Set<Integer> visited, Set<Integer> astronautInCountry) {
            visited.add(node);
            astronautInCountry.add(node);
            
            for (int neighbor : adj.get(node)) {
                if (!visited.contains(neighbor)) {
                    dfsVisit(adj, neighbor, visited, astronautInCountry);
                }
            }
        }
        
        private static Map<Integer, List<Integer>> buildAdjList(int n, List<List<Integer>> astronaut) {
            Map<Integer, List<Integer>> adj = new HashMap<>();
            
            for (int i=0; i<n; i++) {
                adj.put(i, new ArrayList<>());
            }
            for (List<Integer> edge : astronaut ) {
                adj.get(edge.get(0)).add(edge.get(1));
                adj.get(edge.get(1)).add(edge.get(0));
            }
            
            return adj;
        }
    
  • + 0 comments

    You can always use a long long type in C++.

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    
    static inline int Count(const std::vector<int> &groups, int head)
        {
        return -groups[head];
        }
    
    static int Find(std::vector<int> &groups, int node)
        {
        if (groups[node] < 0)
            return node;
        return (groups[node] = Find(groups, groups[node]));
        }
    
    static void Union(std::vector<int> &groups, int a, int b)
        {
        int parentA = Find(groups, a);
        int parentB = Find(groups, b);
    
            if (parentA == parentB)
                return;
    
        int countA   = Count(groups, parentA);
        int countB   = Count(groups, parentB);
        int newCount = countA + countB;
    
            if (countA > countB) {
                groups[parentB] = parentA;
                groups[parentA] = -newCount;
            } else {
                groups[parentA] = parentB;
                groups[parentB] = -newCount;
            }
        }
    
    int main(void)
        {
            int n, p;
    
            std::cin >> n >> p;
            std::vector<int> groups(n, -1);
    
            for (int i = 0; i < p; i++) {
                int a, b;
                std::cin >> a >> b;
                Union(groups, a, b);
            }
    
            long long total     = 0;
            long long prevCount = 0;
            for (int i = 0; i < n; i++) {
                if (Find(groups, i) == i) {
                    int count = Count(groups, i);
    
                    total     += count * prevCount;
                    prevCount += count;
                }
            }
    
            std::cout << total << "\n";
        }
    
  • + 0 comments

    Has anyone successfully passed test case 11

    100000 2
    1 2
    3 4

    expected ans : 4999949998

    the expected output is 4,999,949,998, which exceeds the 32-bit integer limit (2,147,483,647). This seems to cause overflow in languages where int is 32-bit by default.

  • + 0 comments

    Test case 11 doesn't work with the function signature in rust. The answer is > i32::MAX.

  • + 0 comments

    For python code, test case 11, if you use summation over and over for calculating the final results, you could use cummulative sum to avoid it. it will help you to speed up.