刷题刷到自闭,写个博客放松一下

题意:n个人,m对朋友,每寻找一个人传播消息需要花费相应的价钱,朋友之间传播消息不需要花钱,问最小的花费

把是朋友的归到一起,求朋友中花钱最少的,将所有最少的加起来。

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<string.h>
4 #define ll long long
5 using namespace std;
6
7 ll a[1000100];
8
9 struct lll
10 {
11 ll par,b,c;
12 }p[100100];
13
14 int cmp(lll x,lll y)
15 {
16 if(x.par==y.par) return x.b<y.b;
17 return x.par<y.par;
18 }
19
20 ll find(ll x)
21 {
22 if(x!=p[x].par) p[x].par=find(p[x].par);
23 return p[x].par;
24 }
25
26 void unionn(ll x,ll y)
27 {
28 ll fa=find(x);
29 ll fb=find(y);
30 if(fb==fa) return ;
31 if(p[fa].c<p[fb].c){
32 p[fa].par=fb;
33 }
34 else p[fb].par=fa;
35 if(p[fa].c==p[fb].c) p[fa].c++;
36 }
37
38 int main()
39 {
40 ll n,m;
41 while(scanf("%lld%lld",&n,&m)!=EOF){
42 memset(a,0,sizeof a);
43 memset(p,0,sizeof p);
44 for(ll i=1;i<=n;i++){
45 scanf("%lld",&a[i]);
46 p[i].par=i;
47 p[i].b=a[i];
48 p[i].c=0;
49 }
50 ll x,y;
51 for(ll i=0;i<m;i++){
52 scanf("%lld%lld",&x,&y);
53 unionn(x,y);
54 }
55 ll sum=0;
56 for(int i=1;i<=n;i++)
57 while(p[i].par!=i&&p[p[i].par].par!=p[i].par) //是同一类但是头不是老大 把头变为老大;
58 p[i].par=p[p[i].par].par;
59 sort(p+1,p+n+1,cmp); //把结构体按类排序,主要是同一类的花费少的在前
60 sum=p[1].b;
61 for(int i=2;i<=n;i++){
62 if(p[i].par!=p[i-1].par) sum+=p[i].b; //是同一类的加上花费最少的第一个 不是同一类就继续循环
63 }
64 printf("%lld\n",sum);
65 }
66 return 0;
67 }

CodeForces 893C (并查集板子题)的更多相关文章

  1. HDU 1232 并查集板子题

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...

  2. Zjnu Stadium HDU - 3047 带权并查集板子题

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; +; int ...

  3. 畅通工程 HDU - 1232 并查集板子题

    #include<iostream> #include<cstring> using namespace std; ; int p[N]; int find(int x) { ...

  4. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  5. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  6. poj1182 食物链(并查集 好题)

    https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...

  7. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  8. PAT甲级 并查集 相关题_C++题解

    并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...

  9. Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题

    E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. 天梯赛练习 L3-006 迎风一刀斩 (30分) 几何关系

    题目分析: 对于给出的两个多边形是否可以组成一个矩形,这里我们分以下几种情况讨论 1.首先对于给出的两个多边形只有3-3,3-4,3-5,4-4才有可能组成一个矩形,并且两个多边形只可能是旋转90,1 ...

  2. 【UML】Use Case Diagrams

    文章目录 Use Case Diagrams Introduction Use case Diagram Use Case Diagrams - Actors Use Case Diagrams – ...

  3. Git安装/VScode+Git+Github

    Git安装/VScode+Git+Github 1. 相关简介 git 版本控制工具,支持该工具的网站有Github.BitBucket.Gitorious.国内的OS China仓库.Csdn仓库等 ...

  4. 微信登录1-OAuth2简介

    一.OAuth2解决什么问题 1.开放系统间授权 照片拥有者想要在云冲印服务上打印照片,云冲印服务需要访问云存储服务上的资源 2.图例 资源拥有者:照片拥有者 客户应用:云冲印 受保护的资源:照片 3 ...

  5. C++ unordered_map/unordered_set 自定义键类型

    1. unordered_map 和 unordered_set template < class Key, // unordered_map::key_type class T, // uno ...

  6. python(re正则)

    import re #导入模块   info = 'qwewwer12332423kdsjfkl2342kdjfl213nkafal123123' 例1: res1 = re.compile('er( ...

  7. centos7防火墙firewalld拒绝某ip或者某ip段访问服务器任何服务

    安装firewall后(LINUX7系统一般情况下默认已安装),防火墙默认配置是只打开SSH端口的,也就是22端口,如果SSH的端口已更改成别的端口了,请切记一定在启动firewall前先修改对应服务 ...

  8. LDAP 简介

    一.使用 Directory Services(目录服务)的目的 对于局域网内的一个用户来讲,工作等其它应用需要,我们必须凭帐号登录主机.用帐号收发E-mail,甚至为了管理需要公司还需要维护一个电子 ...

  9. 深度漫谈数据系统架构——Lambda architecture

    https://mp.weixin.qq.com/s/whmhm2yzug2WVdH3dTq8hg

  10. (013)每日SQL学习:日期的各种计算

    1.确定两个日期之间的工作日天数 --确定两个日期之间的工作日天数with x0 as (select to_date('2018-01-01','yyyy-mm-dd') as 日期 from du ...