Sort by

recency

|

517 Discussions

|

  • + 0 comments

    Javascript

    let cnt = 0;
    function Dfs(visited, tree, i) {
        if (visited[i]) {
            return;
        }
        
        visited[i] = true;
        ++cnt
        if (tree[i]) {
            for (let v of tree[i]) {
                Dfs(visited, tree, v);
            }
        }
    }
    
    /*
     * Complete the 'journeyToMoon' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER n
     *  2. 2D_INTEGER_ARRAY astronaut
     */
    
    function journeyToMoon(n, astronaut) {
        // Write your code here
        let tree = {};
        for (const [a1, a2] of astronaut) {
            tree[a1] = tree[a1] ?? new Set();
            tree[a2] = tree[a2] ?? new Set();
            tree[a1].add(a2);
            tree[a2].add(a1);
        }
        let a = [];
        let ans = 0;
    
        for (let i=0;i<n;++i) {
            if (!visited[i]) {
                Dfs(visited, tree, i);
                a.push(cnt);
                cnt = 0;
            }
        }
        
        for (let i=0;i<a.length;++i) {
            for (let j=i+1;j<a.length;++j) {
                ans += a[i] * a[j];
            }
        }
        return ans;
    }
    
  • + 0 comments

    Using union_find is more efficient than using graph.

  • + 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.