The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23978 | Accepted: 8505 |
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
3Not Unique!
题意:给你N个点、M条无向边以及边的权值。问你MST是否唯一,若唯一输出MST的值,反之输出Not Unique!.
次小生成树模版看这里:点我
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 100+10 #define INF 100000000 using namespace std; int Map[MAXN][MAXN]; bool vis[MAXN]; int low[MAXN];//上面三个数组 求MST 专用 int MST[MAXN][MAXN];//MST中 i 和 j的最大边权 int pre[MAXN];//记录前驱 bool InMST[MAXN][MAXN];//标记该边是否在MST中 int N, M; void init() { for(int i = 1; i <= N; i++) { Map[i][i] = 0; for(int j = 1; j < i; j++) Map[i][j] = Map[j][i] = INF; } } void getMap() { int a, b, c; for(int i = 1; i <= M; i++) { scanf("%d%d%d", &a, &b, &c); if(Map[a][b] > c) Map[a][b] = Map[b][a] = c; } } void solve() { //prime求MST int mincost = 0;//最小代价 int next, Min; memset(InMST, false, sizeof(InMST)); memset(MST, 0, sizeof(MST)); for(int i = 1; i <= N; i++) { low[i] = Map[1][i]; vis[i] = false; pre[i] = 1;//每个点前驱 } vis[1] = true; for(int i = 2; i <= N; i++) { Min = INF; next = -1; for(int j = 1; j <= N; j++) { if(!vis[j] && Min > low[j]) { next = j; Min = low[j]; } } mincost += Min; vis[next] = true; int fa = pre[next];//当前找到点的 前驱 InMST[next][fa] = InMST[fa][next] = true;//在MST中 for(int j = 1; j <= N; j++) { if(vis[j] && j != next)//MST中的点 MST[j][next] = MST[next][j] = max(MST[fa][j], low[next]); if(!vis[j] && low[j] > Map[next][j])//更新low { low[j] = Map[next][j]; pre[j] = next;//更新前驱 } } } //判断MST是否唯一 for(int i = 1; i <= N; i++) { for(int j = 1; j < i; j++) { if(Map[i][j] != INF && !InMST[i][j])//有边 且不在MST中 { if(Map[i][j] == MST[i][j]) { printf("Not Unique!\n"); return ; } } } } printf("%d\n", mincost); } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d%d", &N, &M); init(); getMap(); solve(); } return 0; }