zoukankan      html  css  js  c++  java
  • POJ 1679 The Unique MST(次短生成树)

    Language:
    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 29098   Accepted: 10404

    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!

    思路:找有没有次短生成树,先找出最小生成树,再把每条边替换,是否与最小生成树相同

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #define MAXN 201
    struct node {
        int x,y;
        int val;
    };
    node  a[MAXN*MAXN];
    int fa[MAXN],n,t,m,num[MAXN*MAXN],dis;
    bool flag;
    using namespace std;
    inline void read(int&x) {
        x=0;int f=1;char c=getchar();
        while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<1)+(x<<3)+c-48;c=getchar();}
        x=x*f;
    }
    inline int find(int x) {
        if(x==fa[x]) return x;
        else return fa[x]=find(fa[x]);
    }
    inline bool cmp(node x,node y) {
        return x.val<y.val;
    }
    inline void MST() {
        int cnt=0;dis=0;
        for(int i=1;i<=n;i++) fa[i]=i;
        for(int i=1;i<=m;i++) {
            int xx=find(a[i].x);
            int yy=find(a[i].y);
            if(xx!=yy) {
                fa[xx]=yy;
                dis+=a[i].val;
                num[cnt++]=i;
            }
        }
        flag=false;
        for(int k=0;k<cnt;k++) {
            for(int i=1;i<=n;i++) fa[i]=i;
            int ans=0,sum=0,t=0;
            for(int i=1;i<=m;i++) {
                if(i==num[k]) continue;
                int xx=find(a[i].x);
                int yy=find(a[i].y);
                if(xx!=yy) {
                    fa[xx]=yy;
                    ans+=a[i].val;
                    t++;
                }
            }
            if(t!=cnt) continue;
            if(ans==dis) {
                flag=true;
                return;
            }
        }
    }
    int main() {
        read(t);
        while(t--) {
            read(n);read(m);
            for(int i=1;i<=m;i++) {read(a[i].x);read(a[i].y);read(a[i].val);}
            sort(a+1,a+1+m,cmp);
            MST();
            if(flag) printf("Not Unique!
    ");
            else printf("%d
    ",dis);
        }
        return 0;
    }
    View Code


    作者:乌鸦坐飞机
    出处:http://www.cnblogs.com/whistle13326/
    新的风暴已经出现 怎么能够停止不前 穿越时空 竭尽全力 我会来到你身边 微笑面对危险 梦想成真不会遥远 鼓起勇气 坚定向前 奇迹一定会出现

     
  • 相关阅读:
    Effective C++ 读书笔记(3544):继承关系与面向对象设计
    《全景探秘游戏设计艺术》读后感之引子
    Effective C++ 读书笔记(1117):构造析构和赋值函数
    Effective C++ 读书笔记(2934):类与函数之实现
    Unity中使用PersistentDataPath加载文件
    打开本地【C】【D】【E】驱动器时候提示 X:\ 找不到应用程序
    C#进制转换
    在VS里编辑unity代码调用系统方法不显示中文注释或英文注释
    Spreadsheet说明
    C#中删除控件的事件的方法类.
  • 原文地址:https://www.cnblogs.com/whistle13326/p/6366291.html
Copyright © 2011-2022 走看看