zoukankan      html  css  js  c++  java
  • poj 1679 The Unique MST

    The Unique MST
    Time Limit: 1000MS Memory Limit: 10000K
    Total Submissions: 34254 Accepted: 12473
    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<stack>
    #include<queue>
    #include<math.h>
    #include<vector>
    #include<string>
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<map>
    #include<algorithm>
    #define maxn 10005
    #define MAXN 10000
    #define MAXM 10005
    #define mem(a,b) memset(a,b,sizeof(a))
    #define ll long long
    #define inf 0x3f3f3f3f
    using namespace std;
    struct node{
        int x,y,z,id;
    }d[maxn],d1[maxn];
    int mark[maxn];
    int f[maxn];
    int fin(int x){
        if(f[x]==x)
            return x;
        else return f[x]=fin(f[x]);
    }
    bool join(int a,int b){
        int x=fin(a),y=fin(b);
        if(x!=y){
            f[x]=y;return true;
        }
        return false;
    }
    bool cmp(node n,node m){
        return n.z<m.z;
    }
    void init(int n){
        for(int i=0;i<=n;i++)f[i]=i;
    }
    int main(){
        int t;scanf("%d",&t);
        while(t--){
            mem(d,0);mem(d1,0);mem(mark,0);
            int n,m;scanf("%d%d",&n,&m);
            init(n);
            for(int i=0;i<m;i++){
                scanf("%d%d%d",&d[i].x,&d[i].y,&d[i].z);
            }
            sort(d,d+m,cmp);int x=0,ans1=0,ans2=0,l=0;
            for(int i=0;i<m;i++){
                if(join(d[i].x,d[i].y)){
                    x++;ans1+=d[i].z;
                    mark[l++]=i;
                }
                if(x==n-1)break;
            }
            int y=inf,flag=0;
            for(int i=0;i<l;i++){
                 swap(y,d[mark[i]].z);
                 memcpy(d1,d,sizeof(d1));
                 sort(d1,d1+m,cmp);
                 x=0;ans2=0; init(n);
                for(int j=0;j<m;j++){
                if(join(d1[j].x,d1[j].y)){
                    x++;ans2+=d1[j].z;}
                if(x==n-1)break;
                }swap(y,d[mark[i]].z);
                if(ans1==ans2){flag=1;break;}
            }
            if(flag==0)printf("%d
    ",ans1);
            else printf("Not Unique!
    ");
            }
    }
    




  • 相关阅读:
    终于把老板的项目搞完了---最后一步项目部署
    linux rz/sz 拖动文件上传
    layui之table.render使用(含后台详细代码实现)
    layui upload 后台获取不到值
    Layui upload动态传参,后台接收不到,解决方法
    hibernate 多条件查询,查询部分字段等操作
    IDEA自动生成序列化ID
    MySQL范围查询(日期)
    安全随机数!Java 随机数 Random 与 SecureRandom
    java poi 写excel到指定目录
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053238.html
Copyright © 2011-2022 走看看