码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1679 The Unique MST 判断最小生成树是否唯一

时间:2015-06-05 10:12:05      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22715   Accepted: 8055

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

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 <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 10090
using namespace std;

struct Node
{
    int a,b,c;
    bool same,used,del;
}f[N];
int n,m;
int fa[N];

int findfa(int x)
{
    if(x!=fa[x])
    fa[x]=findfa(fa[x]);

    return fa[x];
}

void init()
{
    for(int i=0;i<200;i++)
    fa[i]=i;
}

int cmp(Node a,Node b)
{
    return a.c<b.c;
}

bool first;

void make_same(int m)
{
    for(int i=1;i<m;i++)
    if(f[i].c==f[i-1].c)
    f[i-1].same=true;
}

int kruscal(int m)
{
    int ans=0;
    for(int i=0;i<m;i++)
    {
        if(f[i].del)continue;

        int x=findfa(f[i].a);
        int y=findfa(f[i].b);

        if(x==y)
        continue;
        else
        {
            fa[x]=y;

            ans+=f[i].c;
            if(first)
            f[i].used=true;

        }
    }
    return ans;
}

int main()
{
    int ca=1;
    scanf("%d",&ca);

    while(ca--)
    {
        scanf("%d %d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d %d %d",&f[i].a,&f[i].b,&f[i].c);
            f[i].del=false;f[i].same=false;f[i].used=false;
        }

        sort(f,f+m,cmp);

        first=true;
        init();

        int ans1=kruscal(m);
        first=false;

        make_same(m);

        int flag=0;

        for(int i=0;i<m;i++)
        {
            if(f[i].used && f[i].same)//used表示在第一次求出的最小生成树中添加过的边
            {//same表示在存在和已添加边权值相同的边,此时标记删除该边在判断是否ans相等

                f[i].del=true;
                init();

                int ans2=kruscal(m);

                //cout<<"ans2="<<ans2<<endl;
                if(ans1==ans2)
                {
                    puts("Not Unique!");
                    flag=1;
                    break;
                }
                f[i].del=false;
            }
        }

        if(flag==0)
        printf("%d\n",ans1);

    }

    return 0;
}








POJ 1679 The Unique MST 判断最小生成树是否唯一

标签:

原文地址:http://blog.csdn.net/wust_zjx/article/details/46372139

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!