题目链接:http://poj.org/problem?id=1679
The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29408 | Accepted: 10520 |
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! 题目大意: 给定一个连通无向网,判定它的最小生成树是否唯一。 解题思路: http://www.cnblogs.com/yoke/p/6527300.html AC代码:
#include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; struct point
{
int u,v,w;
int equal; // 标记,1表示存在其他边权值跟该边一样,0表示不存在
int used; // 在第一次求得的MST中,是否包含该边,1包含,0不包含
int del; // 边是否删除 0不删除 1删除
}p[]; // 存边的数组
int n,m;
int first; // 表示第一次求MST的标记变量
int parent[];
bool cmp(point a, point b)
{
return a.w < b.w;
}
int find (int x)
{
int s,tmp;
for (s = x; parent[s] >= ; s = parent[s]);
while (s != x)
{
tmp = parent[x];
parent[x] = s;
x = tmp;
}
return s;
}
void Union (int A, int B)
{
int a = find(A), b = find(B);
int tmp = parent[a]+parent[b];
if (parent[a] > parent[b])
{
parent[a] = b;
parent[b] = tmp;
}
else
{
parent[b] = a;
parent[a] = tmp;
}
}
int kruskal()
{
int sum = ,num = ;
memset(parent,-,sizeof(parent));
for (int i = ; i < m; i ++)
{
if (p[i].del) continue; // 忽略去掉的边
int u = p[i].u, v = p[i].v;
if (find(u) != find(v))
{
if (first) p[i].used = ;
sum += p[i].w;
Union(u,v);
num ++;
}
if (num == n-) break;
}
return sum;
}
int main ()
{
int t,i,j,u,v,w;
scanf("%d",&t);
while (t --)
{
scanf("%d%d",&n,&m);
for (i = ; i < m; i ++)
{
scanf("%d%d%d",&u,&v,&w);
p[i].u = u; p[i].v = v; p[i].w = w;
p[i].equal = ; p[i].used = ; p[i].del = ;
}
for (i = ; i < m; i ++) // 标记相同权值的边
for (j = i+; j < m; j ++)
if (p[i].w == p[j].w)
p[i].equal = ;
first = ;
sort(p,p+m,cmp);
int sum = kruskal(), sum1; // 第一次求MST
first = ;
for (i = ; i < m; i ++)
{
if (p[i].equal && p[i].used) // 依次去掉原MST中相同权值的边
{
p[i].del = ;
sum1 = kruskal();
if (sum == sum1)
{
printf("Not Unique!\n");
break;
}
p[i].del = ;
}
}
if (i == m)
printf("%d\n",sum);
}
return ;
}