leetcode:冗余连接 II[并查集检查环][节点入度]

学习要点

  1. 并查集检查环
  2. 分类讨论

题目链接

685. 冗余连接 II - 力扣(LeetCode)

题目描述

解法:并查集检查环

cpp 复制代码
class Solution {
public:
    int Find_Root(vector<int>& v_root, int pos) {
        while (v_root[pos] > 0) {
            pos = v_root[pos];
        }
        return pos;
    }
    bool Is_Tree(vector<vector<int>>& edges, pair<int, int> pair1_edg_rudu2) {
        vector<int> v_root( edges.size() + 1, -1);
        for (int i = 0; i < edges.size(); i++) {
            if (edges[i][0] == pair1_edg_rudu2.first &&
                edges[i][1] == pair1_edg_rudu2.second) {
                continue;
            }
            int root1 = Find_Root(v_root, edges[i][0]);
            int root2 = Find_Root(v_root, edges[i][1]);
            if (root1 != root2) {
                v_root[edges[i][1]] = root1;
            } else {
                return false;
            }
        }
        return true;
    }

    vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
        // 标记入度为2的点
        int n = edges.size();
        vector<int> v_find_rudu(n + 1, 0);
        for (int i = 0; i < edges.size(); i++) {
            v_find_rudu[edges[i][1]]++;
        }
        // 寻找入度为2的点
        int point_rudu2 = -1;
        for (int i = 1; i < n + 1; i++) {
            if (v_find_rudu[i] == 2) {
                point_rudu2 = i;
            }
        }
        // 如果不存在入度为2的点:并查集解法
        vector<int> v_root( edges.size() + 1, -1);
        if (point_rudu2 == -1) {
            for (int i = 0; i < edges.size(); i++) {
                int root1 = Find_Root(v_root, edges[i][0]);
                int root2 = Find_Root(v_root, edges[i][1]);
                if (root1 != root2) {
                    v_root[edges[i][1]] = root1;
                } else {
                    return vector<int>{edges[i][0], edges[i][1]};
                }
            }
            return vector<int>();
        }
        // 如果存在入度为2的点
        else {
            // 倒序寻找这个边
            pair<int, int> pair1_edg_rudu2;
            pair<int, int> pair2_edg_rudu2;
            bool flag1 = true;
            for (int i = n - 1; i >= 0; i--) {
                if (flag1 && edges[i][1] == point_rudu2) {
                    pair1_edg_rudu2.first = edges[i][0];
                    pair1_edg_rudu2.second = edges[i][1];
                    flag1 = false;
                    continue;
                }
                if (flag1 == false && edges[i][1] == point_rudu2) {
                    pair2_edg_rudu2.first = edges[i][0];
                    pair2_edg_rudu2.second = edges[i][1];
                    break;
                }
            }
            bool is_pair1 = Is_Tree(edges, pair1_edg_rudu2);
            if (is_pair1) {
                return vector<int>{pair1_edg_rudu2.first,
                                   pair1_edg_rudu2.second};
            } else {
                return vector<int>{pair2_edg_rudu2.first,
                                   pair2_edg_rudu2.second};
            }
        }
    }
};
相关推荐
courniche25 分钟前
ECDH、ECDHE、ECDLP、ECDSA傻傻分不清?
算法·密码学
前端小刘哥36 分钟前
超低延迟与高并发:视频直播点播平台EasyDSS在游戏直播场景的技术实践
算法
毅炼44 分钟前
常见排序算法
java·算法·排序算法
猫梦www1 小时前
力扣21:合并两个有序链表
数据结构·算法·leetcode·链表·golang·力扣
Han.miracle1 小时前
数据结构——排序的学习(一)
java·数据结构·学习·算法·排序算法
爱coding的橙子1 小时前
每日算法刷题Day76:10.19:leetcode 二叉树12道题,用时3h
算法·leetcode·职场和发展
liu****2 小时前
20.哈希
开发语言·数据结构·c++·算法·哈希算法
夏鹏今天学习了吗3 小时前
【LeetCode热题100(47/100)】路径总和 III
算法·leetcode·职场和发展
smj2302_796826523 小时前
解决leetcode第3721题最长平衡子数组II
python·算法·leetcode
m0_626535203 小时前
力扣题目练习 换水问题
python·算法·leetcode