zoukankan      html  css  js  c++  java
  • POJ——T1679 The Unique MST

    http://poj.org/problem?id=1679

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 30120   Accepted: 10778

    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!
    

    Source

     
     
    蒟蒻就是弱、、、
     1 #include <algorithm>
     2 #include <cstdio>
     3 
     4 using namespace std;
     5 
     6 const int N(10001);
     7 int num,n,m;
     8 int fa[N],cnt;
     9 int Fir,MST;
    10 int u,v,w,used[N];
    11 struct Edge
    12 {
    13     int u,v,w;
    14 } edge[N<<1];
    15 
    16 bool cmp(Edge a,Edge b)
    17 {
    18     return a.w<b.w;
    19 }
    20 
    21 int find(int x)
    22 {
    23     return fa[x]==x?x:fa[x]=find(fa[x]);
    24 }
    25 
    26 int Kruskal()
    27 {
    28     int ans=0; cnt=0;
    29     sort(edge+1,edge+m+1,cmp);
    30     for(int i=1; i<=n; i++) fa[i]=i;
    31     for(int i=1; i<=m; i++)
    32     {
    33         int fx=find(edge[i].u),fy=find(edge[i].v);
    34         if(fx!=fy)
    35         {
    36             fa[fx]=fy;
    37             used[++cnt]=i;
    38             ans+=edge[i].w;
    39         }
    40         if(cnt==n-1) return ans;
    41     }
    42     return ans;
    43 }
    44 
    45 int SecKru(int cant)
    46 {
    47     int ans=0; cnt=0;
    48     for(int i=1;i<=n;i++) fa[i]=i;
    49     for(int i=1;i<=m;i++) 
    50     {
    51         if(cant==i) continue;
    52         int fx=find(edge[i].u),fy=find(edge[i].v);
    53         if(fx!=fy)
    54         {
    55             cnt++;
    56             fa[fx]=fy;
    57             ans+=edge[i].w;
    58         }
    59         if(cnt==n-1) return ans;
    60     }
    61     return 0x7fffffff;
    62 }
    63 
    64 int main() 
    65 {
    66     scanf("%d",&num);
    67     for(;num--;)
    68     {
    69         scanf("%d%d",&n,&m);
    70         for(int i=1;i<=m;i++) 
    71             scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
    72         Fir=Kruskal(); MST=0x7fffffff;
    73         for(int i=1;i<n;i++)
    74         {
    75             MST=min(SecKru(used[i]),MST);
    76         }
    77         if(Fir==MST)
    78               printf("Not Unique!
    ");
    79         else  printf("%d
    ",Fir);
    80     }
    81     return 0;
    82 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    关于CSS网页布局id与class命名的问题
    JavaScript弹出层技术探讨文章收藏
    Jquery实现弹窗
    Asp.net中时间格式化的几种方法
    新建商品时,导入商品,随机文件名称的形成
    Linux问题:Crontab 执行shell脚本时相对路径的问题,不能识别。
    linux环境php扩展pdo_mysq安装l错误
    测试php代码的运行时间
    Apache的工作方式
    PHP时间处理
  • 原文地址:https://www.cnblogs.com/Shy-key/p/6821073.html
Copyright © 2011-2022 走看看