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’.
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!
题意:最小生成树是否唯一?
题解:求一个最小生成树和一个次小生成树,因为要去训练了,所以详细内容发布到下一篇文章,另外,把prim和次小生成树理解背诵一下
/* * 次小生成树 * 求最小生成树时,用数组Max[i][j]来表示MST中i到j最大边权 * 求完后,直接枚举所有不在MST中的边,替换掉最大边权的边,更新答案 * 点的编号从0开始 */ #include<algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cassert> #include <cstdio> #include <vector> #include <string> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define P(a,b,c) make_pair(a,make_pair(b,c)) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define CLR(vis) memset(vis,0,sizeof(vis)) #define MST(vis,pos) memset(vis,pos,sizeof(vis)) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef pair<int,pair<int,int> >pii; typedef long long ll; const ll mod = 1000000007; ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } const int inf=0x3f3f3f3f; const int maxn=100+10; bool link[maxn][maxn],vis[maxn]; int w[maxn][maxn],lowc[maxn],pre[maxn],Max[maxn][maxn]; int n,m; int prim() { int i,j,p,k; int minc,res=0; memset(vis,false,sizeof(vis)); memset(pre,0,sizeof(pre)); memset(Max,0,sizeof(Max)); vis[1]=true,pre[1]=1; for(i=2; i<=n; i++) //初始化 { lowc[i]=w[1][i]; pre[i]=1; } for(i=2; i<=n; i++) //prim { minc=inf,p=-1; for(j=1; j<=n; j++) { if(!vis[j]&&lowc[j]<minc) { minc=lowc[j]; p=j; } } vis[p]=true; res+=minc;//最小生成树加权值 Max[pre[p]][p]=minc; link[pre[p]][p]=true;//将这两条边标记为最小生成树的边 link[p][pre[p]]=true; for(k=1; k<=n; k++) Max[k][p]=max(Max[pre[p]][p],Max[k][p]); for(j=1; j<=n; j++) if(!vis[j]&&lowc[j]>w[p][j]) { lowc[j]=w[p][j]; pre[j]=p; } } return res; } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int s,e,t,ans,ans1; int h; scanf("%d", &h); while(h--) { scanf("%d%d",&n,&m); int i,j; bool ok=true;//是否唯一最小生成树的标志 for(i=1; i<=n; i++) for(j=1; j<=n; j++) w[i][j]=inf; memset(link,false,sizeof(link)); for(i=1; i<=m; i++) { scanf("%d%d%d",&s,&e,&t); w[s][e]=t; w[e][s]=t; } ans=prim();//最小生成树的权值 for(i=1; i<=n; i++) { for(j=i+1; j<=n; j++) { if(w[i][j]!=inf&&!link[i][j]) { ans1=ans+w[i][j]-Max[i][j];//ans1次小生成树的权值 } if(ans1==ans) { ok=0; break; } } if(!ok) break; } if(ans==-1||ans==ans1)printf("Not Unique!"); else printf("%d", ans); if(h)printf("\n"); } return 0; }