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

    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!

    给你一个图 判断其最小生成树是不是唯一的

    记录第一个最小生成树的路径,枚举每一条边删除的情况,如果还能再生成一个长度为n-1并且值相等的最小生成树,那么就不是唯一的了

     在POJ discuss 找到的一组好样例

    9
    1 0
    4 5
    1 2 1
    2 3 1
    3 4 1
    1 4 2
    2 4 1
    10 15
    2 10 97
    2 6 18
    7 1 63
    5 4 62
    7 5 93
    1 3 10
    6 9 99
    3 7 73
    2 7 6
    5 9 22
    5 3 82
    4 2 36
    8 1 50
    10 3 20
    7 9 69
    10 15
    10 5 79
    4 2 33
    4 8 41
    9 3 97
    5 2 25
    2 6 9
    2 10 66
    8 3 38
    10 8 89
    1 10 83
    1 7 91
    7 3 94
    7 10 40
    7 2 70
    2 3 82
    10 15
    3 8 84
    7 10 34
    1 10 14
    1 9 60
    7 6 49
    8 5 39
    4 5 96
    4 7 78
    7 3 33
    2 8 56
    8 9 71
    5 2 83
    3 6 61
    7 9 63
    2 6 43
    10 15
    1 10 25
    1 3 14
    10 5 72
    8 3 18
    2 5 41
    4 9 86
    6 8 17
    6 2 98
    5 6 34
    1 8 90
    7 1 65
    7 2 63
    8 7 71
    4 2 64
    9 6 50
    10 15
    2 7 13
    5 10 52
    5 2 5
    10 6 47
    9 4 23
    8 10 54
    1 10 20
    4 10 8
    6 1 87
    8 2 43
    8 1 87
    6 3 53
    3 1 87
    2 3 82
    4 6 91
    10 15
    1 2 14
    4 1 89
    7 6 8
    9 4 81
    5 2 81
    10 9 6
    1 5 44
    1 3 33
    2 6 25
    6 10 10
    1 10 65
    6 9 74
    8 10 41
    2 3 89
    5 10 2
    10 15
    9 8 14
    2 10 66
    10 5 73
    2 3 98
    1 3 30
    6 5 3
    2 1 84
    2 6 33
    10 8 24
    5 8 34
    7 1 69
    3 7 60
    7 4 38
    4 10 65
    3 4 32
    
    答案是
    0
    Not Unique!
    287
    432
    406
    326
    264
    220
    273
    
    
    #include<iostream>
    #include<cstdio>     //EOF,NULL
    #include<cstring>    //memset
    #include<cstdlib>    //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
    #include<cmath>           //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
    #include<algorithm>  //fill,reverse,next_permutation,__gcd,
    #include<string>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<utility>
    #include<iterator>
    #include<iomanip>             //setw(set_min_width),setfill(char),setprecision(n),fixed,
    #include<functional>
    #include<map>
    #include<set>
    #include<limits.h>     //INT_MAX
    #include<bitset> // bitset<?> n
    using namespace std;
    
    #define rep(i,a,n) for(int i=a;i<n;i++)
    #define per(i,a,n) for(int i=n-1;i>=a;i--)
    #define fori(x) for(int i=0;i<x;i++)
    #define forj(x) for(int j=0;j<x;j++)
    #define memset(x,y) memset(x,y,sizeof(x))
    #define memcpy(x,y) memcpy(x,y,sizeof(y))
    #define all(x) x.begin(),x.end()
    #define readc(x) scanf("%c",&x)
    #define read(x) scanf("%d",&x)
    #define read2(x,y) scanf("%d%d",&x,&y)
    #define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
    #define print(x) printf("%d
    ",x)
    #define lowbit(x) x&-x
    #define lson(x) x<<1
    #define rson(x) x<<1|1
    #define pb push_back
    #define mp make_pair
    typedef pair<int,int> P;
    typedef long long LL;
    typedef long long ll;
    const double eps=1e-8;
    const double PI = acos(1.0);
    const int INF = 0x3f3f3f3f;
    const int inf = 0x3f3f3f3f;
    const int mod = 1e9+7;
    const int MAXN = 1e6+7;
    const int maxm = 1;
    const int maxn = 10000+10;
    int T;
    int n,m;
    int cnt,ans,tot,sum,pos;
    int pre[maxn];
    int path[maxn];
    int flag;
    
    struct node{
      int st,ed,w;
      bool operator < (node b) const{
         return w < b.w;
      }
    }rod[maxn];
    
    int find(int x){ return x == pre[x] ? x : pre[x] = find(pre[x]);}
    bool join(int x,int y){
        if(find(x)!=find(y)){
          pre[find(y)] = find(x);
          return true;
        }
        return false;
    }
    void kruskal(){
      for(int i = 0 ; i <= n; i++){
        pre[i] = i;
      }
        for(int i = 0 ;i < m ; i++){
        int mp1 = find(rod[i].st);
        int mp2 = find(rod[i].ed);
        if(join(mp1,mp2)) {
          ans += rod[i].w;
          path[tot++] = i;
        }
      }
      for(int k = 0; k < m ;k++){
          for(int i = 1; i <= n; i++){
            pre[i]  = i;
          }
          sum = pos = 0;
          for(int i = 0; i < m ;i++){
            if(i == path[k]) continue;
            int mp1 = find(rod[i].st);
            int mp2 = find(rod[i].ed);
            if(join(mp1,mp2)) {
              sum += rod[i].w;
              pos++;
            }
          }
          if(pos == n-1 && sum == ans){
            flag = 1; break;
          }
      }
    }
    int main(){
      read(T);
      while(T--){
        flag = cnt = ans = tot =  0;
        read2(n,m);
        if(n==1) {
          printf("0
    ");
          continue;
        }
        int a,b,c;
        for(int i = 0 ; i < m ;i++){
          read3(a,b,c);
          rod[i].st = a;
          rod[i].ed = b;
          rod[i].w = c;
        }
        sort(rod,rod+m);
        kruskal();
        if(flag)
          printf("Not Unique!
    ");
        else
          print(ans);
      }
    }
  • 相关阅读:
    Python:遍历文件目录及子目录,并批量改变文件名称
    python:利用递归遍历文件夹所有文件(包括子文件夹里的文件)
    python文件,文件夹操作
    Ubuntu安装vim报错的的处理
    Centos7安装Chrome
    Centos7更换阿里yum源
    CentOS7设置启动默认界面方法
    Vim快捷键学习---记性不行了,留这里备查
    第7-9章作业汇总
    第四次作业---第三题陈岩岩
  • 原文地址:https://www.cnblogs.com/llke/p/10780107.html
Copyright © 2011-2022 走看看