Poj 1679 the unique MST (judge whether the minimum spanning tree is unique)

Source: Internet
Author: User

The unique MST
Time limit:1000 ms   Memory limit:10000 K
Total submissions:20679   Accepted:7255

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 '.

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

23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2

Sample output

3Not Unique!

Source

Poj monthly -- 2004.06.27 [email protected]

Determine whether the minimum spanning tree is unique:

1. Scan other edges for each edge in the graph. If an edge with the same weight exists, the edge is marked.

2. Use Kruskal or prim to find the MST.

3. If no marked edge exists in the MST, the MST is unique. Otherwise, the marked edge is removed from the MST and the MST is obtained, if the obtained MST value is the same as the original MST value, the MST is not unique.


#include"stdio.h"#include"string.h"#include"iostream"#include"algorithm"using namespace std;#define N 105const int inf=0x7fffffff;struct node{    int u,v,w;    int eq,used,del;}e[N*N];int n,first,m;int pre[N];bool cmp(node a,node b){    return a.w<b.w;}int find(int x){    if(x!=pre[x])        pre[x]=find(pre[x]);    return pre[x];}int kruskal(){    int i,f1,f2,ans,cnt;    ans=cnt=0;    for(i=1;i<=n;i++)        pre[i]=i;    for(i=0;i<m;i++)    {        if(e[i].del)            continue;        f1=find(e[i].u);        f2=find(e[i].v);        if(f1!=f2)        {            if(first)                e[i].used=1;            pre[f1]=f2;            ans+=e[i].w;            cnt++;            if(cnt>=n-1)                break;        }    }    return ans;}int main(){    int i,j,T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        for(i=0;i<m;i++)        {            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);            e[i].del=e[i].eq=e[i].used=0;        }        sort(e,e+m,cmp);        for(i=0;i<m;i++)        {            for(j=i+1;j<m;j++)            {                if(e[i].w==e[j].w)                {                    e[i].eq=e[j].eq=1;                }                else                    break;            }        }        first=1;        int ans=kruskal();        first=0;        for(i=0;i<m;i++)        {            if(e[i].used&&e[i].eq)            {                e[i].del=1;                if(kruskal()==ans)                    break;                e[i].del=0;            }        }        if(i<m)            printf("Not Unique!\n");        else            printf("%d\n",ans);    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.