图论第8天

685.冗余连接II

这题需要考虑两种情况:

1.两个输入

2.没有两个输入就是有成环

cpp 复制代码
class Solution
{
public:
    static const int N = 1005;
    int father[N];
    int n;
    void init()
    {
        for (int i = 0; i <= n; i++)
        {
            father[i] = i;
        }
    }
    int find(int x)
    {
        return x == father[x] ? x : father[x] = find(father[x]);
    }
    int isSame(int a, int b)
    {
        a = find(a);
        b = find(b);
        return (a == b);
    }
    void join(int a, int b)
    {
        a = find(a);
        b = find(b);
        if (a == b)
            return;
        father[b] = a;
    }
    bool lianggeshuru(vector<vector<int>> &edges, int deleteNode)
    {
        init();
        for (int i = 0; i < edges.size(); i++)
        {
            if (i == deleteNode)
                continue;
            if (isSame(edges[i][0], edges[i][1]))
            {
                return false;
            }
            join(edges[i][0], edges[i][1]);
        }
        return true;
    }
    vector<int> huan(vector<vector<int>> &edges)
    {
        init();
        for (int i = 0; i < n; i++)
        {
            if (isSame(edges[i][0], edges[i][1]))
                return edges[i];
            join(edges[i][0], edges[i][1]);
        }
        return {};
    }
    vector<int> findRedundantDirectedConnection(vector<vector<int>> &edges)
    {
        n = edges.size();
        int count[N] = {0};
        // 有两个输入
        for (int i = 0; i < n; i++)
        {
            count[edges[i][1]]++;
        }
        vector<int> vec;
        for (int i = n - 1; i >= 0; i--)
        {
            if (count[edges[i][1]] == 2)
            {
                vec.push_back(i);
                cout << i << endl;
            }
        }
        if (vec.size() > 0)
        {
            if (lianggeshuru(edges, vec[0]))
            {
                return edges[vec[0]];
            }
            else
            {
                return edges[vec[1]];
            }
        }
        // 有环
        return huan(edges);
    }
};

默写一遍再。其实突然就对这行代码不理解了,需要做个小实验。其实就是后面可以跟一个等式。

cpp 复制代码
    int find(int x)
    {
        return x == father[x] ? x : father[x] = find(father[x]);
    }

挺好挺好,默写出来啦!!!思路很重要!!!

cpp 复制代码
class Solution {
public:
    static const int N = 1005;
    int father[N] = {0};
    void init(int num){
        for(int i = 0;i < num;i++){
            father[i] = i;
        }
    }
    int find(int x){
        return x == father[x] ? x : father[x] = find(father[x]);
    }
    bool isSame(int a,int b){
        a = find(a);
        b = find(b);
        return (a == b);
    }
    void join(int a ,int b){
        a = find(a);
        b = find(b);
        if(a == b)return;
        father[b] = a;
    }
    bool liashuru(vector<vector<int>>& edges,int deleteNode){
        init(edges.size());// because 1 <= ui, vi <= n
        for(int i = 0;i< edges.size();i++){
            if(i == deleteNode)continue;
            if(isSame(edges[i][0],edges[i][1])){
                return false;
            }
            join(edges[i][0],edges[i][1]);
        }
        return true;
    }
    vector<int> huan(vector<vector<int>>& edges){
        init(edges.size());// because 1 <= ui, vi <= n
        for(int i = 0;i < edges.size();i++){
            if(isSame(edges[i][0],edges[i][1])){
                return edges[i];
            }
            join(edges[i][0],edges[i][1]);
        }
        return {};
    }
    vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
        int n = edges.size();
        int count[N] = {0};
        for(int i = 0;i < n;i++){
            count[edges[i][1]]++;
        }
        vector<int>vec;
        for(int i = n-1;i>=0;i--){
            if(count[edges[i][1]] == 2){
                vec.push_back(i);
            }
        }
        //俩输入
        if(vec.size() > 0){
            if(liashuru(edges,vec[0])){
                return edges[vec[0]];
            }else{
                return edges[vec[1]];
            }
        }
        //有环
        return huan(edges);
    }
};

那明天开始做面试题。

相关推荐
github_czy33 分钟前
RRF (Reciprocal Rank Fusion) 排序算法详解
算法·排序算法
许愿与你永世安宁1 小时前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子1 小时前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
满分观察网友z2 小时前
从一次手滑,我洞悉了用户输入的所有可能性(3330. 找到初始输入字符串 I)
算法
YuTaoShao2 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
Heartoxx2 小时前
c语言-指针(数组)练习2
c语言·数据结构·算法
大熊背2 小时前
图像处理专业书籍以及网络资源总结
人工智能·算法·microsoft
满分观察网友z2 小时前
别怕树!一层一层剥开它的心:用BFS/DFS优雅计算层平均值(637. 二叉树的层平均值)
算法
杰克尼3 小时前
1. 两数之和 (leetcode)
数据结构·算法·leetcode
YuTaoShao4 小时前
【LeetCode 热题 100】56. 合并区间——排序+遍历
java·算法·leetcode·职场和发展