Day5 - G - The Unique MST POJ - 1679
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
Output
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! 思路:找次小生成树,如果权值相等则不唯一,用kruskal实现次小生成树
const int maxm = ;
const int maxn = ; struct edge {
int u, v, w;
edge(int _u=-, int _v=-, int _w=):u(_u), v(_v), w(_w){}
bool operator<(const edge &a) const {
return w < a.w;
}
};
vector<edge> Edge; int fa[maxm], T, N, M, tree[maxn], k; void init() {
Edge.clear();
for(int i = ; i <= N; ++i)
fa[i] = i;
k = ;
} int Find(int x) {
if(fa[x] == x)
return x;
return fa[x] = Find(fa[x]);
} void Union(int x, int y) {
x = Find(x), y = Find(y);
if(x != y) fa[x] = y;
} int main() {
scanf("%d", &T);
while(T--) {
int t1, t2, t3, u, v;
scanf("%d%d", &N, &M);
init();
int sum = ;
for(int i = ; i < M; ++i) {
scanf("%d%d%d", &t1, &t2, &t3);
Edge.push_back(edge(t1, t2, t3));
}
sort(Edge.begin(), Edge.end());
bool flag = true;
for(int i = ; i < M; ++i) {
u = Edge[i].u, v = Edge[i].v;
u = Find(u), v = Find(v);
if(u != v) {
sum += Edge[i].w;
Union(u,v);
tree[k++] = i;
}
}
for(int i = ; i < k; ++i) {
int cnt = , edgenum = ;
for(int t = ; t <= N; ++t)
fa[t] = t;
for(int j = ; j < M; ++j) {
if(j == tree[i]) continue;
u = Edge[j].u, v = Edge[j].v;
u = Find(u), v = Find(v);
if(u != v) {
cnt += Edge[j].w;
edgenum++;
Union(u,v);
}
}
if(cnt == sum && edgenum == N - ) {
flag = false;
break;
}
}
if(flag)
printf("%d\n", sum);
else printf("Not Unique!\n");
}
return ;
}
次小生成树博客:https://www.cnblogs.com/bianjunting/p/10829212.html
https://blog.csdn.net/niushuai666/article/details/6925258
注:这里的Max数组是记录从i到j节点中边权最大值(不是和),从其父节点与新连接的边中比较
Day5 - G - The Unique MST POJ - 1679的更多相关文章
- (最小生成树 次小生成树)The Unique MST -- POJ -- 1679
链接: http://poj.org/problem?id=1679 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#probl ...
- The Unique MST POJ - 1679 (次小生成树)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- K - The Unique MST - poj 1679
题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...
- The Unique MST POJ - 1679 次小生成树prim
求次小生成树思路: 先把最小生成树求出来 用一个Max[i][j] 数组把 i点到j 点的道路中 权值最大的那个记录下来 used数组记录该条边有没有被最小生成树使用过 把没有使用过的一条边加 ...
- The Unique MST POJ - 1679 最小生成树判重
题意:求一个无向图的最小生成树,如果有多个最优解,输出"Not Unique!" 题解: 考虑kruskal碰到权值相同的边: 假设点3通过边(1,3)连入当前所维护的并查集s. ...
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- POJ 1679 The Unique MST(判断最小生成树是否唯一)
题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...
- poj 1679 The Unique MST (判定最小生成树是否唯一)
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
随机推荐
- ubuntu 18.04 上安装 docker
命令安装 docker 1.直接从 ubuntu 仓库安装,打开终端,输入: 2.启动 docker 服务 . 设置开机自启动 docker 服务 3.免 sudo 配置:
- 文件目录T位
场景: 共享目录设置T标志 可以看别人的文件,但不可以删除.修改别人的文件 除非是ROOT,目录的拥有者
- 七 Struts2访问Servlet的API方式二:原生方式
Struts2访问Servlet的API方式二:原生方式 和解耦合的方式不同,原生方式既可以拿到域对象,也可以调用域对象中的方法 前端jsp: <%@ page language="j ...
- CPD
CPD,Cost per day的缩写,意思是按天收费,是一种广告合作方式.在实际的广告合作中根据行业不同还包括Cost per Download的缩写含义,意思是依据实际下载量收费.
- SpringBoot nohup启动
#!/bin/sh nohup java -jar /data/wwwroot/xxx.jar > /data/wwwlogs/xxx.log >&
- Python作业篇 day03
###一.有变量name = 'aleX leNb',完成如下的操作 name = 'aleX leNb' name1 = ' aleX leNb ' #1.移除name1 变量对应的值两边的空格 , ...
- 【剑指Offer面试编程题】题目1386:旋转数组的最小数字--九度OJ
题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转 ...
- JS enter键一键登录
$("body").keydown(function (event) { ) { //enter键键值为13 $('.finish-btn').click(); // $('.fi ...
- Vue - 引入本地图片的两种方式
第一种,只引入单个图片,这种引入方法在异步中引入则会报错. 比如需要遍历出很多图片展示时 <image :src = require('图片的路径') /> 第二种,可引入多个图片,也可引 ...
- 一个前端博主的nginx+php+mysql的环境搭建
这几天天某的公司给了在下一个需求,让我修改一个后端大佬用PHP写的一个官网,虽然说修改的内容还是很简单,但是毕竟之前还是没接触过PHP,于是开始了漫长的爬坑之旅,话不多说,这次就给大家介绍一下我配置安 ...