關於最小生成樹

2022-10-10 18:30:11 字數 2123 閱讀 6320

乙個有n個結點的連通圖的生成樹是原圖的極小連通子圖,且包含原圖中的所有n個結點,並且有保持圖連通的最少的邊。

一般是用並查集的合併與查詢操作來支撐的,這裡簡單提一下

先定義乙個陣列\(fa_i\)表示\(i\)的父節點

inline int find(int x)

//查詢祖先

inline void merge(int x,int y)

}//合併x,y所在的集合

就是將邊權從小到大排序然後依次查詢看是否組成一顆生成樹,若是則停止操作

並查集一定要記得初始化!!!

並查集一定要記得初始化!!!

並查集一定要記得初始化!!!

板子題......

code

#include using namespace std;

const int maxn=2e5+5;

struct node

e[maxn];

int fa[maxn];

int find(int x)

inline void merge(int x,int y)

}inline bool cmp(node x,node y)

e[250000];

int s,p;

inline double dis(double x1,double y1,double x2,double y2)

double x[505],y[505];

int cnt;

bool cmp(edge x,edge y)

for(register int i=1;i<=cnt;i++)

if(ss==p-s)

}return 0;

}

全是裸的生成樹真的好嗎......

code

#include using namespace std;

int n,m,f[5005],cnt,id;

double ans;

long long x[5005],y[5005];

inline int read()

struct node a[1000005];

int cmp( const void *a , const void *b )

int find(int x)

void work(int x,int y)

int main() a[10001];

inline bool cmp(hay a,hay b)

for(i=1;i<=m;i++)

sort(a+1,a+m+1,cmp);

kruskal();

return 0;

}

終於不是裸題了,這道題還是需要一些特殊處理的,首先題目要讀懂,理解對,其次是需要做些特殊處理

code

#include using namespace std;

const int maxn=2e5+5;

struct node

e[maxn];

int fa[maxn];

int find(int x)

inline void merge(int x,int y)

}bool cmp(node x,node y)

e[maxn];

int fa[maxn];

int find(int x)

inline void merge(int x,int y)

}bool cmp(node x,node y)

int n,m,s,t;

int ans=-1;

int main()

for(register int i=1;i<=m;i++)

sort(e+1,e+m+1,cmp);

int k=0;

for(register int i=1;i<=m;i++) }

printf("%d %d",n-1,ans);

return 0;

}

最小生成樹 次小生成樹

一 最小生成樹 說到生成樹首先要解釋一下樹,樹是乙個聯通的無向無環圖,多棵樹的集合則被稱為森林。因此,樹具有許多性質 1.兩點之間的路徑是唯一的。2.邊數等於點數減一。3.連線任意兩點都會生成乙個環。對於乙個無向聯通圖g的子圖,如果它包含g的所有點,則它被稱為g的生成樹,而各邊權和最小的生成樹則被稱...

最小生成樹

package 圖 最小生成樹是用最少的邊吧把所有的節點連線起來。於是和圖的深度優先搜素差不多。class stack public void push int key public int pop 檢視棧頂的元素 public int peek public boolean isempty cla...

最小生成樹

define max vertex num 20 最大頂點數 typedef int adjmatrix max vertex num max vertex num 鄰接矩陣型別 typedef char vertextype typedef struct mgraph struct dnodecl...