The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 33838 | Accepted: 12302 |
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<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#define FAST ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N=1000+5;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
int a[MAX_N],cost[MAX_N][MAX_N],vis[MAX_N],path[MAX_N][MAX_N],mincost[MAX_N],pre[MAX_N],used[MAX_N][MAX_N];
int n,m,ans1,ans2;
void prim(){
for(int i=2;i<=n;i++) mincost[i]=INF;
mincost[1]=0;
for(int i=1;i<=n;i++){
int mn=INF,v=-1;
for(int j=1;j<=n;j++)
if(!vis[j]&&mincost[j]<mn) mn=mincost[j],v=j;
// cout <<"v="<<v<<endl;
if(pre[v]!=-1){
used[v][pre[v]]=used[pre[v]][v]=1;
// printf("%d %d no\n",v,pre[v]);
for(int u=1;u<=n;u++){
if(vis[u]){
path[u][v]=path[v][u]=max(cost[v][pre[v]],path[u][pre[v]]);
}
}
}
ans1+=mincost[v];
vis[v]=1;
for(int j=1;j<=n;j++){
if(!vis[j]&&cost[v][j]<mincost[j]){
// printf("~~~%d %d\n",v,j);
pre[j]=v;
mincost[j]=cost[v][j];
}
}
}
}
void find_second(){
ans2=INF;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(!used[i][j]){
// if(ans1-path[i][j]+cost[i][j]<ans2)
// printf("!!!!%d %d\n",i,j);
ans2=min(ans2,ans1-path[i][j]+cost[i][j]);
}
}
}
}
int main(void){
int t;cin >> t;
while(t--){
ans1=ans2=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) cost[i][j]=INF;
memset(vis,0,sizeof vis);
memset(pre,-1,sizeof pre);
memset(used,0,sizeof used);
memset(path,0,sizeof path);
for(int i=1;i<=m;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
cost[u][v]=cost[v][u]=c;
}
// cout <<cost[1][3]<<endl;
prim();
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n;j++){
// printf("path[%d][%d]=%d\n",i,j,path[i][j]);
// }puts("");
// }
find_second();
if(ans1==ans2){
cout <<"Not Unique!"<<endl;
}
else{
cout << ans1 << endl;
}
}
}