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. JDK安装及IDE安装编辑

    1.下载及安装JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 环境变量的系统变量填上如下: 变量 ...

  2. WindowsPE文件格式入门10.TLS表

    https://www.bpsend.net/thread-407-1-1.html 当一个全局变量,所有的线程都会同时访问这个全局变量,其实就是访问同一块内存,有时我们希望所有的内存访问同一块内存, ...

  3. odoo16里面修改tree视图样式

    一.在static文件夹下新建一个css文件夹并将*.css文件写入 /*该文件用来定义视图中的一些格式,需要用到的地方直接在xml文件中进行引用*/ /*语法说明*/ /* table th:nth ...

  4. Django Web应用开发实战第二章

    一.基本配置信息 """ Django settings for myblog project. Generated by 'django-admin startproj ...

  5. AtCoder Beginner Contest 341-F

    AtCoder Beginner Contest 341-F F - Breakdown Problem 给你一个由 \(N\) 个顶点和 \(M\) 条边组成的简单无向图.每个顶点拥有权重\(W_i ...

  6. Hypermesh_LsDyna划分网格

    1.创建角点 2.创建单元(按F6) 3.划分网格(按F12) 4.清除临时节点(Geom → temp nodes → clear all) 5.设置单元集(PD单元)(*SET_SHELL_LIS ...

  7. 在工具类静态方法调用@Autowired注入的bean方法

    今天在搞一个工具类的时候,需要在工具类的静态方法中调用mapper的方法插入数据,但是,用spring的@Autowired注入bean后,测试一跑,报空指针异常. 解决方案如下: 1.对工具类使用@ ...

  8. vue3 + springboot实现微信登录

    创建VUE3项目 创建初始文件 进入项目存放位置 右键用命令行打开(终端打开) npm create vite@latest wechat-report --template vue npm:包管理需 ...

  9. 用好 JUnit 5 的高级特性:提升单测效率和质量

    写在前面 在当今的软件开发实践中,单元测试已成为保障代码质量的必备环节.许多团队已经积累了一定的单元测试经验,能够编写基本的测试用例来验证功能逻辑.然而,当我们面对复杂的业务场景时,仅靠基础的JUni ...

  10. 几分钟了解下java虚拟机--04

    方法内联 它的基本思想是在调用某个方法时,不通过跳转指令去执行该方法的代码,而是直接将该方法的代码复制到调用点处.这样可以减少方法调用的开销,包括减少函数调用和返回的指令执行时间,以及减少堆栈操作 方 ...