并查集是一种树型的数据结构,用于处理一些不交集的合并及查询问题。
给出一个有向图,经过并查集算法可以很快地判断任意连个点是否属于同一个集合。
#include<iostream>
#include<stdio.h>
#include<string.h>
#define MAXN 1000
using namespace std;
int root[MAXN]; //the root of a node
int layer[MAXN];
int n, m; //the number of node and edge
void init(){
for (int i =