The Unique MST

 
The Unique MST 
 
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, emmmmm 新来小白一枚,先找最小生成树,然后找一个未被使用的最小边进行判断



先放一个prim模板
int cost[maxn][maxn];
int mincost[maxn];
bool used[maxn];//用int也行,这个无所谓
int V;
int prim()
{
    for(int i=0;i<V;i++)
    {
        mincost[i]=inf;
        used[i]=false;
    }
    mincost[0]=1;
    int res=0;
    while(true)
    {
        int v=-1;
        for(int u=0;u<V;u++)
        {
            if(!used[u]&&(v==-1||mincost[u]<mincost[v]))
                v=u;
        }
        if(v==-1)
            break;
        used[v]=true;
        res+=mincost[v];
        for(int u=0;u<V;u++)
        {
            mincost[u]=min(mincost[u],cost[v][u]);
        }
    }
    return res;
}



不叭叭了
AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=110;
const int inf=100000000;
int n,m;
int MAP[maxn][maxn];  
int used[maxn];
int b[maxn];
//这三个数组是prim三兄弟
void once()//初始化而已
for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i!=j) MAP[i][j]=inf; else MAP[i][j]=0; } } }
void prim(int x)//我在这里传入了一个参数x,在这之前我用的是n, { for(int i=1;i<=n;i++) { b[i]=MAP[1][i];//这里只存数组第一排的
}
memset(used,
0,sizeof(used)); used[1]=1; int sum=0; int flag=0; while(x--)//因为n是全局变量,所以在这里再次传入的n和全局变量的n不一样,不建议重名。
//如果重名的话就不能用while(n--),就只能用for(int i=0;i<n;i++),或者在前面定义int node=n;热按后才可以用while(n--) 哇原来这么神奇的啊~~~~~~~ {
int u=-1;//这个u可以不赋初值 int ret=inf; for(int j=1;j<=n;j++) { if(!used[j]&&ret>b[j])//找和第一个位置最近的那个点权值 { ret=b[j]; u=j; } } if(ret==inf) break; sum+=ret; //求权值和 int k=0; //_____________________________
for(int j=1;j<=n;j++) { if(used[j]&&MAP[u][j]==ret)//找到u这一行,开始找有没有和刚刚找到的ret值相等的数 k++;//如果找到了,那就加加,记录 } if(k>1)//要是刚刚有找到一样权值的数,那换边之后只能是权值和一样了,所以不行 { flag=1; break; } //_________________________________和模板不一样的是中间加了这个。
used[u]
=1; for(int j=1;j<=n;j++) { if(!used[j]&&b[j]>MAP[u][j]) b[j]=MAP[u][j];//更新b数组 } } if(flag) printf("Not Unique!\n"); else printf("%d\n",sum); } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); once(); int a,b,c; for(int i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); MAP[a][b]=MAP[b][a]=c;//我也是今天才知道的这个数组还可以这样存~~~ } prim(n); } return 0; }

 

good luck!everyone.今天也是元气满满的一天。

posted @ 2017-08-20 16:36  ikefire  阅读(163)  评论(0编辑  收藏  举报