标签:red public rip == nec order desc http union
https://leetcode.com/problems/redundant-connection/description/
Use map to do Union Find.
class Solution { public: vector<int> findRedundantConnection(vector<vector<int>>& edges) { unordered_map<int,int> parent; for (const auto& e : edges) { int p = findParent(parent, e[0]); int c = findParent(parent, e[1]); if (p == c) return e; parent[c] = p; } return vector<int>(); } int findParent(unordered_map<int,int>& parents, int child) { while (parents.count(child)) child = parents[child]; return child; } };
标签:red public rip == nec order desc http union
原文地址:https://www.cnblogs.com/JTechRoad/p/8990947.html