The Unique MST
来源:互联网 发布:淘宝达人发布完哪里查 编辑:程序博客网 时间:2024/06/16 22:21
The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 26786 Accepted: 9599
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!
Source
POJ Monthly–2004.06.27 srbga@POJ
题意:
给出一个无向图,请问该无向图的最小生成树是否唯一。若果唯一则输出最小生成树权值,否则输出:Not Unique!
分析:
(1)用prim算法判断是否唯一:当每次选取当前可取最小边时,假设最小值为min=dis[u],如果在已经确定了的顶点中还有另一个顶点到u存在一条等于min的边则说明最小生成树不唯一。(遇到了一题看题解是用求次小生成树的方式判断是否唯一,我用kruskal的方法超时了。)
(2)用kruskal算法判断:将权值相等的边做上标记,算出最小生成树后看这些标记的边是否在生成树里面,若果不在则唯一,若果有,则依次删去这些边,看再次的最小生成树的权值是否和之前的一样。一样则不唯一。
AC代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <queue>using namespace std;const int maxn=110;const int maxm=10010;struct Edge{ int u,v,w; int equal; int used; int del;};Edge E[maxm];int p[maxn];int n,m;int cmp(Edge a,Edge b){ return a.w<b.w;}int find_(int x){ return p[x]==x?x:p[x]=find_(p[x]);}int kruskal(){ int ans=0; for(int i=0;i<n;i++) p[i]=i; int num=0; for(int i=0;i<m;i++) { if(E[i].del==1)continue; int xx = find_(E[i].u); int yy = find_(E[i].v); if(xx!=yy) { p[xx]=yy; ans+=E[i].w; E[i].used=1; num++; } if(num>=n-1) break; } return ans;}int main(){ int t,u,v,w; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); E[i].u=u-1; E[i].v=v-1; E[i].w=w; E[i].equal=0; E[i].used=0; E[i].del=0; } for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { if(i==j)continue; if(E[i].w==E[j].w) E[i].equal=1; } } sort(E,E+m,cmp); bool first=true; int w1=kruskal(),w2; first = false; int i; for(i=0;i<m;i++) { if(E[i].used && E[i].equal) { E[i].del=1; w2=kruskal(); if(w1==w2) { printf("Not Unique!\n"); break; } E[i].del=0; } } if(i>=m) printf("%d\n",w1); } return 0;}
- The Unique MST
- pku1679 The Unique MST
- PKU1679 the unique mst
- POJ1679:The Unique MST
- The Unique MST
- The Unique MST(P1679)
- poj_1679 The Unique MST
- POJ1679 The Unique MST
- 【hdu】 The Unique MST
- poj1679 - The Unique MST
- POJ1679 The Unique MST
- poj The Unique MST
- poj1679 The Unique MST
- The Unique MST
- poj1679 The Unique MST
- The Unique MST
- The Unique MST--hdoj
- The Unique MST
- spring IOC容器接口体系简介
- 【一天一道LeetCode】#326. Power of Three
- Spring学习笔记四: 从Spring容器中获取Bean
- 【一天一道LeetCode】#231. Power of Two
- 根据表结构自动生成JavaBean,史上最强最专业的表结构转JavaBean的工具(第4版)
- The Unique MST
- 【一天一道LeetCode】#191. Number of 1 Bits
- 使用ob缓冲技术实现页面真静态
- hadoop学习笔记—基础篇
- leetcode:Repeated DNA Sequences
- Binary Tree Level Order Traversal II
- Java学习笔记之多态
- BZOJ2802——[Poi2012]Warehouse Store
- 对无限图片轮播器的封装,一句话代码设置轮播器的属性