POJ 1679 The Unique MST 判斷最小產生樹是否唯一,pojmst

來源:互聯網
上載者:User

POJ 1679 The Unique MST 判斷最小產生樹是否唯一,pojmst

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22715   Accepted: 8055

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

23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2

Sample Output

3Not Unique!


#include <iostream>#include <stdio.h>#include <string>#include <cstring>#include <cmath>#include <algorithm>#define N 10090using namespace std;struct Node{    int a,b,c;    bool same,used,del;}f[N];int n,m;int fa[N];int findfa(int x){    if(x!=fa[x])    fa[x]=findfa(fa[x]);    return fa[x];}void init(){    for(int i=0;i<200;i++)    fa[i]=i;}int cmp(Node a,Node b){    return a.c<b.c;}bool first;void make_same(int m){    for(int i=1;i<m;i++)    if(f[i].c==f[i-1].c)    f[i-1].same=true;}int kruscal(int m){    int ans=0;    for(int i=0;i<m;i++)    {        if(f[i].del)continue;        int x=findfa(f[i].a);        int y=findfa(f[i].b);        if(x==y)        continue;        else        {            fa[x]=y;            ans+=f[i].c;            if(first)            f[i].used=true;        }    }    return ans;}int main(){    int ca=1;    scanf("%d",&ca);    while(ca--)    {        scanf("%d %d",&n,&m);        for(int i=0;i<m;i++)        {            scanf("%d %d %d",&f[i].a,&f[i].b,&f[i].c);            f[i].del=false;f[i].same=false;f[i].used=false;        }        sort(f,f+m,cmp);        first=true;        init();        int ans1=kruscal(m);        first=false;        make_same(m);        int flag=0;        for(int i=0;i<m;i++)        {            if(f[i].used && f[i].same)//used表示在第一次求出的最小產生樹中添加過的邊            {//same表示在存在和已添加邊權值相同的邊,此時標記刪除該邊在判斷是否ans相等                f[i].del=true;                init();                int ans2=kruscal(m);                //cout<<"ans2="<<ans2<<endl;                if(ans1==ans2)                {                    puts("Not Unique!");                    flag=1;                    break;                }                f[i].del=false;            }        }        if(flag==0)        printf("%d\n",ans1);    }    return 0;}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.