题目链接

题目要求的是得到k种不同的元素,(题解)将每种类型视为一个点,跑一边克鲁斯卡尔即可。

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <bitset>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define MAXN 1010100
#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll __int64
#define INF 0x7fffffff
#define cs(s) freopen(s,"r",stdin)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
int f[100001];
int find(int x){
return x==f[x]?x:f[x]=find(f[x]);
}
struct uzi
{
int a,b,w;
bool operator <(const uzi &t)const {
return w<t.w;
}
};
vector<uzi>v;
map<int,int>ma;
int a[100001],vis[100001],cnt,num;
int main(){
ios::sync_with_stdio(false);
int n,m,ka;
cin>>n>>m>>ka;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1;i<=m;i++){
int s,t,w;
cin>>s>>t>>w;
if(a[s]!=a[t])v.pb({a[s],a[t],w});
}
sort(v.begin(), v.end());
LL sum=0;
for(auto k:v){
int x=find(k.a);
int y=find(k.b);
if(x==y)continue;
f[y]=x;
sum+=k.w;
if(++cnt==ka-1)break;
}
if(cnt<ka-1)cout<<-1<<endl;
else cout<<sum<<endl;
return 0;
}

牛客寒假算法基础集训营4 G(最小生成树)的更多相关文章

  1. 牛客寒假算法基础集训营4 G Applese 的毒气炸弹

    链接:https://ac.nowcoder.com/acm/contest/330/G来源:牛客网 众所周知,Applese 是个很强的选手,它的化学一定很好. 今天他又AK了一套题觉得很无聊,于是 ...

  2. 2020牛客寒假算法基础集训营5 G街机争霸

    题目描述 哎,又是银首,要是你这个签到题少WA一发就金了 牛牛战队的队员打完比赛以后又到了日常甩锅的时间.他们心情悲伤,吃完晚饭以后,大家相约到一个街机厅去solo.牛牛和牛能进入了一个迷宫,这个迷宫 ...

  3. 2020牛客寒假算法基础集训营5 G.街机争霸 (bfs)

    https://ac.nowcoder.com/acm/problem/201961 预处理出僵尸走的路径,僵尸走的周期长度应该为2k-2,在普通的bfs基础上加上一维表示时间,从当前位置x,y和和时 ...

  4. 2020牛客寒假算法基础集训营4 G音乐鉴赏

    题目描述 作为“音乐鉴赏”课的任课老师,你的课程作为刷学分好课一直受到广泛欢迎.但这一学期,学校制定了新的标准,你的课的优秀率(分数超过90分的人数)被限制在10%以下! 为了应对这个调整,你要求所有 ...

  5. 2020牛客寒假算法基础集训营3 G.牛牛的Link Power II (树状数组维护前缀和)

    https://ac.nowcoder.com/acm/contest/3004/G 发现每个“1”对于它本身位置产生的影响贡献为0,对前面的“1”有产生贡献,对后面的"1"也产生 ...

  6. 2020牛客寒假算法基础集训营3 - G. 牛牛的Link Power II(线段树)

    题目链接:牛牛的Link Power II 题意:给你一个只含$0$和$1$的串,定义串的$Link$值为串中两个的$1$之间的距离的和,$(u,v)$和$(v,u)$被看认为是同一对,有$m$次操作 ...

  7. 2020牛客寒假算法基础集训营2 J题可以回顾回顾

    2020牛客寒假算法基础集训营2 A.做游戏 这是个签到题. #include <cstdio> #include <cstdlib> #include <cstring ...

  8. 2020牛客寒假算法基础集训营1 J题可以回顾回顾

    2020牛客寒假算法基础集训营1 这套题整体来说还是很简单的. A.honoka和格点三角形 这个题目不是很难,不过要考虑周全,面积是1,那么底边的长度可以是1也可以是2, 注意底边1和2会有重复的, ...

  9. Applese 的毒气炸弹 G 牛客寒假算法基础集训营4(图论+最小生成树)

    链接:https://ac.nowcoder.com/acm/contest/330/G来源:牛客网 Applese 的毒气炸弹 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262 ...

随机推荐

  1. windows蜜汁调音

    哈,用的蜂鸣器,我静音了这东西还放. 只能调的很垃圾,但是很好玩. #include<cstdio> #include<windows.h> using namespace s ...

  2. Django(十)COOKIE和session

    https://www.cnblogs.com/haiyan123/p/7763169.html from django.shortcuts import render,redirect # Crea ...

  3. django的RestFramework模块的源码分析

    一.APIView源码分析 查看源码的前提要知道,找函数方法必须先在自己的类中找,没有再往父类找,一层一层网上找,不能直接按ctrl点击 在我们自己定义的类中没有as_view方法的函数,所以肯定是继 ...

  4. 工具类:Colletions ,Arrays(静态导入,可变参数,强循环)

    一.Collecti 专门用来操作集合的工具类,没有构造函数,全静态方法. 常用方法: static <T extends Comparable<? super T>> voi ...

  5. Unity 着色过程

    图形API包括:OpenGL.Metal.Vulkan.Director3D. 渲染管线(图形管线)通用结构: 1.数据收集(图形的网格.纹理.材质)--------2.顶点着色器(获取图形的2D坐标 ...

  6. Android Studio 每次运行都会再下载一遍,修改

    Android Studio 每次运行都会再下载一遍 把 gradle 设置 use local gradle distribution

  7. css 中 position属性

    position属性详解 文档流 1.html中的布局方式分为三种: 标准流(顺序布局):因为html中的元素大体可以分为两大类(a:块级元素:div .H1-H6. table表格  有序级无序列表 ...

  8. opencv: flip函数的使用;

    flip函数用于图像翻转,比较方便.在opencv中有几种形式: C++: void flip(InputArray src, OutputArray dst, int flipCode) Pytho ...

  9. go build 和 go run区别

  10. spring XML配置参数替代properties文件

    xml中配置BEAN与参数 <bean id="beanXXX" class="com.benXXXX" init-method="initia ...