题目链接:http://poj.org/problem?id=2377

于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次。

 /*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lrt rt << 1
#define rrt rt << 1 | 1
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; typedef struct Edge {
int u, v, w;
Edge() {}
Edge(int uu, int vv, int ww) : u(uu), v(vv), w(ww) {}
}Edge;
const int maxn = ;
const int maxm = ;
int n, m;
int pre[maxn];
Edge edge[maxm]; bool cmp(Edge a, Edge b) {
RT a.w > b.w;
} int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
} int unite(int x, int y) {
int fx = find(x);
int fy = find(y);
if(fx != fy) {
pre[fy] = fx;
return ;
}
return ;
} int main() {
// FRead();
int u, v, c;
while(~Rint(n) && ~Rint(m)) {
Rep(i, n+) pre[i] = i;
Rep(i, m) {
Rint(u); Rint(v); Rint(c);
edge[i] = Edge(u, v, c);
}
sort(edge, edge+m, cmp);
int ret = ;
Rep(i, m) {
if(unite(edge[i].u, edge[i].v)) {
ret += edge[i].w;
}
}
int flag = ;
For(i, , n+) {
if(pre[i] == i) flag++;
if(flag > ) break;
}
if(flag > ) printf("-1\n");
else printf("%d\n", ret);
}
RT ;
}

[POJ2377]Bad Cowtractors(最大生成树,Kruskal)的更多相关文章

  1. TZOJ 3710 修路问题(最小差值生成树kruskal或者LCT)

    描述 xxx国“山头乡”有n个村子,政府准备修建乡村公路,由于地形复杂,有些乡村之间可能无法修筑公路,因此政府经过仔细的考察,终于得到了所有可能的修路费用数据.并将其公布于众,广泛征求村民的修路意见. ...

  2. #图# #最大生成树# #kruskal# ----- OpenJudge 799:Heavy Transportation

    OpenJudge 799:Heavy Transportation 总时间限制: 3000ms 内存限制: 65536kB 描述BackgroundHugo Heavy is happy. Afte ...

  3. POJ - 2031 Building a Space Station 三维球点生成树Kruskal

    Building a Space Station You are a member of the space station engineering team, and are assigned a ...

  4. poj2377 Bad Cowtractors

    思路: 最大生成树. 实现: #include <iostream> #include <cstdio> #include <vector> #include &l ...

  5. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

  6. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  7. NOIP2017 考前汇总

    时隔一年,相比去年一无所知的自己,学到了不少东西,虽然还是很弱,但也颇有收获[学会了打板QAQ] 现在是2017.11.9   21:10,NOIP2017的前两天晚上,明天就要出发,做最后的总结 N ...

  8. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  9. OI题目类型总结整理

    ## 本蒟蒻的小整理qwq--持续更新(咕咕咕) 数据结构 数据结构 知识点梳理 数据结构--线段树 推荐yyb dalao的总结--戳我 以后维护线段树还是把l,r写到struct里面吧,也别写le ...

随机推荐

  1. ServiceStack.OrmLite 调用存储过程

    最近在做关于ServiceStack.OrmLite调用存储过程时,有问题.发现ServiceStack.OrmLite不能调用存储过程,或者说不能实现我想要的需求.在做分页查询时,我需要传入参数传出 ...

  2. Team Homework #1 学长“学霸英语学习软件”试用

    简介: 一款英语单词记忆和管理辅助软件. 基本功能: 内置GRE词汇及其常考形态.Webster英语解释 单词发音功能 单词测验模式 简易词典功能 基本界面 词库单词读取 单词测试 优点: 1.界面简 ...

  3. Document.defaultView

    Document.defaultView 引子 最近愚安在写一个可以将页面上的资源链接转为二维码以方便移动端浏览的chrome插件,由于dom操作并不多,而且作为插件不需要考虑跨 浏览器兼容性,所以并 ...

  4. ios 开发常用快捷键

    CTRL + K 删除一行,尽量在行首处使用: CMD+ /  注释,取消注释 CMD + R  运行 CMD + . 停止运行 CMD + F   普通搜索 CMD + CTRL + ↑/↓ 切换头 ...

  5. HDU 3974 Assign the task 暴力/线段树

    题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  6. struts2 ,web.xml中配置为/*.action,运行报错Invalid <url-pattern> /*.action in filter mapp

    首先,修改成: <filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/ ...

  7. jQuery+AJAX实现网页无刷新上传

    新年礼,提供简单.易套用的 jQuery AJAX上传示例及代码下载.后台对文件的上传及检查,以 C#/.NET Handler 处理 (可视需要改写成 Java 或 PHP). 有时做一个网站项目 ...

  8. 【BZOJ】【3196】Tyvj 1730 二逼平衡树

    树套树 Orz zyf 学(co)习(py)了一下树套树的写法,嗯……就是线段树套平衡树. 具体实现思路就是:外部查询用的都是线段树,查询内部自己调用平衡树的操作. 抄抄代码有助理解= = 八中挂了… ...

  9. 【BZOJ】【2132】圈地计划

    网络流/最小割 Orz Hzwer 这类大概是最小割建模中的经典应用吧…… 黑白染色,然后反转黑色的技巧感觉很巧妙!这个转化太神奇了…… /****************************** ...

  10. struts2多文件上传(带进度条)demo+说明

    利用plupload插件实现多文件上传,实现图片: 在jsp写入js代码: z<%@ page language="java" contentType="text/ ...