#include <stdio.h>
#define MAXEDGE 100
#define MAXVERTEX 100
typedef struct Edge
{
int begin;
int end;
int wight;
} Edge;
typedef struct Graph
{
char vertex[MAXVERTEX];
Edge edges[MAXEDGE];
int numvertex, numedges;
} MGraph;
void CreateGraph(MGraph *G)
{
printf("请输入顶点和边的个数:\n");
scanf("%d%d", &G->numvertex, &G->numedges);
printf("请输入顶点:\n");
getchar();
for (int i = 0; i < G->numvertex; i++)
{
scanf("%c", &G->vertex[i]);
}
printf("按权值从小到大输入边(vi,vj)对应的起点和终点的下标,begin,end 以及权值 wight:\n");
for (int k = 0; k < G->numedges; k++)
{
Edge e;
scanf("%d%d%d", &e.begin, &e.end, &e.wight);
G->edges[k] = e;
}
}
int Find(int *parent, int f)
{
while (parent[f] > 0)
{
f = parent[f];
}
return f;
}
void Kruskal(MGraph *G)
{
int parent[MAXVERTEX];
for (int i = 0; i < G->numvertex; i++)
{
parent[i] = 0;
}
int m, n;
for (int i = 0; i < G->numedges; i++)
{
n = Find(parent, G->edges[i].begin);
m = Find(parent, G->edges[i].end);
if (n != m)
{
parent[n] = m;
printf("(%d,%d) %d\t", G->edges[i].begin, G->edges[i].end, G->edges[i].wight);
}
}
}
int main()
{
MGraph G;
CreateGraph(&G);
Kruskal(&G);
return 0;
}