算法训练营|图论第5天

题目:107.寻找存在的路径

题目链接:

107. 寻找存在的路径 (kamacoder.com)

代码:

cpp 复制代码
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
vector<int>father;
int find(int u) {
	if (u == father[u]) return u;
	else return father[u] = find(father[u]);
}
void join(int u, int v) {
	u = find(u);
	v = find(v);
	if (u == v) return;
	father[v] = u;
}
bool isSame(int u, int v) {
	u = find(u);
	v = find(v);
	return u == v;
}
int main() {
	int n, m;
	cin >> n >> m;	
	father = vector<int>(n + 1);
	for (int i = 0; i <= n; i++) {
		father[i] = i;
	}
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		join(s, t);
	}
	int source, destination;
	cin >> source >> destination;
	if (isSame(source, destination)) {
		cout << 1 << endl;
	}
	else {
		cout << 0 << endl;
	}
}
相关推荐
czy87874752 分钟前
深入了解 C++ 中的 Lambda 表达式(匿名函数)
c++
52Hz1188 分钟前
力扣230.二叉搜索树中第k小的元素、199.二叉树的右视图、114.二叉树展开为链表
python·算法·leetcode
苦藤新鸡11 分钟前
56.组合总数
数据结构·算法·leetcode
LiLiYuan.24 分钟前
【Cursor 中找不到LeetCode 插件解决办法】
算法·leetcode·职场和发展
Charlie_lll25 分钟前
力扣解题-[3379]转换数组
数据结构·后端·算法·leetcode
CSDN_RTKLIB32 分钟前
include_directories和target_include_directories说明
c++
captain37635 分钟前
Java队列(Queue)
算法·链表
TracyCoder12340 分钟前
LeetCode Hot100(23/100)——142. 环形链表 II
算法·leetcode·链表
jigsaw_zyx40 分钟前
提示词工程
人工智能·算法