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

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 20430   Accepted: 7186

    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!


    先求一次最小生成树,然后枚举边,如果去掉这条边的最小生成树价值和原来一样,则不唯一。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <string>
     5 #include <iomanip>
     6 #include <algorithm>
     7 #include <queue>
     8 #include <vector>
     9 #include <map>
    10 using namespace std;
    11 struct Node{
    12     int u, v, w;
    13 }node[110*110/2];
    14 int t, n, m, sum1, sum2,  ans1, ans2;
    15 int pre[110], mark[110*110/2];
    16 int find(int x){
    17     if(pre[x] == x) return x;
    18     else return find(pre[x]);
    19 }
    20 bool cmp(Node x, Node y){
    21     return x.w < y.w;
    22 }
    23 
    24 int main(){
    25     scanf("%d", &t);
    26     while(t--){
    27         scanf("%d%d", &n, &m);
    28         for(int i = 1; i <= m; i++){
    29             scanf("%d%d%d", &node[i].u, &node[i].v, &node[i].w);
    30         }
    31         sort(node+1,node+1+m,cmp);
    32         sum1 = 0;
    33         ans1 = 0;
    34         for(int i = 1; i <= n; i++) pre[i] = i;
    35         for(int i = 1; i <= m; i++){
    36             if(ans1 == n-1) break;
    37             int aa = find(node[i].u);
    38             int bb = find(node[i].v);
    39             if(aa != bb){
    40                 sum1 += node[i].w;
    41                 pre[aa] = bb;
    42                 mark[++ans1] = i;
    43             //    ans++;
    44             }
    45         }
    46         bool flag = true;
    47         for(int k = 1; k <= ans1; k++){
    48             for(int i = 1; i <= n; i++) pre[i] = i;
    49             sum2 = 0; ans2 = 0;
    50             for(int i = 1; i <= m; i++){
    51                 if(i == mark[k]) continue;
    52                 if(ans2 == n-1) break;
    53                 int aa = find(node[i].u);
    54                 int bb = find(node[i].v);
    55                 if(aa != bb){
    56                     sum2 += node[i].w;
    57                     pre[aa] = bb;
    58                     ans2++;
    59                 }
    60             }
    61             if(ans2 == n-1 && sum2 == sum1){
    62                 flag = false; break;
    63             }
    64         }
    65         if(flag) printf("%d
    ", sum1);
    66         else printf("Not Unique!
    ");
    67         
    68         
    69 
    70     }
    71     
    72     return 0;
    73 }
  • 相关阅读:
    gRPC java 客户端,服务器端通讯使用json格式
    HDTV(1920x1080)码率和视频质量关系的研究 2 (实验结果)
    Fedora 18/19没有注销
    window API一天一练之邮槽
    在C语言环境下使用google protobuf
    Cantor展开式
    LeetCode题解:Rotate List
    不知不觉vs2012 update 4出来了
    http://download.csdn.net/detail/yanzi1225627/6548337
    【虚拟化实战】Cluster设计之一资源池
  • 原文地址:https://www.cnblogs.com/titicia/p/3905750.html
Copyright © 2011-2022 走看看