The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 36692 | Accepted: 13368 |
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 <algorithm> #include <vector> #include <cstring> #include <iostream> using namespace std; #define line cout<<"------------------"<<endl; const int MAXN=1e4+10; const int INF=0x3f3f3f3f; int n,m; struct node{ int x,y; int v; bool vis; }Edge[MAXN]; bool cmp(node a,node b) { return a.v<b.v; } int pre[MAXN]; int Find(int a) { if(pre[a]==a) return a; return Find(pre[a]); } vector<int >G[110]; int maxd[110][110];//并查集划到一个树上后,树上任意两点之间的距离 void init() { for (int i = 1; i <=n; ++i) { G[i].clear(); pre[i] = i; G[i].push_back(i); } } int main() { int _; scanf("%d",&_); while(_--) { scanf("%d%d",&n,&m); init(); for(int i=1;i<=m;i++) { scanf("%d%d%d",&Edge[i].x,&Edge[i].y,&Edge[i].v); Edge[i].vis=false; } sort(Edge+1,Edge+1+m,cmp); int sum=0; for (int i = 1; i <=m ; ++i) { int x=Find(Edge[i].x); int y=Find(Edge[i].y); if(x!=y) { pre[x]=y; sum+=Edge[i].v; int len1=G[x].size(); int len2=G[y].size(); for (int j = 0; j <len1 ; ++j) { for (int k = 0; k <len2 ; ++k) { maxd[G[x][j]][G[y][k]]=maxd[G[y][k]][G[x][j]]=Edge[i].v;//构建两点间最小距离 } } int tem[110]; for (int j = 0; j <len2 ; ++j) { tem[j]=G[y][j]; } for (int j = 0; j <len1 ; ++j) { G[y].push_back(G[x][j]); } for (int j = 0; j <len2 ; ++j) { G[x].push_back(tem[j]); } Edge[i].vis=true; } } int cis=INF; for (int i = 1; i <=m ; ++i) {//从不是最小生成树上的边,遍历向上加。找到次小生成树 if(!Edge[i].vis) cis=min(cis,sum+Edge[i].v-maxd[Edge[i].x][Edge[i].y]); } if(cis>sum) printf("%d ",sum); else printf("Not Unique! "); } return 0; } //poj1679