zoukankan      html  css  js  c++  java
  • [poj1679]The Unique MST(最小生成树)

    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 28207   Accepted: 10073

    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

     
    求一个图是否有不同的最小生成树
    大家都太暴力,枚举去那条边,暴力一遍判断重复
    但其实可以更快
    可以参考这篇论文(注意,文章中代码我认为有错,请自行思考)
    http://www.docin.com/p-806495282.html
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<climits>
     6 #include<algorithm>
     7 #include<queue>
     8 #define LL long long
     9 using namespace std;
    10 typedef struct{
    11     int to,frm,dis;
    12 }edge;
    13 edge gra[20010];
    14 int num=0,fa[110];
    15 int n,m;
    16 int cmp(const edge &a,const edge &b){
    17     return a.dis<b.dis;
    18 }
    19 int fnd(int x){
    20     return x==fa[x]?x:fnd(fa[x]);
    21 }
    22 int uni(int x,int y){
    23     int fx=fnd(x);
    24     int fy=fnd(y);
    25     fa[fy]=fx;
    26     return 0;
    27 }
    28 inline int read(){
    29        int sum=0;char ch=getchar();
    30        while(ch>'9'||ch<'0')ch=getchar();
    31        while(ch<='9'&&ch>='0'){
    32              sum=sum*10+ch-'0';
    33              ch=getchar();
    34        }
    35        return sum;
    36 }
    37 int kru(){
    38     int ans=0;
    39     sort(gra+1,gra+m+1,cmp);
    40     for(int i=1;i<=n;i++)fa[i]=i;
    41     for(int i=1;i<=m;i++){
    42         int x=gra[i].frm;
    43         int y=gra[i].to;
    44         int fx=fnd(x);
    45         int fy=fnd(y);
    46         if(fx!=fy){
    47             int j=i+1;
    48             while(j<=m&&gra[j].dis==gra[i].dis){
    49                 int y1=gra[j].frm;
    50                 int x1=gra[j].to;
    51                 int fy1=fnd(y1);
    52                 int fx1=fnd(x1);
    53                 if((fx1==fx&&fy1==fy)||(fx1==fy&&fy1==fx))return -1;
    54                 j++;
    55             }
    56             ans+=gra[i].dis;
    57             uni(fx,fy);
    58         }
    59     }
    60     return ans;
    61 }
    62 int main(){
    63     int t;
    64     t=read();
    65     while(t--){
    66         memset(gra,0,sizeof(gra));
    67           n=read(),m=read();
    68           num=0;
    69         for(int i=1;i<=m;i++){
    70             gra[i].frm=read();
    71             gra[i].to=read();
    72             gra[i].dis=read();
    73         }
    74 
    75         int ans=kru();
    76         if(ans==-1)printf("Not Unique!
    ");
    77         else printf("%d
    ",ans);
    78     }
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    网络基础知识
    mysql安装
    docker打包镜像
    python的基础
    python静态属性的理解
    python中的静态方法和类方法
    python类的两种创建方式
    python的继承
    python中time和datetime模块
    python之模块
  • 原文地址:https://www.cnblogs.com/Pumbit-Legion/p/5925326.html
Copyright © 2011-2022 走看看