poj1679 次最小生成树 kruskal(暴力枚举)
Description
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! 这题直接暴力枚举,找到一颗最小生成树,标记一下,依次删边
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <string>
#include <math.h>
using namespace std;
const int maxn = ;
const int INF = 0x7fffffff;
struct node {
int u, v, w;
int used, num, flag;
} qu[ * maxn];
int fa[maxn], n, m;
int cmp(node a, node b) {
return a.w < b.w;
}
void init() {
for (int i = ; i <= n ; i++) fa[i] = i;
}
int Find(int x) {
return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
int combine(int x, int y) {
int nx = Find(x);
int ny = Find(y);
if (nx != ny) {
fa[nx] = ny;
return ;
}
return ;
}
int kruskal(int flag) {
init();
int sum = , cnt = ;
for (int i = ; i < m ; i++) {
if (qu[i].flag) continue;
if (combine(qu[i].v, qu[i].u)) {
if (!flag) qu[i].used = ;
sum += qu[i].w;
cnt++;
if (cnt == n - ) break;
}
}
if (cnt != n - ) return -;
return sum;
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
for (int i = ; i < m ; i++) {
scanf("%d%d%d", &qu[i].u, &qu[i].v, &qu[i].w);
qu[i].flag = , qu[i].used = ;
}
sort(qu, qu + m, cmp);
int sum = kruskal();
int flag = ;
for (int i = ; i < m ; i++) {
if (qu[i].used ) {
qu[i].flag = ;
int temp = kruskal();
qu[i].flag = ;
if (temp == sum) {
flag = ;
break;
}
}
}
if (flag) printf("Not Unique!\n");
else printf("%d\n", sum);
}
return ;
}
poj1679 次最小生成树 kruskal(暴力枚举)的更多相关文章
- poj1679(最小生成树)
传送门:The Unique MST 题意:判断最小生成树是否唯一. 分析:先求出原图的最小生成树,然后枚举删掉最小生成树的边,重做kruskal,看新的值和原值是否一样,一样的话最小生成树不唯一. ...
- CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)
题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...
- 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)
/* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...
- HNU 12886 Cracking the Safe(暴力枚举)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...
- 51nod 1116 K进制下的大数 (暴力枚举)
题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...
- Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举
题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
- bzoj 1028 暴力枚举判断
昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...
- POJ-3187 Backward Digit Sums (暴力枚举)
http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...
- hihoCoder #1179 : 永恒游戏 (暴力枚举)
题意: 给出一个有n个点的无向图,每个点上有石头数个,现在的游戏规则是,设置某个点A的度数为d,如果A点的石子数大于等于d,则可以从A点给每个邻接点发一个石子.如果游戏可以玩10万次以上,输出INF, ...
随机推荐
- [异常笔记] spring cloud 服务消费者启动-2018040501
一.异常信息: Error starting ApplicationContext. To display the auto-configuration report re-run your appl ...
- python安装教程(面向对象的解释型计算机程序设计语言)
inux下默认自带的.包括mac,是python2.x 但咱们玩的最新的 python3.x 怎么办呢centos下安装方法: sudo yum install epel- sudo yum i ...
- 百度MIP校验错误整理与解决方法
MIP校验工具地址: https://www.mipengine.org/validator/validate 1.强制性标签缺失或错误 错误提示:line 1,col 1: 强制性标签'<sc ...
- PAT (Basic Level) Practice 1021 个位数统计
个人练习 给定一个 k 位整数 N=dk−110k−1+⋯+d1101+d0 (0≤di≤9, i=0,⋯,k−1, dk−1>0),请编写程序统计每种 ...
- POJ:3061-Subsequence(尺取法模板详解)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18795 Accepted: 8043 Descript ...
- 12,nginx+uWSGI+django+virtualenv+supervisor发布web服务器
导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收请求,处理请求,响应请求) 基于wsgi运行的框架 ...
- EVALUation mode running with code size limit:2k keil进行仿真过程中出现的报错
EVALUation mode running with code size limit:2k 如果keil软件未破解,会限制程序的存储大小.第一是你的软件没有破解,不能编译2K以上的程序:这种情况下 ...
- 3670: [Noi2014]动物园
题目链接 题意:给n个字符串,求出每个字符串的num值,加1后相乘.num[i]表示1~i中,有多少没有重叠的公共前缀后缀. 分析: kmp中p数组表示最大的公共前缀后缀.设一cnt数组,表示1~i中 ...
- 您的手机上未安装应用程序 android 点击快捷方式提示未安装程序的解决
最近APP出现一个很奇怪的问题,在Android 4.4.2和android 4.4.3系统上点击应用的快捷方式,打不开应用,而且会提示未安装程序. 确认了应用的MainActivity中设置了and ...
- 接口&抽象类&继承&父类和子类
1.接口是什么? 接口可以理解为一个特殊的抽象类,是由全局常量和公共抽象方法组成的. 所以,如果,一个抽象类的方法都是抽象的,那么就可以通过接口的形式来表示. class 用于定义类 interfac ...