zoukankan      html  css  js  c++  java
  • LintCode "Find the Weak Connected Component in the Directed Graph"

    New stuff learnt - Union-Find. Simpler and more elegant than I thought.

    class Solution {
        
        unordered_map<int, int> father;
        int find(int val)
        {
        if(!father.count(val))
        {
            father[val] = val;
            return val;
        }
        while(val != father[val]) val = father[val];
        return val;
        }
        void union_(int x, int y) 
        {
            int x_root = find(x), y_root = find(y);
            father[min(x_root, y_root)] = max(x_root, y_root);
        }
    public:
        /**
         * @param nodes a array of directed graph node
         * @return a connected set of a directed graph
         */
        vector<vector<int>> connectedSet2(vector<DirectedGraphNode*>& nodes) 
        {
        // Sort first
        sort(nodes.begin(), nodes.end(),
                 [](const DirectedGraphNode* a, const DirectedGraphNode* b) {
                    return a->label < b->label;
                 });
        
        // Union and Find
        for (auto &n : nodes) 
        {
            for(auto &&nn : n->neighbors)
            {
            int fn = find(n->label);
            int fnn= find(nn->label);
            if(fn != fnn)
                union_(fn, fnn);
            }
        }
        
        // Grouping
        unordered_map<int, vector<int>> group;
            for (const auto& node : nodes) {
                group[find(node->label)].emplace_back(node->label);
            }
            
            // Output
        vector<vector<int>> ret;
        for (auto& kvp : group) {
                ret.emplace_back(move(kvp.second));
            }
            return ret;
        }
    };
  • 相关阅读:
    (Problem 4)Largest palindrome product
    (Problem 3)Largest prime factor
    (Problem 2)Even Fibonacci numbers
    (Problem 1)Multiples of 3 and 5
    Ural 1086
    Ural 1319
    Ural 1149
    Ural 1079
    Ural 1068
    2016/04/06
  • 原文地址:https://www.cnblogs.com/tonix/p/4897482.html
Copyright © 2011-2022 走看看