The Unique MST POJ1679

題目:

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!

題意:

判斷最小生成樹是否唯一,如果唯一嘖輸出長度,否則輸出“Not Unique!”;

先進行一次Kruskal,保存下來每一條邊,之後再依次將每一條邊都刪除進行Kruskal,如果有結果與第一次相同則直接輸出

Not Unique!;

注意:

進行完kruskal後一定要判斷是否依然連通,如果不連通一定要返回小於0的數(因爲返回0wa了好多次)

同時在kruskal中記得初始化;

代碼:

#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<string.h>
using namespace std;
struct edge
{
    int u,v,w;
}e[10005];
int n,m,t;
int f[105];
int num[10005];
bool book[10005];
void intt()
{
    for(int i=0;i<=n;i++)
        f[i]=i;
}
int getf(int v)
{
    if(f[v]==v)
        return v;
    else
    {
        f[v]=getf(f[v]);
        return  f[v];
    }
}
bool mergee(int v,int u)
{
    int t1,t2;
    t1=getf(v);
    t2=getf(u);
    if(t1!=t2)
    {
        f[t2]=t1;
        return 1;
    }
    return 0;
}
bool cmp(const edge &a,const edge &b)
{
    return a.w<b.w;
}
int Kruskal(int a)//消失哪一條邊
{
    int sum=0;
    int countt=0;
    intt();
    for(int i=1;i<=m;i++)
    {
        if(i==a)//跳過需要消失的那條邊
            continue;
        if(mergee(e[i].u,e[i].v))
        {
            countt++;
            sum+=e[i].w;
            //printf("%d\n",sum);
            if(a==0)//記錄最小生成樹的邊
                book[i]=1;
        }
        if(countt==n-1)
            break;
    }
    for(int i=1;i<n;i++)//已經無法連通
    {
        if(getf(i)!=getf(i+1))
        {
            return -1;
        }
    }
    return sum;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        for(int i=1;i<=m;i++)
        {
           scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w);
        }
        intt();
        memset(book,0,sizeof(book));
        bool flag=0;
        sort(e+1,e+m+1,cmp);
        int ans=Kruskal(0);//第一次哪一條邊也不消失
        //printf("%d\n",ans);
        for(int i=1;i<=m;i++)
        {

            if(book[i])
            {
                int cnt=Kruskal(i);
                if(cnt==ans)
                {
                    flag=1;
                    break;
                }
            }
        }
        if(flag)
            printf("Not Unique!\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章