Description
Input
Output
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!
Source
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int maxn=105;
int father[maxn];
struct node
{
int from,to;
int weight;
}edge[maxn*maxn];
bool used[maxn][maxn];
void unit(int n)
{
for(int i=0;i<=n;i++)
father[i]=i;
}
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
int cmp(node a,node b)
{
return a.weight < b.weight;
}
int kruskal(int n,int m)
{
unit(n);
sort(edge,edge+m,cmp);
memset(used,0,sizeof(used));
int sum=0;
int cnt=0;
for(int i=0;i<m;i++)
{
int a=find(edge[i].from);
int b=find(edge[i].to);
if(a!=b)
{
father[a]=b;
used[edge[i].from][edge[i].to]=1;
used[edge[i].to][edge[i].from]=1;
sum+=edge[i].weight;
cnt++;
if(cnt==n-1)
break;
}
}
return sum;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].weight);
int sum=kruskal(n,m),ans=0x3f3f3f3f;
for(int i=0;i<m;i++)
{
if(!used[edge[i].from][edge[i].to] && !used[edge[i].to][edge[i].from])
{
for(int j=0;j<m;j++)
{
if(used[edge[j].from][edge[j].to] || used[edge[j].to][edge[j].from])
ans=min(ans,sum-edge[j].weight+edge[i].weight);
}
}
}
if(ans==sum)
printf("Not Unique!\n");
else
printf("%d\n",sum);
}
return 0;
}