codeforces地址:https://codeforces.com/problemset/problem/893/C

CF893C 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 $ c_{i} $ 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.

输入格式

The first line contains two integer numbers $ n $ and $ m $ ( $ 1<=n<=10{5},0<=m<=10 $ ) — the number of characters in Overcity and the number of pairs of friends.

The second line contains $ n $ integer numbers $ c_{i} $ ( $ 0<=c_{i}<=10^{9} $ ) — the amount of gold $ i $ -th character asks to start spreading the rumor.

Then $ m $ lines follow, each containing a pair of numbers ( $ x_{i},y_{i} $ ) which represent that characters $ x_{i} $ and $ y_{i} $ are friends ( $ 1<=x_{i},y_{i}<=n $ , $ x_{i}≠y_{i} $ ). It is guaranteed that each pair is listed at most once.

输出格式

Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.

输入输出样例 #1

输入 #1

5 2
2 5 3 4 8
1 4
4 5

输出 #1

10

输入输出样例 #2

输入 #2

10 0
1 2 3 4 5 6 7 8 9 10

输出 #2

55

输入输出样例 #3

输入 #3

10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10

输出 #3

15

说明/提示

In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.

In the second example Vova has to bribe everyone.

In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.

思路

不难发现是并查集的简单应用,这题注意要进行路径压缩,否则会超时

并查集模板

vector<int> father = vector<int> (n, 0);

void init() {
for (int i = 0; i < n; ++i) {
father[i] = i;
}
} int find(int u) {
return u == father[u] ? u : find(father[u]);
} bool isSame(int u, int v) {
u = find(u);
v = find(v);
return u == v;
}
void join(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return ;
father[v] = u;
}

题解

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
int n,m;
ll c[N];
ll father[N];
void init() {
for (int i = 1; i <= n; ++i) {
father[i] = i;
}
}
int find(ll u) {
return u == father[u] ? u : father[u]=find(father[u]);#路径压缩在于father[u]=find(father[u])
}
void join(ll u, ll v) {
u = find(u);
v = find(v);
if (u == v) return ;
c[u]=min(c[u],c[v]);
c[v]=min(c[u],c[v]);
father[v] = u;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>c[i];
init();
for(int i=1;i<=m;i++)
{
ll a,b;
cin>>a>>b;
join(a,b);
}
ll ans=0;
for(int i=1;i<=n;i++)
{
if(find(i)==i)ans+=c[i];
}
cout<<ans<<endl;
return 0;
}

CF893C Rumor (并查集)的更多相关文章

  1. Educational Codeforces Round 33 (Rated for Div. 2) C. Rumor【并查集+贪心/维护集合最小值】

    C. Rumor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  2. CodeForces - 893C Rumor【并查集】

    <题目链接> 题目大意: 有n个人,其中有m对朋友,现在你有一个秘密你想告诉所有人,第i个人愿意出价a[i]买你的秘密,获得秘密的人会免费告诉它的所有朋友(他朋友的朋友也会免费知道),现在 ...

  3. CodeForces - 893C-Rumor(并查集变式)

    Vova promised himself that he would never play computer games... But recently Firestorm - a well-kno ...

  4. 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 ...

  5. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  6. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  7. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  8. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  9. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  10. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

随机推荐

  1. RPC实战与核心原理之流量回放

    流量回放:保障业务技术升级的神器 回顾 时钟轮在 RPC 中的应用,核心原理就一个关键字"分而治之",我们可以把它用在任何需要高效处理大量定时任务的场景中,最具有代表性的就是在高并 ...

  2. B1051 复数乘法

    描述 复数可以写成 (A+Bi) 的常规形式,其中 A 是实部,B 是虚部,i 是虚数单位,满足 i^​2=−1:也可以写成极坐标下的指数形式 (R×e​(Pi)​ ),其中 R 是复数模,P 是辐角 ...

  3. 9 easybr指纹浏览器https代理认证教程

    目的 在高匿名浏览环境中,代理是关键组件之一.相比普通 HTTP 代理,HTTPS 代理(HTTP over TLS) 支持加密传输,在保障隐私.防止中间人攻击方面更具优势. Chromium 浏览器 ...

  4. AI 制作游戏美术素材流程分享(程序员方向粗糙版)

    AI 制作游戏美术素材分享(程序员方向粗糙版) 视频讲解: 抖音:https://www.douyin.com/user/self?from_tab_name=main&modal_id=75 ...

  5. java LocalDateTime 加减当前时间

      LocalDateTime 可以对当前时间进行加减,在LocalDateTime类中,以plus打头的方法是增加某项时间,如plusDays的请求参数表示将要增加的天数,但是可以为负值:以minu ...

  6. github无法访问问题解决方法

    问题描述: 直接使用gtihub.com网址访问github浏览器无响应. 解决办法: 1.登录https://github.com.ipaddress.com/去查询github.com.githu ...

  7. UniApp前端+Java后端技术栈 解析微信支付功能的设计实现与关键实践

    感觉本篇对你有帮助可以关注一下我的微信公众号(深入浅出谈java) 会不定期更新知识!!! 一.概述 在移动互联网时代,支付功能已成为应用开发的核心能力之一.本文将以 UniApp前端+Java后端技 ...

  8. DAG任务调度系统 Taier 演进之道,探究DataSourceX 模块

    熟悉Taier的小伙伴们应该都知道,在11月7日发布的Taier1.3新版本中,我们融合了「DataSourceX 模块」.这是十分重要的一个变化,移除Taier外部插件依赖,新增数据源插件相关特性, ...

  9. .Net Web API 003 添加Controller实现用户登录

    1.Get和Post WEB API 中HTTP 请求方式的四个主要方法 (GET, PUT, POST, DELETE).Get一般用于查询.Put一般用于更新,Post用户创建,delete用户删 ...

  10. Nuclear - 基于流媒体的隐私优先音乐播放器

    Nuclear - 基于流媒体的隐私优先音乐播放器 项目描述 Nuclear是一款专注于从免费来源流媒体播放音乐的桌面应用,具有以下核心特点: 隐私优先设计,不进行用户追踪或数据分析 无广告干扰的纯净 ...