并查集
1.合并兩個集合
2.查詢兩個數是否在一個集合
基本原理:
每個集合用一棵樹來表示,樹根的編號就是整個集合的編號,每個結點存盤他的父節點,p[x]表示x的父節點
1.是否是一個集合 if( find(a)==find(b) )
2.合并兩個集合 p[find(a)]=find(b)
static int p[]=new int[N]; static int find(int x){ //回傳x的祖宗結點+路徑壓縮 if(p[x]!=x) p[x]=find(p[x]); return p[x]; }

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/102330.html
標籤:其他
上一篇:關于matlab的問題
下一篇:836. 合并集合(并查集)
