• LeetCode-684. Redundant Connection [C++][Java]


    LeetCode-684. Redundant ConnectionLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/redundant-connection/

    In this problem, a tree is an undirected graph that is connected and has no cycles.

    You are given a graph that started as a tree with n nodes labeled from 1 to n, with one additional edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed. The graph is represented as an array edges of length n where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.

    Return an edge that can be removed so that the resulting graph is a tree of n nodes. If there are multiple answers, return the answer that occurs last in the input.

    Example 1:

    Input: edges = [[1,2],[1,3],[2,3]]
    Output: [2,3]
    

    Example 2:

    Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
    Output: [1,4]
    

    Constraints:

    • n == edges.length
    • 3 <= n <= 1000
    • edges[i].length == 2
    • 1 <= ai < bi <= edges.length
    • ai != bi
    • There are no repeated edges.
    • The given graph is connected.

    【C++】

    1. 并查集

    1. class UF {
    2. vector<int> id, size;
    3. public:
    4. UF(int n): id(n), size(n, 1) {
    5. iota(id.begin(), id.end(), 0); // iota函数可以把数组初始化为0到n-1
    6. }
    7. int find(int p) {
    8. while (p != id[p]) {
    9. id[p] = id[id[p]]; // 路径压缩,使得下次查找更快
    10. p = id[p];
    11. }
    12. return p;
    13. }
    14. void connect(int p, int q) {
    15. int i = find(p), j = find(q);
    16. if (i != j) {
    17. // 按秩合并:每次合并都把深度较小的集合合并在深度较大的集合下面
    18. if (size[i] < size[j]) {
    19. id[i] = j;
    20. size[j] += size[i];
    21. } else {
    22. id[j] = i;
    23. size[i] += size[j];
    24. }
    25. }
    26. }
    27. bool isConnected(int p, int q) {
    28. return find(p) == find(q);
    29. }
    30. };
    31. class Solution {
    32. public:
    33. vector<int> findRedundantConnection(vectorint>>& edges) {
    34. int n = edges.size();
    35. UF uf(n + 1);
    36. for (auto e: edges) {
    37. int u = e[0], v = e[1];
    38. if (uf.isConnected(u, v)) {return e;}
    39. uf.connect(u, v);
    40. }
    41. return vector<int>{-1, -1};
    42. }
    43. };

    2. 粗暴循环打法

    1. class Solution {
    2. public:
    3. vector<int> findRedundantConnection(vectorint>>& edges) {
    4. int n = 0;
    5. for(const auto &e : edges) {
    6. n = max(e[0]+1, n);
    7. n = max(e[1]+1, n);
    8. }
    9. parent_ = vector<int>(n);
    10. for(int i = 0; i < n; ++i) parent_[i] = i;
    11. for(auto it = edges.begin(); it != edges.end(); ++it) {
    12. const auto &e = *it;
    13. if(GetParent(e[0]) == GetParent(e[1])) {
    14. if(e[0] < e[1]) return {e[0], e[1]};
    15. else return {e[1], e[0]};
    16. }
    17. parent_[GetParent(e[0])] = GetParent(e[1]);
    18. }
    19. return {};
    20. }
    21. private:
    22. int GetParent(int x) {
    23. if(parent_[x] != x) parent_[x] = GetParent(parent_[x]);
    24. return parent_[x];
    25. }
    26. vector<int> parent_;
    27. };

    【Java】

    1. class Solution {
    2. private int[] parent_;
    3. public int[] findRedundantConnection(int[][] edges) {
    4. int n = 0;
    5. for(int[] e : edges) {
    6. n = Math.max(e[0]+1, n);
    7. n = Math.max(e[1]+1, n);
    8. }
    9. parent_ = new int[n];
    10. for(int i = 0; i < n; ++i) parent_[i] = i;
    11. for(int[] e : edges) {
    12. if (GetParent(e[0]) == GetParent(e[1])) {
    13. if(e[0] < e[1]) return new int[]{e[0], e[1]};
    14. else return new int[]{e[1], e[0]};
    15. }
    16. parent_[GetParent(e[0])] = GetParent(e[1]);
    17. }
    18. return new int[]{};
    19. }
    20. private int GetParent(int x) {
    21. if(parent_[x] != x) parent_[x] = GetParent(parent_[x]);
    22. return parent_[x];
    23. }
    24. }

  • 相关阅读:
    Go sync.Map探究
    【云原生 | Kubernetes 系列】---altermanager消息配置和pushgateway
    Vue3 - 组件通信(子传父)
    mybatis
    C语言qsort()函数详细解析
    css---定位
    信创产业多点开花,AntDB数据库积极参与行业标准研制,协同价值链伙伴共促新发展
    LVGL---走进LVGL
    【实操干货】做好这 16 项优化,你的 Linux 操作系统焕然一新
    【EI会议】第三届大数据、人工智能与风险管理国际学术会议 (ICBAR 2023)
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126456032