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! 题意:t组测试数据,n个点,m条边,求最小生成树是否唯一。唯一则输出最小生成树的边劝和,否则输出Not Unque!。 没打过次小生成树,然后乱搞一通,似乎数据很水,莫名其妙的过掉了?!!
代码:
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=100+10,maxm=maxn*maxn;
int T,n,m,ans,mi[12]; int aa;char cc;
int read() {
aa=0;cc=getchar();
while(cc<'0'||cc>'9') cc=getchar();
while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
return aa;
} struct Line{
int x,y,z;bool usd;
}li[maxm]; bool cmp(const Line& a,const Line& b) {return a.z<b.z;} int fa[maxn][12];
int find(int x) {return fa[x][0]==x? x:fa[x][0]=find(fa[x][0]);} int fir[maxn],nxt[2*maxn],to[2*maxn],e=0,v[2*maxn];
void add(int x,int y,int z) {
to[++e]=y;nxt[e]=fir[x];fir[x]=e;v[e]=z;
to[++e]=x;nxt[e]=fir[y];fir[y]=e;v[e]=z;
} void Kr() {
int tot=0,xx,yy; ans=0;
for(int i=1;i<=n;++i) fa[i][0]=i;
for(int i=1;i<=m&&tot<n;++i) {
xx=find(li[i].x);yy=find(li[i].y);
if(xx==yy) continue;
fa[xx][0]=yy; ans+=li[i].z;
li[i].usd=1; tot++;
add(li[i].x,li[i].y,li[i].z);
}
memset(fa,0,sizeof(fa));
} int d[maxn],f[maxn][12];
void dfs(int pos,int dep) {
d[pos]=dep;int y,z;
for(y=fir[pos];y;y=nxt[y]) {
if((z=to[y])==fa[pos][0]) continue;
f[z][0]=v[y]; fa[z][0]=pos;
dfs(z,dep+1);
}
} bool work(int x,int y,int z) {
if(d[x]!=d[y]) {
if(d[x]<d[y]) swap(x,y);
int cha=d[x]-d[y];
for(int i=10;i>=0&&cha;--i) if(cha>=mi[i]) {
if(f[x][i]==z) return 1;
cha-=mi[i];x=fa[x][i];
}
}
if(x==y) return 0;
for(int i=10;i>=0;--i) if(fa[x][i]!=fa[y][i]) {
if(f[x][i]==z||f[y][i]==z) return 1;
x=fa[x][i];y=fa[y][i];
}
if(f[x][1]==z||f[y][1]==z) return 1;
return 0;
} int main() {
T=read(); mi[0]=1; bool ok;
for(int i=1;i<=10;++i) mi[i]=mi[i-1]*2;
while(T--) {
n=read();m=read();ok=0;e=0;
memset(fir,0,sizeof(fir));
memset(f,0,sizeof(f));
for(int i=1;i<=m;++i) {
li[i].x=read();
li[i].y=read();
li[i].z=read();
li[i].usd=0;
}
sort(li+1,li+m+1,cmp);
Kr(); dfs(1,1);f[1][0]=1e9;
for(int i=1;i<=10;++i) for(int j=1;j<=n;++j) {
fa[j][i]=fa[fa[j][i-1]][i-1];
f[j][i]=max(f[j][i-1],f[fa[j][i-1]][i-1]);
}
for(int i=1;i<=m;++i) if(!li[i].usd&&work(li[i].x,li[i].y,li[i].z)) {
printf("Not Unique!\n");
ok=1; break;
}
if(!ok) printf("%d\n",ans);
}
return 0;
}

  

 
 

POJ 1679The Unique MST的更多相关文章

  1. POJ - 1679_The Unique MST

    The Unique MST Time Limit: 1000MS Memory Limit: 10000K Description Given a connected undirected grap ...

  2. poj 1679 The Unique MST

    题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...

  3. POJ 1679 The Unique MST (最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 D ...

  4. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  5. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  6. poj 1679 The Unique MST (判定最小生成树是否唯一)

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  7. POJ 1679 The Unique MST (最小生成树)

    The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...

  8. poj 1679 The Unique MST【次小生成树】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24034   Accepted: 8535 D ...

  9. POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 D ...

随机推荐

  1. PAT甲级——A1023 Have Fun with Numbers

    Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...

  2. DVWA 之low级别sql注入

    将Security level设为low,在左侧列表中选择“SQL Injection”,然后在右侧的“User ID”文本框中输入不同的数字就会显示相应的用户信息. 我们首先需要判断这里所传输的参数 ...

  3. [转载] DDK中VPORT Mini-Driver的使用说明

    学习下. 原文地址:DDK中VPORT Mini-Driver的使用说明作者:跳皮筋的小老鼠 要使用TI DDK中实现的VPORT驱动程序,首先需要在程序中提供VPORT_PortParams类型的参 ...

  4. hashMap 源码解读理解实现原理和hash冲突

    hashMap 怎么说呢. 我的理解是 外表是一个set 数组,无序不重复 . 每个set元素是一个bean ,存着一对key value 看看代码吧 package test; import jav ...

  5. ubuntn右上角小键盘消失及fictx切换输入法快捷键

    Ubuntu任务栏右上角的小键盘消失,打开系统设置-文本输入-左下角将当前输入法显示在任务栏. 切换输入法快捷键,打开系统设置 > 文件输入 >切换到下一个源(上一个源)的快捷键设置一个不 ...

  6. TZ_16_Vue的v-for、v-if、v-show、v-bind、watch

    1.v-for 遍历数据渲染页面是非常常用的需求,Vue中通过v-for指令来实现. 1>遍历一个users数组 <!-- ve-for --> <ul> <li ...

  7. npm常用命令及版本号

    npm 包管理器的常用命令 测试环境为node>=8.1.3&&npm>=5.0.3 1, 首先是安装命令 //全局安装 npm install 模块名 -g //本地安装 ...

  8. 容斥原理学习(Hdu 4135,Hdu 1796)

    题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. Appium_Python_Client介绍

    一.Appium_Python_Client介绍 Appium的实用方法都藏在Client的源码里,对于driver和webelement实例,均有对应的元素查找方法(webelement查找的是下面 ...

  10. 安装 Composer

    参考百度经验:http://jingyan.baidu.com/article/4f34706ed04013e386b56d72.html Composer下载:https://getcomposer ...