题目大意:判断最小生成树是否唯一。
思路:先标记最小生成树中的边 枚举删除边 判断是否还能构成最小生成树。
#include "stdio.h"
#include "string.h"
#include "queue"
#include "iostream"
#include "functional"
#include "math.h"
#include "algorithm"
using namespace std;
const int maxn = 105;
const int mod = 1000000007 ;
const int inf = 1<<30;
typedef __int64 LL;
int n,m,ans,mst;
struct node
{
int u,v,w;
}edge[maxn*maxn];
int p[maxn],path[maxn];
bool cmp( node a,node b )
{
return a.w < b.w;
}
void MakeSet()
{
ans = 0;
for( int i = 1; i <= n; i ++ )
p[i] = i;
}
int find( int x )
{
return p[x] == x?x:p[x] = find( p[x] );
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int a,b,c,cas;
scanf("%d",&cas);
while( cas -- )
{
mst = 0;
scanf("%d%d",&n,&m);
for( int i = 0; i < m; i ++ )
{
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
}
sort( edge,edge+m,cmp );
int top = 0;
MakeSet();
for( int i = 0; i < m; i ++ )
{
int x = find( edge[i].u );
int y = find( edge[i].v );
if( x != y )
{
p[x] = y;
mst += edge[i].w;
path[top++] = i;
}
}
int flag = 0,k;
for( int i = 0; i < top; i ++ )
{
k = 0;
MakeSet();
for( int j = 0; j < m; j ++ )
{
if( j != path[i] )
{
int x = find( edge[j].u );
int y = find( edge[j].v );
if( x != y )
{
p[x] = y;
ans += edge[j].w;
k ++;
}
}
}
if( k == n-1 && ans == mst )
flag = 1;
}
if( flag )
puts("Not Unique!");
else
printf("%d\n",mst);
}
return 0;
}