衝突的條件是:用乙個dist陣列來儲存節點到根的距離,這個距離在路徑壓縮的時候更新一下,dist[x]+=dist[parent[x]],然後 在合併時,令r1=find(u),r2=find(v),於是合併時就有parent[r2]=r1,dist[r2]=dist[u]+w-dist[v]。
#include#include#include#include#include#include#include#include#includeusing namespace std;
typedef long long ll;
const int maxn = 50005;
int n,m;
int par[maxn];
int dis[maxn];
void init()
int find(int x)
if(isroot) return x;
else
}bool unite(int x,int y,int k){
int fax = find(x);
//cout<
hdu3047之並查集
include include include include include include include include include define inf 99999999 using namespace std const int max 50000 10 const int mod 3...
並查集,帶權並查集
題意 ignatius過生日,客人來到,他想知道他需要準備多少張桌子。然而一張桌子上面只能坐上相互熟悉的人,其中熟悉可定義成為a與b認識,b與c認識,我們就說a,b,c相互熟悉 例如a與b熟悉and b與c熟悉,d與e熟悉,此時至少需要兩張桌子。輸入 t表示樣例個數,n表示朋友個數,朋友從1到n編號...
帶權並查集
食物鏈 time limit 1000ms memory limit 10000k total submissions 71395 accepted 21146 description 動物王國中有三類動物a,b,c,這三類動物的食物鏈構成了有趣的環形。a吃b,b吃c,c吃a。現有n個動物,以1 n...