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;
    }
};
原文地址:https://www.cnblogs.com/tonix/p/4897482.html