判断MST的是否唯一
郁闷的一题,WA了一下午,不断改代码,看了discuss存在非连通图(反正我添加了判断就ac了)。
思路是,对相等的边标记,先求一次mst,然后再对每一条存在相同的且被选中的边去除之后再求一次mst,如果结果中存在于第一次mst相同的值,怎不是唯一的。否则是唯一的。
The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14771 | Accepted: 5092 |
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!
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAXN 110
struct line
{
int s,e;
int l;
int used;
int del;
int equal;
};
line l[MAXN*MAXN];
int set[MAXN];
int n,m;
bool first;
bool cmp(line a,line b)
{
return a.l<b.l;
}
void init(int n)
{
for(int i=0;i<=n;i++)
set[i]=i;
}
//查找a属于的集合编号
int find(int a)
{
int root=a;
int temp;
while(set[root]!=root)
root=set[root];
while(set[a]!=root)
{
temp=a;
a=set[a];
set[temp]=root;
}
return root;
}
//合并两个集合
bool merge(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y)
return false;
set[x]=y;
return true;
}
int kruscal()
{
int ans=0;
int num=0;
init(n);
for(int i=1;i<=m && num<n-1 ;i++)
{
if(l[i].del!=1)
{
if(merge(l[i].s,l[i].e))
{
ans+=l[i].l;
if(first)
l[i].used=1;
num++;
}
}
}
if(num!=n-1)
return -1;
return ans;
}
void solve()
{
first=true;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>l[i].s>>l[i].e>>l[i].l;
l[i].used=l[i].equal=l[i].del=0;
}
for(int i=1;i<=m;i++)
{
for(int j=i+1;j<=m;j++)
{
if(l[i].l==l[j].l)
{
l[i].equal=l[j].equal=1;
}
}
}
sort(l+1,l+m+1,cmp);
int k1=kruscal();
if(k1==-1)
cout<<"0"<<endl;
int k2;
first=false;
for(int i=1;i<=m;i++)
{
if(l[i].used && l[i].equal)
{
l[i].del=1;
k2=kruscal();
if(k2==k1)
{
cout<<"Not Unique!"<<endl;
return ;
}
l[i].del=0;
}
}
cout<<k1<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
solve();
return 0;
}