#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <set> #include <vector> #include <map> #include <queue> #include <set> #include <algorithm> #include <limits> using namespace std; typedef long long LL; const int MAXN=1e+4; const int MAXM=1e+5; const int MAXX=15; const int INF = numeric_limits<int>::max(); const LL LL_INF= numeric_limits<LL>::max(); struct Edge { int to,next,w,id; int select; Edge(){} Edge(int _to,int _next,int _w,int _id):to(_to),next(_next),w(_w),id(_id){select=0;} }e[MAXM]; bool cmp(const Edge &x,const Edge &y){ if(x.w!=y.w)return x.w<y.w; return x.id<y.id; } int p[MAXN][MAXX],mm[MAXN][MAXX],deep[MAXN],head[MAXN],tot=0; int n,m; void init(){ memset(head,-1,sizeof(head)); memset(deep,0,sizeof(deep)); memset(mm,0,sizeof(mm)); tot=0; } void AddEdge(int u,int v,int w){ e[tot]=Edge(v,head[u],w,tot); head[u]=tot++; e[tot]=Edge(u,head[v],w,tot); head[v]=tot++; } void dfs(int u){ for(int i=head[u];~i;i=e[i].next){ int v=e[i].to; if(!deep[v]&&e[i].select){ p[v][0]=u; deep[v]=deep[u]+1; mm[v][0]=e[i].w; dfs(v); } } } void st(){ for(int j=1;(1<<j)<=n;j++) for(int i=1;i<=n;i++) if(p[i][j-1]!=-1){ p[i][j]=p[p[i][j-1]][j-1]; mm[i][j]=max(mm[i][j-1],mm[p[i][j-1]][j-1]); } } int mst_lca(int a,int b){ if(deep[a]<deep[b])swap(a,b); int x=deep[a]-deep[b],y=0,maxm=0; while(x){ if(x&1){ maxm=max(maxm,mm[a][y]); a=p[a][y]; } x>>=1; ++y; } if(a==b)return maxm; for(x=1;(1<<x)<=deep[a];x++); while(--x>=0){ if(p[a][x]!=-1&&p[a][x]!=p[b][x]){ maxm=max(maxm,mm[a][x]); a=p[a][x]; maxm=max(maxm,mm[b][x]); b=p[b][x]; } } maxm=max(mm[a][0],max(maxm,mm[b][0])); return maxm; } int path[MAXN]; int Find(int x){ return x==path[x]?x:path[x]=Find(path[x]); } int Union(int x,int y){ x=Find(x);y=Find(y); if(x==y)return false; path[x]=y; return true; } int read(){ char ch; while(!((ch=getchar())>='0'&&ch<='9')); int t=ch-'0'; while((ch=getchar())>='0'&&ch<='9'){ t=(t<<1)+(t<<3)+ch-'0'; } return t; } int main() { int t; t=read(); while(t--){ n=read();m=read(); init(); for(int i=0;i<=n;i++)path[i]=i; for(int i=0,x,y,w;i<m;i++){ x=read();y=read();w=read(); AddEdge(x,y,w); } int ans=0; sort(e,e+tot,cmp); for(int i=0;i<tot;i+=2){ if(Union(e[i].to,e[i^1].to)){ ans+=e[i].w; e[i].select=e[i^1].select=1; } } p[1][0]=-1; deep[1]=1; dfs(1); st(); int ans1=INF; for(int i=0;i<tot;i+=2){ if(!e[i].select){ ans1=min(ans1,ans-mst_lca(e[i].to,e[i^1].to)+e[i].w); } } if(ans1==ans)puts("Not Unique!"); else printf("%d\n",ans); } return 0; }
|