参考[1]:https://zhuanlan.zhihu.com/p/93647900/
参考[2]:https://blog.csdn.net/weixin_34037515/article/details/89009891
题目:
顶点集合V={1,2,3,4,5,6},其中有E={ {1,2},{1,3},{2,4},{5,6}}顶点对连通。给出(x,y),x,y∈V,判断x与y是否连通?
思路:
- 初始化 fa={0,1,2,3,4,5,6}
- 对所有的边集,fa[y]=x; 更新fa矩阵为i顶点的前驱连通节点。
- find(x)==find(y) 则连通 ,否则不连通
初始化
int fa[MAXN];
inline void init(int n)
{