poj1679(prim或者krusal的变形)
来源:互联网 发布:电脑管家for mac 编辑:程序博客网 时间:2024/06/03 23:38
The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 29778 Accepted: 10660
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'.
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
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
Sample Output
3Not Unique!
Source
POJ Monthly--2004.06.27 srbga@POJ
判断最小生成树的唯一性 用删边来判断 具体看注释
#include <bits/stdc++.h>#define mod 1000000007#define inf 0x3f3f3f3f#define pi acos(-1.0)using namespace std;typedef long long ll;const int N=11000;const int M=15005;int n,m,cnt;int parent[N];bool flag;struct man { int u,v,w; int eq,used,del;} edg[N];bool cmp(man g,man h) {return g.w<h.w;}void init() { for(int i=0; i<=10005; i++) { parent[i]=i; }}int Find(int x) { if(parent[x] != x) parent[x] = Find(parent[x]); return parent[x];}//查找并返回节点x所属集合的根节点void Union(int x,int y) { x = Find(x); y = Find(y); if(x == y) return; parent[y] = x;}//将两个不同集合的元素进行合并int Kruskal() { init(); int sum=0; int num=0; for(int i=0;i<m;i++){ if(edg[i].del==1)continue; int u=edg[i].u;int v=edg[i].v;int w=edg[i].w; if(Find(u)!=Find(v)){ sum+=w; if(!flag)edg[i].used=1;//标记这条边有纳入生成树中 num++; Union(u,v); } if(num>=n-1)break; } return sum;}int main() { int t,d; cin>>t; while(t--) { cnt=0; cin>>n>>m; for(int i=0; i<m; i++) { cin>>edg[i].u>>edg[i].v>>edg[i].w; edg[i].del=0; edg[i].used=0; edg[i].eq=0;//一开始这个地方eq没有初始化,WA了好几发,操 } for(int i=0;i<m;i++){ for(int j=0;j<m;j++){ if(i==j)continue; if(edg[i].w==edg[j].w)edg[i].eq=1;//把有相等的边标记 } } sort(edg,edg+m,cmp); flag=false; cnt=Kruskal(); flag=true; bool gg=false; for(int i=0;i<m;i++){ if(edg[i].used==1&&edg[i].eq==1){ //判断 如果这条边有在生成树中且有其他边与之相等 edg[i].del=1;//删除这条边 int s=Kruskal();//printf("%d %d\n",i,s); if(s==cnt){ gg=true; printf("Not Unique!\n"); break; } edg[i].del=0; //取消删除标记 } } if(!gg)cout<<cnt<<endl; } return 0;}
0 0
- poj1679(prim或者krusal的变形)
- krusal算法正确性的证明
- hdu4081(最小生成树变形,prim)
- ZOJ 3204 Connect them(最小生成树之Krusal 输出字典序最小的)
- poj1679
- poj1679
- poj1679
- poj1679
- poj1679
- POJ1679
- POJ1679
- POJ1679
- hdu4081 最小树+DFS或者次小树的变形
- hdu 1301 Jungle Roads &&poj 1251 (prim 或者kruskal)
- poj1679 判断MST是否是唯一的
- 【POJ 2831】 Can We Build This One?(prim 最小生成树变形)
- prim算法矩阵之变形----引水工程
- 次小生成树(poj1679)
- 第二章 继承(二)
- 二叉树的最大节点
- 文章标题
- cordova-plugin-file 实例
- OOP/第八章 JDBC
- poj1679(prim或者krusal的变形)
- Distributed System: 常见的分布式计算工具以及MapReduce详解
- 二叉树搜索 递归
- 476 Number Complement
- CodeBlocks+CMake+OpenCV+64位Win7
- sdoi r1前的注意事项
- Ubuntu 下安装Source Insight
- (转)Aladdin, 领先的投资管理整合平台
- Javaoop——抽象和封装/继承