poj1679 The Unique MST(次小生成树)
2016-02-01 15:11
141 查看
题意:给你一个n个点m条边的无向图,问你该图的最小生成树是否唯一?如果唯一输出,树的权值,否则输出'Not Unique!'.
思路:本题求该无向图的次小生成树的权值是否等于最小生成树的权值,一个图的次小生成树的权值<=最小生成树的权值,至少有一条边与最小生成树不同,所以可以枚举最小生成树上的边,删除之后用剩下的边重新求最小生成树,求出新生成的最小生成树中权值最小就是为次小生成树的权值
Trick:删除一条边之后原图可能不连通,返回-1,注意考虑清楚
题目
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
Sample Output
思路:本题求该无向图的次小生成树的权值是否等于最小生成树的权值,一个图的次小生成树的权值<=最小生成树的权值,至少有一条边与最小生成树不同,所以可以枚举最小生成树上的边,删除之后用剩下的边重新求最小生成树,求出新生成的最小生成树中权值最小就是为次小生成树的权值
Trick:删除一条边之后原图可能不连通,返回-1,注意考虑清楚
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #include <vector> #include <map> #include <string> #include <set> #include <ctime> #include <cmath> #include <cctype> using namespace std; #define maxn 110 const int maxm = 100*100+10; #define LL long long int cas=1,T; struct Edge { int u,v,dist; int id; //边的编号 Edge(){} Edge(int u,int v,int dist,int id):u(u),v(v),dist(dist),id(id){} bool operator < (const Edge&rhs)const { return dist < rhs.dist; } }; int n,m; Edge edges[maxm]; int pre[maxn]; vector <int> E; //保存最小生成树的边的编号 int Find(int x) { return pre[x]==-1?x:pre[x]=Find(pre[x]); } void init() { m=0; memset(pre,-1,sizeof(pre)); } void AddEdge(int u,int v,int dist,int id) { edges[m++]=Edge(u,v,dist,id); } int Kruskal(int ID) { memset(pre,-1,sizeof(pre)); E.clear(); int sum = 0; int cnt = 0; sort(edges,edges+m); for (int i = 0;i<m;i++) { if (edges[i].id == ID) continue; int u = edges[i].u; int v = edges[i].v; if (Find(u)!=Find(v)) { E.push_back(edges[i].id); pre[Find(u)]=Find(v); sum+=edges[i].dist; if (++cnt >=n-1) break; } } if (cnt < n-1) return -1; return sum; } int main() { //freopen("in","r",stdin); scanf("%d",&T); while (T--) { int mm; scanf("%d%d",&n,&mm); init(); for (int i = 0;i<mm;i++) { int u,v,d; scanf("%d%d%d",&u,&v,&d); AddEdge(u,v,d,i); } int ans1 = Kruskal(-1); int ans2 = 1<<20; vector <int>EE(E); for (int i = 0;i<EE.size();i++) { int temp = Kruskal(EE[i]); if (temp == -1) continue; ans2 = min(ans2,temp); if (ans2 == ans1) break; } if (ans1 == ans2) puts("Not Unique!"); else printf("%d\n",ans1); } //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC); return 0; }
题目
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!
相关文章推荐
- iOS编程——经过UUID和KeyChain来代替Mac地址实现iOS设备的唯一标示(OC版)
- 波浪交互设计与技术实现
- 马赛克滤镜效果
- MediaCodec 编码H264 编码一帧后dequeueOutputBuffer为-1的问题
- hdu Crazy Circuits
- 使用SiteMesh和EasyUI创建Web网站
- ANDROID UI 抽屉效果
- 系统架构调整(一) Druid整改
- 关于 Bitcode (Xcode build setting)
- 303. Range Sum Query - Immutable
- iOS中的UIKIT_EXTERN
- Divide and conquer:4 Values whose Sum is 0(POJ 2785)
- String、StringBuffer和StringBuilder区别
- Build glibc-2.6
- java中queue的使用
- STL之优先队列priority_queue浅析
- c#使用FluentNHibernate,多数据库连接,一个程序,连接多个数据库
- UIView的setNeedLayout layoutIfNeeded LayoutSubviews
- poj2031 Building a Space Station(最小生成树)
- iOS--UITabbarController自定义