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

题意: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. 【Spring】Spring 事务控制

    Spring 事务控制 Spring 事务控制介绍 JavaEE 体系进行分层开发,事务控制位于业务层,Spring 提供了分层设计业务层的事务处理解决方案. Spring 的事务控制都是基于 AOP ...

  2. LeetCode117 每个节点的右向指针 II

    给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } 填充它的每个 ...

  3. 关于spring-data与elasticsearch的使用,自定义repository

    之前没有使用过spring-data,关于spring-data有很多很棒的设计,例如仅仅只需要声明一个接口就行,你甚至都不需要去实现,spring-data有内置默认的实现类,基本就上完成绝大多数对 ...

  4. 【Docker】在Linux系统中安装Docker虚拟机、启动停止重启查看Docker命令

    安装Docker 文章目录 安装Docker Docker虚拟机架构 在Linux系统中安装Docker虚拟机 管理Docker虚拟机 参考资料 Docker虚拟机架构 Docker创建的所有虚拟实例 ...

  5. 【Oracle】常见等待事件处理

    1.查看数据库中需要关注的等待事件: select sw.seq#,sw.sid||','||s.serial# sids,s.username,sw.event,sw.P1,sw.p2,sw.p3, ...

  6. top有用的开关控制命令

    [原创]本文为原创博文,转发请注明出处:https://www.cnblogs.com/dingbj/p/top_command.html 今天偶然用到top命令,在动态刷新的界面上输入h顺便看了下帮 ...

  7. Netty学习:EventLoop事件机制

    目录 EventLoop是什么 EventLoop适用的场景 Netty中的EventLoop Netty中的大量inEventLoop判断 Netty是如何建立连接并监听端口的-NIOSocketC ...

  8. 小白都看得懂的Javadoc使用教程

    Javadoc是什么 官方回答: Javadoc is a tool for generating API documentation in HTML format from doc comments ...

  9. shell批量解压源码包

    有时候部署环境有很多安装包,如果一个一个地解压缩实在太麻烦了,可以用shell批量进行解压缩.命令如下: [root@localhost ~]# vi tar.sh #! /bin/bash #标称是 ...

  10. 通过 profiling 定位 golang 性能问题 - 内存篇 原创 张威虎 滴滴技术 2019-08-02

    通过 profiling 定位 golang 性能问题 - 内存篇 原创 张威虎 滴滴技术 2019-08-02