1. 程式人生 > >The Unique MST

The Unique MST

edge ac代碼 false ~~ 全局 logs out ++ code

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[1]=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) 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]; } } 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.今天也是元氣滿滿的一天。

The Unique MST