代码随想录 打卡第五十四天

卡码网 107 寻找存在的路线

cpp 复制代码
#include<iostream>
#include<vector>

using namespace std;

int n,m;
int father[101];

int find(int u){
    if(u == father[u]) return u;
    u = father[u];
    u = find(u);
    return u;
}

void join(int x,int y){
    x = find(x);
    y = find(y);
    if(x == y) return;
    father[y] = x;
}

bool isSame(int x,int y){
    x = find(x);
    y = find(y);
    if(x == y) return true;
    return false;
}
int main(){
    cin >> n >> m;
    for(int i = 1; i < 101; i++) {
        father[i] = i;
    }
    
    int x,y;
    while(m--){
        cin >> x >> y;
        join(x,y);
    }
    cin >> x >> y;
    if(isSame(x,y)) cout << 1;
    else cout << 0;
    return 0;
}
相关推荐
papership1 小时前
入门级-数据结构-2、简单树:二叉树的遍历(前序、中序、后序)
数据结构·算法
happymaker06261 小时前
LeetCodeHot100——15.三数之和
数据结构·算法
墨白曦煜1 小时前
算法实战笔记:空间换时间的黑魔法——单调栈全景解析(十一)
java·笔记·算法
大模型最新论文1 小时前
小红书提出 RedKnot:分头处理 kv 缓存,延时降低 60%效果还提升
算法
随意起个昵称1 小时前
线性dp-LIS题目6(友好城市,二分优化)
算法·动态规划
数据科学小丫1 小时前
算法:随机森林算法
算法·随机森林·机器学习
Samson Bruce1 小时前
【初高中数学】
线性代数·数学·算法·机器学习
redaijufeng1 小时前
我在C++中深入理解了继承,收获颇丰
java·c++·算法
.千余1 小时前
【C++】C++继承入门(上):继承语法与基本特性详解
开发语言·c++·笔记·学习·其他