POJ 1679 the unique MST "MST is the only, prime algorithm, the best code"

Source: Internet
Author: User

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 27389 Accepted: 9816

Description

Given a connected undirected graph, tell if it minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of 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, which has the and 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 <=), the number of test cases. Each case represents a graph. It begins with a line containing, integers n and m (1 <= n <=), the number of nodes and edges. Each of the following m lines contains a triple (xi, Yi, WI), indicating that Xi and Yi is connected by an edge with Weig HT = wi. For any of the nodes, there is at the most one edge connecting them.

Output

For each input, if the MST was 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!

Source

POJ monthly--2004.06.27[email protected]

Original title Link: http://poj.org/problem?id=1679

Test instructions: Look at the smallest spanning tree is unique, the Web read a lot of code, all use the enumeration, but found a code without enumeration,

Original link: http://blog.csdn.net/cambridgeacm/article/details/7857252, but one thing does not understand, is why the main loop is n times, not n-1 times. Is there a great God, you know?

AC Code:

#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int inf=0x3f3f3f3f;         int a[105][105];int dis[105];bool vis[105];int n,m;void Prime () {for (int i=1; i<=n; i++) {dis[i]=a[1][i];    Vis[i]=false;    } int ans=0;    BOOL Flag=false;        for (int i=1; i<=n; i++) {int minn=inf;        int p=-1;        for (int j=1; j<=n; J + +) {if (!vis[j]&&dis[j]<minn) minn=dis[p=j];        } int k=0;        for (int j=1; j<=n; J + +) {if (Vis[j]&&a[p][j]==minn) k++;            } if (k>1) {flag=true;        Break        } vis[p]=1;        Ans+=minn;        for (int j=1; j<=n; J + +) {if (!vis[j]&&dis[j]>a[p][j]) dis[j]=a[p][j]; }} if (flag) cout<< "Not unique!"    <<endl; else Cout<<ans<<endl;}   int main () { int T;    Freopen ("Data/1679.txt", "R", stdin);    cin>>t;        while (t--) {cin>>n>>m;                for (int i=0, i<=n; i++) {for (int j=0; j<=n; J + +) if (i==j) a[i][j]=0;        else A[i][j]=inf;        } int x, y, Z;            while (m--) {scanf ("%d%d%d", &x,&y,&z);        if (Z<a[x][y]) a[x][y]=a[y][x]=z;    } Prime (); } return 0;}



POJ 1679 the unique MST "MST is the only, prime algorithm, the best code"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.