krustral算法加并查集,按题给要求维护并查集~

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9;
int N,M,x,y;
int c,w;
struct edge {
int u;
int v;
int w;
}Edge[maxn*];
int mst[maxn];
int num[maxn];
int father[maxn];
bool cmp (edge a,edge b) {
return a.w<b.w;
}
int findfather (int x) {
int a=x;
while (x!=father[x]) x=father[x];
while (a!=father[a]) {
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
vector<int> g[maxn];
int main () {
scanf ("%d %d %d",&N,&M,&c);
for (int i=;i<N;i++) father[i]=i,num[i]=;
for (int i=;i<M;i++) {
scanf ("%d %d %d",&Edge[i].u,&Edge[i].v,&Edge[i].w);
}
sort (Edge,Edge+M,cmp);
for (int i=;i<M;i++) {
int u=Edge[i].u;
int v=Edge[i].v;
int faU=findfather(u);
int faV=findfather(v);
if (faU==faV) continue;
if ((Edge[i].w<=mst[faU]+c/num[faU])&&(Edge[i].w<=mst[faV]+c/num[faV])) {
if (faU<faV) swap (faU,faV);
father[faU]=faV;
if (Edge[i].w>mst[faV]) mst[faV]=Edge[i].w;
num[faV]+=num[faU];
}
}
for (int i=;i<N;i++)
g[findfather(i)].push_back(i);
for (int i=;i<N;i++) {
if (g[i].size()==) continue;
sort (g[i].begin(),g[i].end());
for (int j=;j<g[i].size();j++) {
if (j!=) printf (" ");
printf ("%d",g[i][j]);
}
printf ("\n");
}
return ;
}

PAT T1013 Image Segmentation的更多相关文章

  1. PAT甲级—暴力搜索

    1091 Acute Stroke (30point(s)) 基础的搜索,但是直接用递归会导致段错误,改用队列之后就不会了,这说明递归调用在空间利用率上还是很吃亏的. #include <cst ...

  2. 《转载》PAT 习题

    博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...

  3. Caffe Python MemoryDataLayer Segmentation Fault

    转载请注明出处,楼燚(yì)航的blog,http://home.cnblogs.com/louyihang-loves-baiyan/ 因为利用Pyhon来做数据的预处理比较方便,因此在data_l ...

  4. php php-5.6.4.tar.bz2 apache 兼容问题 child pid 27858 exit signal Segmentation fault

    环境 [root envirotar]# uname -a Linux i2..el6.x86_64 # SMP Thu Jul :: UTC x86_64 x86_64 x86_64 GNU/Lin ...

  5. PAT Judge

    原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...

  6. PAT/字符串处理习题集(二)

    B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...

  7. Segmentation fault (core dumped)

    问题: 在Linux中实现快速排序时,没有加入终止条件,结果出现Segmentation fault (core dumped)这个问题,Segmentation fault (core dumped ...

  8. centos yum Segmentation fault 问题解决办法

    今儿在centos 使用yum 安装软件时出现了 ”Segmentation fault“ 错误提示,google一大把执行 yum clean all 命令后,再执行还是没用,最后把 zlib.x. ...

  9. PAT 1041. 考试座位号(15)

    每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...

随机推荐

  1. Laravel 虚拟开发环境 Homestead 密码

    默认数据库账号密码账号: homestead 密码:secret 默认 ssh 账号密码账号:vagrant 密码:vagrant创建默认 root 用户sudo passwd root

  2. MBA 报考

      1. 作者:MBA薛老师链接:https://www.zhihu.com/question/277811289/answer/397083199来源:知乎著作权归作者所有.商业转载请联系作者获得授 ...

  3. C语言中二维数组如何申请动态分配内存

    C语言中二维数组如何申请动态分配内存: 使用malloc函数,先分配第一维的大小,然后再循环分配每一维的大小 #include <stdio.h> #include <malloc. ...

  4. 瀑布流无限加载infinitescroll插件与masonry插件使用

    masonry官网地址http://masonry.desandro.com/,infinitescroll官网地址http://www.infinite-scroll.com/ 无限滚动原理:无限滚 ...

  5. Java进阶学习(1)之类与对象(上)

    package com.study; //自动售卖机 public class vmachine { private int price = 80; private int balance; priv ...

  6. scala安装此时不应有 \scala\bin\scala.bat

    scala安装目录有空格导致的,不应该有空格

  7. 吴裕雄 python 机器学习——数据预处理字典学习模型

    from sklearn.decomposition import DictionaryLearning #数据预处理字典学习DictionaryLearning模型 def test_Diction ...

  8. Mybatis空指针

    查询数据,返回的字段要在 resultMap 中定义, <resultMap id="BaseResultMap" type="com...." > ...

  9. 计算机二级-C语言-对结构体数据进行求平均值。对结构体数据进行比较处理。

    //函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),作为函数返回,并将大于平均值的数放在形参y所指数组中,在主函数中输出. //重难点:对结构体数据进行求平均值. #in ...

  10. ASP.NET Core搭建多层网站架构【15-扩展之使用Obfuscar混淆加密保护代码】

    2020/02/03, ASP.NET Core 3.1, VS2019, Obfuscar 2.2.25 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构[15-扩展之使 ...