Educational Codeforces Round 33 (Rated for Div. 2) C题·(并查集变式)
C. Rumor
Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.
Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.
Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.
The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?
Take a look at the notes if you think you haven't understood the problem completely.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.
The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.
Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.
Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.
Input
Output
思路:明显的并查集。
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define N 100009
#define int long long
int cost[N];
int f[N];
int getf(int v){
if(v==f[v]){
return f[v];
}else{
f[v]=getf(f[v]);
return f[v];
}
}
void merge(int u,int v){
int t1=getf(u);
int t2=getf(v);
if(t1!=t2){
f[t2]=t1;
}
}
signed main(){
int n,m;
cin>>n>>m;
;
;
;i<=n;i++){
scanf("%lld",&cost[i]);
sb+=cost[i];
f[i]=i;
}
){
cout<<sb<<endl;
;
}
;i<m;i++){
int x,y;
scanf("%lld%lld",&x,&y);
merge(x,y);
}
/*for(int i=1;i<=n;i++){
if(i==f[i]){
cout<<i<<" ";
}
}
cout<<endl;*/
;i<=n;i++){
cost[getf(i)]=min(cost[getf(i)],cost[i]);// 关键:将该每一个集合中的最小花费赋值给祖先。
}//
;i<=n;i++){
if(i==f[i]){
ans+=cost[i];
}
}
printf("%lld\n",ans);
;
}
Educational Codeforces Round 33 (Rated for Div. 2) C题·(并查集变式)的更多相关文章
- Educational Codeforces Round 33 (Rated for Div. 2) D题 【贪心:前缀和+后缀最值好题】
D. Credit Card Recenlty Luba got a credit card and started to use it. Let's consider n consecutive d ...
- Educational Codeforces Round 33 (Rated for Div. 2) B题
B. Beautiful Divisors Recently Luba learned about a special kind of numbers that she calls beautiful ...
- Educational Codeforces Round 33 (Rated for Div. 2) A题
A. Chess For Three Alex, Bob and Carl will soon participate in a team chess tournament. Since they a ...
- Educational Codeforces Round 78 (Rated for Div. 2)D(并查集+SET)
连边的点用并查集检查是否有环,如果他们的fa是同一个点说明绕了一圈绕回去了.n个点一共能连n-1条边,如果小于n-1条边说明存在多个联通块. #define HAVE_STRUCT_TIMESPEC ...
- Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays
题目链接 题意:给你两个数x,yx,yx,y,让你构造一些长为yyy的数列,让这个数列的累乘为xxx,输出方案数. 思路:考虑对xxx进行质因数分解,设某个质因子PiP_iPi的的幂为kkk,则这个 ...
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)
题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...
- Educational Codeforces Round 33 (Rated for Div. 2) 题解
A.每个状态只有一种后续转移,判断每次转移是否都合法即可. #include <iostream> #include <cstdio> using namespace std; ...
- Educational Codeforces Round 33 (Rated for Div. 2)A-F
总的来说这套题还是很不错的,让我对主席树有了更深的了解 A:水题,模拟即可 #include<bits/stdc++.h> #define fi first #define se seco ...
- Educational Codeforces Round 33 (Rated for Div. 2) D. Credit Card
D. Credit Card time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
随机推荐
- (十六)JDBC 处理大数据
目录 前言: 基本概念 对于Mysql的Text类型 流地址的写法 blob类型数据 备注 前言: 在实际开发中,程序需要把 大文本或二进制 数据保存到数据库中: 实际上,我们并不存储大的数据到数据库 ...
- Photon Server初识(三) ---ORM映射改进
一:新建一些管理类, 二.实现每个管理类 (1)NHibernateHelper.cs 类,管理数据库连接 using NHibernate; using NHibernate.Cfg; namesp ...
- Timezone offset does not match system offset: 0 != -32400. Please, check your config files
apscheduler使用uWSGI的mule模块部署的时候报错, 因为系统时区和代码运行时区不一样导致. 解决办法:在初始化的时候指定上海的时区 scheduler = BlockingSchedu ...
- linux的定时器(timer_create,timer_gettime,timer_delete,SIGEV_SIGNAL)
ref : http://blog.chinaunix.net/uid-28458801-id-5035347.html 系统中的一个模块需要频繁的获取系统时间,使用linux中内置的函数开销过大 ...
- linux实现一个定时任务
设置定时任务删除logs脚本数据 编写脚本 touch cleanLogs.sh #! /bin/sh -name "*.log*" -exec rm -f {} \; 使用r ...
- spring-boot-plus集成Shiro+JWT权限管理
SpringBoot+Shiro+JWT权限管理 Shiro Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理. 使用Shiro的易于理解的API,您可以 ...
- Python爬取爱奇艺资源
像iqiyi这种视频网站,现在下载视频都需要下载相应的客户端.那么如何不用下载客户端,直接下载非vip视频? 选择你想要爬取的内容 该安装的程序以及运行环境都配置好 下面这段代码就是我在爱奇艺里搜素“ ...
- C# 校验车架号(VIN码)第9位是否有效算法
public static bool checkVIN(string vin) { //VIN码从第1位到第17位的“加权值”: Dictionary<int, int> vinMapWe ...
- Spring web.xml详解
web.xml文件是Java Web项目中的一个配置文件,主要用于配置欢迎页.Filter.Listener.Servlet等,但并不是必须的,一个Java Web项目没有web.xml文件也是照样能 ...
- go语言入门(8)异常处理
1,error接口 Go语言引入了一个关于错误处理的标准模式,即error接口,它是Go语言内建的接口类型,该接口的定义如下: type error interface { Error() strin ...