图论第7天

今天去打了会羽毛球。最近还是有点累啊,今天尽量效率

1971. 寻找图中是否存在路径

第一步是先整init

第二步先把该关联的关联

第三步判断是否有路

cpp 复制代码
class Solution {
private:
    int nMax = 200005;
    vector<int>father = vector<int>(nMax,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;
    }
public:
    bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
        init(n);
        for(int i = 0;i < edges.size();i++){
            join(edges[i][0],edges[i][1]);
        }
        return isSame(source,destination);
    }
};

684.冗余连接

其实做的事就是不断的搭桥,直到哪个用不上,就return。

cpp 复制代码
class Solution {
private:
    int nMax = 1005;
    vector<int>father = vector<int>(nMax,0);
    void init(){
        for(int i = 0;i < nMax;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;
    }

public:
    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        init();
        for(int i = 0; i < edges.size();i++){
            if(isSame(edges[i][0],edges[i][1]))return edges[i];
            else join(edges[i][0],edges[i][1]);
        }
        return {};
    }
};

685.冗余连接II

有点难的那种,今天有点晚啦,先洗澡去啦,后面补上。

相关推荐
kill bert3 小时前
Java八股文背诵 第四天JVM
java·开发语言·jvm
√尖尖角↑3 小时前
力扣——【1991. 找到数组的中间位置】
算法·蓝桥杯
Allen Wurlitzer3 小时前
算法刷题记录——LeetCode篇(1.8) [第71~80题](持续更新)
算法·leetcode·职场和发展
百锦再5 小时前
五种常用的web加密算法
前端·算法·前端框架·web·加密·机密
你是理想6 小时前
wait 和notify ,notifyAll,sleep
java·开发语言·jvm
碳基学AI6 小时前
北京大学DeepSeek内部研讨系列:AI在新媒体运营中的应用与挑战|122页PPT下载方法
大数据·人工智能·python·算法·ai·新媒体运营·产品运营
helloworld工程师6 小时前
【微服务】SpringBoot整合LangChain4j 操作AI大模型实战详解
java·eclipse·tomcat·maven
Java&Develop6 小时前
idea里面不能运行 node 命令 cmd 里面可以运行咋回事啊
java·ide·intellij-idea
q567315236 小时前
使用Java的HttpClient实现文件下载器
java·开发语言·爬虫·scrapy
独家回忆3647 小时前
每日算法-250410
算法