The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18528 | Accepted: 6479 |
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'.
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!
第一次kruscal标记用过的边,并且找出权相同的边,第二次kruscal把权相同的边删去,看看结果同不同,相同则表示有不一样的最小生成树;反之……
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <stack>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define M 10010
#define INF 10000000
using namespace std;
typedef long long ll;
int rank[101],father[101],first,n,m;
struct abc
{
int u,v,w,equal,used,del;
} e[M];
bool cmp(abc p,abc q)
{
return p.w<q.w;
}
int find(int x)
{
return father[x]==x?x:father[x]=find(father[x]);
}
void un(int x,int y)
{
if(x==y) return;
if(rank[x]>rank[y]) father[y]=x;
else
{
if(rank[x]==rank[y]) rank[y]++;
father[x]=y;
}
}
int kruscal()
{
int num=0,sum=0,u,v,i;
for(i=1; i<=n; i++)
{
rank[i]=0;
father[i]=i;
}
for(i=0; i<m; i++)
{
if(e[i].del==1) continue;
u=find(e[i].u);
v=find(e[i].v);
if(u!=v)
{
sum+=e[i].w;
num++;
un(u,v);
if(first) e[i].used=1;
}
if(num==n-1) break;
}
return sum;
}
int main()
{
int i,j,t;
sca(t);
while(t--)
{
cin>>n>>m;
for(i=0; i<m; i++)
{
cin>>e[i].u>>e[i].v>>e[i].w;
e[i].equal=0;
e[i].del=0;
e[i].used=0;
}
for(i=0; i<m; i++)
for(j=0; j<m; j++)
{
if(i==j) continue;
if(e[i].w==e[j].w) e[i].equal=1;
}
sort(e,e+m,cmp);
first=1;
int sum=kruscal();
first=0;
for(j=0; j<m; j++)
{
if(e[j].used&&e[j].equal)
{
e[j].del=1;
if(sum==kruscal())
{
cout<<"Not Unique!"<<endl;
break;
}
}
}
if(j==m) cout<<sum<<endl;
}
return 0;
}