zoukankan      html  css  js  c++  java
  • POJ 1679 The Unique MST 推断最小生成树是否唯一

    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
    ",ans1);
    
        }
    
        return 0;
    }
    
    
    






  • 相关阅读:
    win10安装virtualBox创建CentOS6.5虚拟机
    ES系列二、CentOS7安装ES head6.3.1
    ES系列一、CentOS7安装ES 6.3.1、集成IK分词器
    Python easyGUI 猜数字
    Python easyGUI 登录框 非空验证
    Python easyGUI 文件浏览 显示文件内容
    Python easyGUI 文件对比 覆盖保存
    Python 统计代码量
    什么是一个人真正的魅力?
    Python学习笔记(15)- osos.path 操作文件
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6844456.html
Copyright © 2011-2022 走看看