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};
            }
        }
    }
};
相关推荐
夏鹏今天学习了吗18 分钟前
【LeetCode热题100(73/100)】买卖股票的最佳时机
算法·leetcode·职场和发展
gaosushexiangji27 分钟前
一项基于粒子图像测速(PIV)速度场反演的压力场重构技术
人工智能·算法
Voyager_428 分钟前
算法学习记录17——力扣“股票系列题型”
学习·算法·leetcode
雨大王51228 分钟前
汽车涂装工艺的智能化与绿色化升级:技术、案例与趋势
算法
XFF不秃头36 分钟前
【力扣刷题笔记-在排序数组中查找元素的第一个和最后一个位置】
c++·笔记·算法·leetcode
yoyo君~42 分钟前
FAST-LIVO2 深度技术解析
算法·计算机视觉·机器人·无人机
我也要当昏君1 小时前
时间复杂度
算法·数学建模
业精于勤的牙1 小时前
浅谈:算法中的斐波那契数(六)
人工智能·算法
小孟的CDN1 小时前
使用pytorch进行batch_size分批训练,并使用adam+lbfgs算法——波士顿房价预测
pytorch·算法·batch·代码·adam+lbfgs
仰泳的熊猫1 小时前
1037 Magic Coupon
数据结构·c++·算法·pat考试