首页 > 代码库 > POJ 1679 The Unique MST(推断最小生成树_Kruskal)
POJ 1679 The Unique MST(推断最小生成树_Kruskal)
Description
Given a connected undirected graph, tell if its minimum spanning tree is unique.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V‘, E‘), with the following properties:
1. V‘ = V.
2. T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E‘) of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E‘.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V‘, E‘), with the following properties:
1. V‘ = V.
2. T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E‘) of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E‘.
Input
The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a
triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.
Output
For each input, if the MST is unique, print the total cost of it, or otherwise print the string ‘Not Unique!‘.
Sample Input
2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2
Sample Output
3 Not Unique!
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int father[111],n,m,first; struct node { int u,v,w; int used; int equal; int del; } a[11111]; bool cmp(node x,node y) { if(x.w<y.w) return true; return false; } int find(int x) { int r=x; while(father[r]!=r) r=father[r]; int i=x,j; while(i!=r) { j=father[i]; father[i]=r; i=j; } return r; } int prime() { int i,j,k,sum,num; sum=0;num=0; for(i=1;i<=n;i++) father[i]=i; for(i=1;i<=m;i++) { if(a[i].del) continue; int fx=find(a[i].u); int fy=find(a[i].v); if(fx!=fy) { num++; father[fx]=fy; sum+=a[i].w; if(first) a[i].used=1; } if(num==n-1) break; } return sum; } int main() { int i,j,k,u,v,w,sum1,sum2; int t; scanf("%d",&t); while(t--) { sum1=sum2=0; memset(a,0,sizeof(a)); scanf("%d%d",&n,&m); for(i=1;i<=m;i++) { scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w); } for(i=1;i<=m;i++) { for(j=i+1;j<=m;j++) { if(a[i].w==a[j].w) a[i].equal=1; } } sort(a+1,a+1+m,cmp); first=1; sum1=prime(); first=0; for(i=1;i<=m;i++) { if(a[i].used && a[i].equal) { a[i].del=1; sum2=prime(); if(sum1==sum2) { printf("Not Unique!\n"); break; } } } if(i==m+1) printf("%d\n",sum1); } }
POJ 1679 The Unique MST(推断最小生成树_Kruskal)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。