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 }
  • 相关阅读:
    17-Flutter移动电商实战-首页_楼层区域的编写
    16-Flutter移动电商实战-切换后页面状态的保持AutomaticKeepAliveClientMixin
    15-Flutter移动电商实战-商品推荐区域制作
    AndroidStudio中Flutter打包APK
    windows下dos窗口实现持续ping显示时间保存至日志
    14-Flutter移动电商实战-ADBanner组件的编写
    13-Flutter移动电商实战-ADBanner组件的编写
    12-Flutter移动电商实战-首页导航区域编写
    11-Flutter移动电商实战-首页_屏幕适配方案和制作
    10-Flutter移动电商实战-使用FlutterSwiper制作轮播效果
  • 原文地址:https://www.cnblogs.com/titicia/p/3905750.html
Copyright © 2011-2022 走看看