并查集适用问题举例

1、已知,有n个人和m对好友关系
2、如果两个人是直接的或者间接的好友(好友的好友的好友。。。),那么他们属于一个集合,就是一个朋友圈中
3、写出程序,求这n个人中一共有多少个朋友圈

更详细请查看

http://www.omegaxyz.com/2019/02/25/union-find/

PAT-2019年春季考试-甲级-3

Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone call records.

A person must be detected as a suspect if he/she makes more than K short phone calls to different people everyday, but no more than 20% of these people would call back. And more, if two suspects are calling each other, we say they might belong to the same gang. A makes a short phone call to B means that the total duration of the calls from A to B is no more than 5 minutes.

Input Specification:

Each input file contains one test case. For each case, the first line gives 3 positive integers K (≤500, the threshold(阈值) of the amount of short phone calls), N (≤10​3​​, the number of different phone numbers), and M (≤10​5​​, the number of phone call records). Then M lines of one day’s records are given, each in the format:

caller receiver duration

where caller and receiver are numbered from 1 to N, and duration is no more than 1440 minutes in a day.

Output Specification:

Print in each line all the detected suspects in a gang, in ascending order of their numbers. The gangs are printed in ascending order of their first members. The numbers in a line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

If no one is detected, output None instead.

Sample Input 1:

5 15 31
1 4 2
1 5 2
1 5 4
1 7 5
1 8 3
1 9 1
1 6 5
1 15 2
1 15 5
3 2 2
3 5 15
3 13 1
3 12 1
3 14 1
3 10 2
3 11 5
5 2 1
5 3 10
5 1 1
5 7 2
5 6 1
5 13 4
5 15 1
11 10 5
12 14 1
6 1 1
6 9 2
6 10 5
6 11 2
6 12 1
6 13 1

Sample Output 1:

3 5
6

Note: In sample 1, although 1 had 9 records, but there were 7 distinct receivers, among which 5 and 15 both had conversations lasted more than 5 minutes in total. Hence 1 had made 5 short phone calls and didn’t exceed the threshold 5, and therefore is not a suspect.

Sample Input 2:

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

Sample Output 2:

None

C++代码:

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<map>using namespace std;int durcnt[1010][1010]; //统计总通话时间
int father[1010]; //并查集fatherint findFather(int n){int a = n;while(a != father[a])a = father[a];while(n != father[n]){int z = n;n = father[n];father[z] = a;}return a;
}void setUnion(int x, int y){int fx = findFather(x);int fy = findFather(y);if(fx != fy){if(fy < fx) swap(fx, fy); //序号较小的作为leaderfather[fy] = fx;}
}void init(int n){for(int i = 1; i <= n; i++)father[i] = i;
}int main(){map<int, vector<int> > gangs;vector<int> suspects;int K, N, M;cin>>K>>N>>M;init(N);int c, r, d;for(int i = 0; i < M; i++){cin>>c>>r>>d;durcnt[c][r] += d;}//判断是否是诈骗团伙for(int i = 1; i <= N; i++){int sumDiff = 0, rcall = 0;for(int j = 1; j <= N; j++){if(durcnt[i][j] > 0 && durcnt[i][j] <= 5){sumDiff++;if(durcnt[j][i] > 0)rcall++;}}if(sumDiff > K && 1.0 * rcall / sumDiff <= 0.2)suspects.push_back(i);}//找出每一个团伙的成员for(int i = 0; i < suspects.size(); i++){for(int j = i; j < suspects.size(); j++){if(durcnt[suspects[i]][suspects[j]] > 0 && durcnt[suspects[j]][suspects[i]] > 0){setUnion(suspects[i], suspects[j]); //同一个团伙的合并}}}for(int i = 0; i < suspects.size(); i++){int leader = findFather(suspects[i]);gangs[leader].push_back(suspects[i]);}if(gangs.size() == 0)cout<<"None"<<endl;else{for(int i = 0; i < gangs.size(); i++){sort(gangs[i].begin(), gangs[i].end());for(int j = 0; j < gangs[i].size(); j++){printf("%d%s", gangs[i][j], j == gangs[i].size() - 1 ? "\n" : " ");}}}return 0;
}

更多内容访问 omegaxyz.com
网站所有代码采用Apache 2.0授权
网站文章采用知识共享许可协议BY-NC-SA4.0授权
© 2019 • OmegaXYZ-版权所有 转载请注明出处

并查集应用——PAT甲级2019春季相关推荐

  1. 【最新合集】PAT甲级最优题解(题解+解析+代码)

    以下每道题均是笔者多方对比后, 思考整理得到的最优代码,欢迎交流! 共同成长哇.可以和博主比拼一下谁刷的更快~ 欢迎收藏.欢迎来玩儿 PAT题解目录 题号 标题 题解 分类 使用算法 1001 A+B ...

  2. PAT甲级 2019年冬季 题解

    题目1:7-1 Good in C (20分) When your interviewer asks you to write "Hello World" using C, can ...

  3. 【置顶】【PAT】PAT甲级题目及分类总结(持续更新ing)

    在2019年3月底,我决定考浙大计院,经过一个月还算凑合的学习,痛定思痛,决定整理整理自己的博客. 粗略估计,大概一个月的时间里我AC了31道题,大概用时40个小时上下,毕竟还要把大部分时间花在笔试上 ...

  4. PAT甲级1118 Birds in Forest :[C++题解]并查集

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:并查集的合并和查询. 问:一张照片上的鸟如何合并?相邻的合并(笔者采用的方式)或者全合并到第一只鸟就行,遍历一遍.所有照片中的鸟,合并 ...

  5. PAT甲级1114 Family Property:[C++题解]结构体、并查集、测试点3、4、5有问题的进来!!

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 先建边.读入每家的信息,在本人和父母(如果有的话),本人与子女(如果有的话)之间分别建边.边用结构体来存,边记录两个端点. 遍历每条 ...

  6. PAT甲级1013 Battle Over Cities:[C++题解]并查集、结构体存边

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:并查集题目. 不清楚并查集的小伙伴,请移步并查集原理并查集板子:acwing836. 合并集合. 题意:给定一个连通图,当删掉任意1个 ...

  7. PAT甲级1126 Eulerian Path:[C++题解] 欧拉路径、并查集,测试点4有问题请进来

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 欧拉图: 1)连通 2)度都为偶数 半欧拉图:欧拉路径:2)连通2) 度为奇数的结点有两个,其他度都是偶数 非欧拉图:不是欧拉图和半 ...

  8. PAT甲级1021 Deepest Root :[C++题解]树的最大深度、并查集、dfs求树的深度

    文章目录 题目分析 题目链接 题目分析 分析: 考察知识点:并查集.dfs.树的深度 给定n个结点,n-1条边,只要能保证只有1个连通分量,就是一棵树.否则的话就不是树,它是不连通的. 用并查集来看是 ...

  9. PAT甲级1107 Social Clusters (30 分):[C++题解]并查集,爱好、人数

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 凭爱好,分人群.注意点:爱好可传递.什么意思?意思是A和B的有共同爱好1, B和C有共同爱好2,那么认为A和C也是同一群人. 按照爱 ...

  10. PAT甲级题目翻译+答案 AcWing(并查集)

    1013 Battle Over Cities (25 分) 题意 :给图,问去掉所询问的一个点后,需要添加多少条边可以使图连通,N<1000N<1000N<1000 思路 :并查集 ...

最新文章

  1. Oracle Database基础
  2. JupyterLab 3.0发布:支持中文界面,安装插件无需Node.js
  3. 渗透测试 回显机制平台 dnslog 搭建
  4. JavaScript 自执行函数剖析
  5. 调用http_【学习充电】直观讲解一下 RPC 调用和 HTTP 调用的区别!
  6. 库克跟乔布斯差几代iPhone? 解读iPhone十年变与不变
  7. Power BI Desktop中的分解树
  8. main函数的argc和argv
  9. 7. JavaScript HTML DOM - 改变 CSS
  10. 网页设计-动态雪花背景源码
  11. ARCGIS矢量数据的空间分析——叠加分析
  12. 迪杰斯特拉--链式向前星
  13. 数据挖掘 顶级期刊_数据挖掘顶级期刊简介_47209.doc
  14. 苹果企业号-通过网页下载应用,部署应用分发服务器
  15. Mac电脑 zsh: command not found: vue
  16. 实现微信小程序与微信生态的互相跳转H5入口
  17. python 幂数拟合及拟合度计算
  18. 成为富人的十大心理特质
  19. yum 安装mysql 后 which is not functionally dependent on columns in GROUP BY clause; this is incompatibl
  20. 原生JS总结-- JS 绑定点击事件

热门文章

  1. Protobuf 判断某个值是否在一个枚举值中
  2. JAVAWeb项目 微型商城项目-------(七)后台添加用户管理和商品类型管理操作
  3. 每日一道剑指offer-两个栈来实现一个队列
  4. python常用数据结构_Python中常用的查找数据结构及算法汇总
  5. python dag调度系统开发_深度解析 | 基于DAG的分布式任务调度平台:Maat
  6. (秒杀项目) 4.7 缓存商品与用户
  7. android 背景逐渐变暗,UI:使View背景逐渐变暗的方法
  8. matlab 自适应波束,(完整word版)自适应波束形成与Matlab程序代码注解
  9. 批量数据插入(Java读取Excel文件并使用mybatis写入MySQL数据库)(含免费源码下载)
  10. HTML:hr横线改变颜色