The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 25231 | Accepted: 9012 |
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<string.h> #include<iostream> #include<algorithm> using namespace std; #define N 110 #define INF 0xffffff int map[N][N];//图 int low[N];//到源点的距离 int pre[N]; //记录该店的前驱 int vis[N];//标记是否走过 int mst[N][N];// 记录在最小生成树上 连接两点的路径上的最长的一条路 int inmst[N][N];//记录在最小树中 int dot,road;//节点数,路的数量 void init()//初始 { int i,j; for(i=1;i<=dot;i++) { map[i][i]=0; for(j=i+1;j<=dot;j++) map[i][j]=map[j][i]=INF; } } void Getp() { int i,j; int start ,end,dist; for(i=0;i<road;i++) { scanf("%d%d%d",&start,&end,&dist); map[start][end]=map[end][start]=dist; } } void solve() { int i,j; int mindist,next,min=0; memset(inmst,0,sizeof(inmst)); memset(mst,0,sizeof(mst)); for(i=1;i<=dot;i++) { low[i]=map[1][i]; pre[i]=1; } memset(vis,0,sizeof(vis)); vis[1]=1; for(i=2;i<=dot;i++) { mindist=INF,next=-1; for(j=1;j<=dot;j++) { if(!vis[j]&&mindist>low[j]) { mindist=low[j]; next=j; } } vis[next]=1; min+=mindist; int father=pre[next]; inmst[next][father]=inmst[father][next]=1;//标记,该路出现在最小树中 for(j=1;j<=dot;j++) { if(vis[j]&&j!=next) { mst[j][next]=mst[next][j]=max(low[next],mst[father][j]);//记录在最小树上任意两点之间的路径上,相连节点距离最长的长度值! } if(!vis[j]&&low[j]>map[next][j]) { low[j]=map[next][j]; pre[j]=next; } } } for(i=1;i<=dot;i++) { for(j=i+1;j<=dot;j++) { int t=map[i][j]; int t1=inmst[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",min); } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&dot,&road); { init(); Getp(); solve(); } } return 0; }