跳到正文

个人记录

三种判联通的写法

qyh_qianyuheng2012约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v2当前
  2. v1
C++
void dfs(int x){//x表示我当前的位置
	vis[x]=1;                      //dfs
	for(auto t:G[x])
		if(!vis[t])
			dfs(t);
}

void bfs(int x){//bfs遍历板子
	vis[x]=1;
	q.push(x);
	while(q.size()){
		int y=q.front();
		q.pop();
		for(auto y:G[x]){
			if(!vis[y]){
				vis[y]=1;
				q.push(y);
			}
		}
	}
}
int find(int x){//并查集
	if(x==f[x]) return x;
	else return f[x]=find(f[x]);
}
原文