The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23942   Accepted: 8492

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!

题意:问最小生成树是否唯一。

分析:求次小生成树,推断次小生成树和最小生成树是否相等。留作模板。

次小生成树的步骤:

(1)先用Prime求出最小生成树T,在Prime的同一时候用一个矩阵max_edge[u][v]记录在T中连接随意两点u,v的唯一路径中权

值最大的那条边的权值。注意这里是非常easy做到的。由于Prime是每次添加一个节点t。而设已经标了号的节点集合为S,则S

中全部节点到t的路径中最大权值的边就是当前增加的这条边。

(2)枚举最小生成树以外的边,并删除该边所在环上权值最大的边。

(3)取得的全部生成树中权值最小的一棵即为所求。

算法的时间复杂度为O(n^2)。

题目链接:

id=1679">http://poj.org/problem?id=1679

代码清单:

#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<string>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull; const int maxn = 100 + 5;
const int maxv = 10000 + 5;
const int max_dis = 1e9 + 5; int T;
int n,m;
int a,b,c;
int MST,_MST;
bool vis[maxn];
int father[maxn];
int dist[maxn];
int graph[maxn][maxn];
bool used[maxn][maxn];
int max_edge[maxn][maxn]; void init(){
memset(vis,false,sizeof(vis));
memset(used,false,sizeof(used));
memset(max_edge,-1,sizeof(max_edge));
memset(graph,0x7f,sizeof(graph));
} void input(){
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
graph[a][b]=graph[b][a]=c;
used[a][b]=used[b][a]=true;
}
} int prim(){
int ans=0;
dist[1]=0;
vis[1]=true;
father[1]=-1;
for(int i=2;i<=n;i++){
father[i]=1;
dist[i]=graph[1][i];
}
for(int i=1;i<n;i++){
int v=-1;
for(int j=1;j<=n;j++){
if(!vis[j]&&(v==-1||dist[j]<dist[v])) v=j;
}
ans+=dist[v];
vis[v]=true;
used[father[v]][v]=used[v][father[v]]=false;
for(int j=1;j<=n;j++){
if(vis[j]){
max_edge[v][j]=max_edge[j][v]=max(max_edge[father[v]][j],dist[v]);
}
else{
if(graph[v][j]<dist[j]){
dist[j]=graph[v][j];
father[j]=v;
}
}
}
}return ans;
} int second_prim(){
int ans=max_dis;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(used[i][j]) ans=min(ans,MST+graph[i][j]-max_edge[i][j]);
return ans;
} void solve(){
MST=prim();
_MST=second_prim();
if(MST==_MST) printf("Not Unique!\n");
else printf("%d\n",MST);
} int main(){
scanf("%d",&T);
while(T--){
init();
input();
solve();
}return 0;
}



POJ_1679_The Unique MST(次小生成树模板)的更多相关文章

  1. poj1679The Unique MST(次小生成树模板)

    次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...

  2. POJ_1679_The Unique MST(次小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  3. POJ-1679 The Unique MST,次小生成树模板题

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

  4. POJ1679 The Unique MST —— 次小生成树

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

  5. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  6. POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)

    题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...

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

    题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...

  8. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

  9. poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 35999   Accepted: 13145 ...

随机推荐

  1. 创建maven项目遇到的问题

    1.新建完成的maven项目,缺少src/main/java 解决方案:把项目中的jre换成eclipse中默认的jre. 另外还可以参考:解决Eclipse建立Maven项目后无法建立src/mai ...

  2. 解决:未能加载文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

    今天发布web API,调用接口报错了:未能加载文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31b ...

  3. Apache JServ protocol服务 怎么关闭?

      Apache JServ protocol  =  AJP 解决方案:修改tomcat 的service.xml配置文件 将 <Connector port="8009" ...

  4. 最优化方法系列:Adam+SGD—>AMSGrad

    自动调参的Adam方法已经非常给力了,不过这主要流行于工程界,在大多数科学实验室中,模型调参依然使用了传统的SGD方法,在SGD基础上增加各类学习率的主动控制,以达到对复杂模型的精细调参,以达到刷出最 ...

  5. nginx_location用法总结

    location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配 ...

  6. eBPF监控工具bcc系列一启航

    eBPF监控工具bcc系列一启航 在eBPF篇中,我们知道虽然可用 C 来实现 BPF,但编译出来的却仍然是 ELF 文件,开发者需要手动析出真正可以注入内核的代码.工作有些麻烦,于是就有人设计了 B ...

  7. block相关归纳

    经过今天的Block的学习.上网查询相关文章归纳了一下 一.一个使用Block的好处有: Block可以用在许多不同的环境中,这样可以让代码更加简单,以及减少函数声明的数量,不用实现代理协议. 简单性 ...

  8. 诊断:expdp导出时遇到错误ORA-31693和ORA-00922

    11.2.0.1使用数据泵expdp导出时,如果使用parallel,可能会遇到 ORA-: Table data object "OWNER"."TABLE" ...

  9. ubuntu安装nvidia驱动以及cuda教程

    最近尝试在ubuntu中安装nvidia的显卡驱动以及cuda.花了近三天时间,真的如网上所说错误百出,期间甚至重装了一次ubuntu系统,搞到怀疑人生,整个都是泪- -.最终经过百般“磨难”总算安装 ...

  10. [luogu4056 JSOI2009] 火星藏宝图 (贪心 dp)

    传送门 Solution 一个显然的贪心:选的点数越多越好.这个随便推推就知道了. 那么我们就贪心的从一列上挑最靠下的转移 直接转移不斜率优化复杂度\(O(nm)\),吸一口O2过了... Code ...