Title:
Give you a weighted phase-free graph to determine whether the minimum spanning tree is unique.
Explanation:
I have another way of writing prim to solve this problem. If you want to see more details, you can go to see that. I'll mainly talk about the way of writing Kruskal's sub-spanning tree that I saw these days.
Kruskal's sub-small spanning tree has three more variables than the original one, eq,del,used. These three functions are eq: whether there are equal edges, del: delete the edges in the original spanning tree, and used: whether they are the edges in the spanning tree. Then sort the edges after you put them into the structure, run a double for loop to find the edges with the same weight, mark them with eq, and then use a flag to determine which edges are in the first minimum spanning tree. Then enumerate each edge for judgment to see which edges are in the minimum spanning tree and have the same weights, then delete the edges, and then Kruskal to judge whether the spanning tree is a spanning tree, and then judge whether the spanning tree is equal to the minimum spanning tree.
Personally, Krusal feels better than prim's sub-spanning tree.
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=105*105;
struct node
{
int u,v,w;
bool eq,del,used;//The three functions are eq: whether there are equal edges, del: delete the edges in the original spanning tree, and used: whether they are the edges in the spanning tree.
}map[MAXN];
int fa[105];
int n,m,k;
bool flag;//The function of this flag is to determine the edges of the minimum spanning tree, because the edges in the minimum spanning tree are deleted when the edges in the non-spanning tree are added later.
void init()
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
int find(int p)
{
return p==fa[p]? p:fa[p]=find(fa[p]);
}
bool cmp(node c,node d)
{
return c.w<d.w;
}
int Kruskal()
{
int sum=0;
int num=0;//How many edges are there in the tree?
init();//The ancestor of the initialization point must be initialized every time, otherwise there will be an error when enumerating the spanning tree.
for(int i=0;i<k;i++)
{
if(map[i].del)//Delete the original edges in the spanning tree after ringing
continue;
int P=find(map[i].u);
int Q=find(map[i].v);
if(P!=Q)
{
sum+=map[i].w;
if(!flag) map[i].used=true;//Used to determine the edges in the minimum spanning tree
num++;
fa[P]=Q;
}
if(num>=n-1)
break;
}
return sum;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
k=0;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
map[k].u=u;
map[k].v=v;
map[k].w=w;
map[k].eq=false;
map[k].del=false;
map[k].used=false;
k++;
}
sort(map,map+k,cmp);
for(int i=0;i<k;i++)
for(int j=0;j<k;j++)
{
if(i==j)
continue;
if(map[i].w==map[j].w)//Judging whether there are equal weighted edges
map[i].eq=true;
}
flag=false;
int mst=Kruskal();
flag=true;
bool mark=false;
for(int i=0;i<k;i++)
{
if(map[i].used==true&&map[i].eq==true)//If this is the edge in the minimum spanning tree and has the same weight, you can add it to see if it can form a spanning tree.
{
map[i].del=true;//Used to delete edges in a spanning tree
int minn=Kruskal();
if(minn==mst)
{
mark=true;
printf("Not Unique!\n");
break;
}
map[i].del=false;
}
}
if(!mark)
printf("%d\n",mst);
}
}