并查集 2019-05-01 图论 UFIND()MERGE(x,y)FFIND(n) UFIND()12345678初始化dir数组使值和自身下标相等初始化结构体数组G存放连通边for i=0 to g.len-1 MERGE(g.x,g,y)for i=0 to 顶点数 if(dir[i]==i) count=count+1return count MERGE(x,y)1234fx=FFIND(x)fy=FFIND(y)if fx!=fy dir[fy]=fx FFIND(n)12345678i=nwhile dir[i]!=i i=dir[i]while dir[n]!=i t=dir[n] dir[n]=i n=treturn n