7-3 Telefraud Detection (25分)
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
题目限制:
题目大意:
- 一个人给不同的K个以上的人打电话,其中给每个人的通话总和不超过5分钟的,称为短通话,短通话中只有不超过20%的人给他回电;
- 满足1的两个人互相通话,就是同一个团伙。
- 要根据通话记录,分析出团伙的成员,按照从小到大的序号输出成员编号。
算法思路:
这个题目的本意就是在所有嫌疑人组成的网络中找到每一个帮派的成员,而每一个帮派可以认为是一个连通分量,这样就可以使用DFS来进行求解,同时也可以认为是一个集合,这样就可以使用并查集的方式来求解,这里给出两种不同的实现方法。
预处理:
DFS和并查集的预处理都是一样的,主要目的就是找出所有的嫌疑人,这样才方便在所有嫌疑人所构成的网络中进行查找和合并。首先我们使用二维数组G保存该图的边权,并注意到此图是一个有向图,边权在输入的时候得累计,使用frequency和callback数组分别表示每一个人打电话不超过5分钟的次数和每一个嫌疑人在打的不超过5分种通话中,接收方回电话的人数,最后并用suspecters数组保存所有的嫌疑人。有了以上的容器后,接下来就是获取所有的嫌疑人了,直接遍历二维数组G,使用i表示caller,j表示receiver,只要G[i][j]!=0&&G[i][j]<=5
,说明当前caller打了一次短电话,累计++frequency[i]
,在此基础上,如果G[j][i]!=0
,说明receiver回电话了,累计++callback[i]
,在遍历完所有caller打的电话记录后,判断是否frequency[i]>K&&callback[i]<=floor(0.2*frequency[i])
如果是,说明i是嫌疑人,将其加入到suspecters。
DFS解法:
使用visited数组标记每一个嫌疑人是否被访问,set<int> gang
保存每一个帮派的人(一次DFS保存一个),bool hasGang
标记是否有帮派,初始为false,然后就使用DFS遍历整个图,每一次DFS遍历一个gang,遍历之前得清空gang集合的内容,核心代码如下:
void DFS(int root,int K){
visited[root] = true;
gang.insert(root);
for (int suspecter : suspecters) {
if(!visited[suspecter]&&G[root][suspecter]!=0&&G[suspecter][root]!=0){
//访问每一个没有被访问并且与当前root相互打电话的嫌疑人
DFS(suspecter,K);
}
}
}
bool hasGang = false;
for (int suspecter : suspecters) {
if(!visited[suspecter]){
gang.clear();
DFS(suspecter,K);
int num = 0;
for(auto &item:gang){
hasGang = true;// 有gang存在
printf("%d",item);
if(num<gang.size()-1) printf(" ");
++num;
}
printf("\n");
}
}
并查集解法:
在预处理完成之后,就要将所有属于一个gang的嫌疑人进行合并,合并操作使用Union函数来实现,由于嫌疑人是从小到大加入的集合,我们就可以直接从第一个没有被访问的嫌疑人开始,将他和其同属于一个gang的其他嫌疑人一并进行输出即可,这样就省去了排序的麻烦,其核心代码如下:
int father[1005];
void init(){
for(int i=0;i<1005;++i){
father[i] = i;
}
}
int getFather(int a){
while(a!=father[a]){
a = father[a];
}
return a;
}
void Union(int a,int b){
int fa = getFather(a);
int fb = getFather(b);
if(fa!=fb){
father[fb] = fa;
}
}
// 合并所有的嫌疑人
for(int i:suspecters){
for(int j:suspecters){
if(i!=j&&G[i][j]!=0&&G[j][i]!=0){
// i和j是一个gang
Union(i,j);
}
}
}
// 嫌疑人是从小到大加入的集合,从小到大直接输出即可
for(int i=0;i<suspecters.size();++i){
if(!visited[suspecters[i]]){
visited[suspecters[i]] = true;
printf("%d",suspecters[i]);
for(int j=i+1;j<suspecters.size();++j){
if(!visited[suspecters[j]]&&getFather(suspecters[i])==getFather(suspecters[j])){
visited[suspecters[j]] = true;
printf(" %d",suspecters[j]);
}
}
printf("\n");
}
}
注意点:
- 1、嫌疑人的判定中需要不超过20%的人回电话,这个20%如果是做的乘法运算得向下取整,也可以使用回电话的人数乘以5来代替。
- 2、嫌疑人之间得互相打电话才算是属于一个gang。
提交结果:
AC代码1:
#include <string>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
int G[1005][1005];
int frequency[1005];// 统计每一个人打电话不超过5分钟的次数
int callback[1005];// 每一个嫌疑人被回电话的人数
vector<int> suspecters;// 所有的嫌疑人
bool visited[1005];// 标记每一个嫌疑人是否被访问
set<int> gang;// 每一个帮派的嫌疑人
void DFS(int root,int K){
visited[root] = true;
gang.insert(root);
for (int suspecter : suspecters) {
if(!visited[suspecter]&&G[root][suspecter]!=0&&G[suspecter][root]!=0){
DFS(suspecter,K);
}
}
}
int main(){
int K,N,M;//阈值,顶点数目,边数目
scanf("%d %d %d",&K,&N,&M);
for (int i = 0; i < M; ++i) {
int a,b,time;
scanf("%d %d %d",&a,&b,&time);
G[a][b] += time;
}
for(int i=1;i<=N;++i){
for (int j = 1; j <=N ; ++j) {
if(G[i][j]!=0&&G[i][j]<=5){
++frequency[i];
if(G[j][i]!=0){
// j给i回电话了
++callback[i];
}
}
}
if(frequency[i]>K&&callback[i]<=floor(0.2*frequency[i])){
// 打不超过5分钟的电话的次数超过了阈值,并且回电话的人数不超过20%
suspecters.push_back(i);
}
}
bool hasGang = false;
for (int suspecter : suspecters) {
if(!visited[suspecter]){
gang.clear();
DFS(suspecter,K);
int num = 0;
for(auto &item:gang){
hasGang = true;
printf("%d",item);
if(num<gang.size()-1) printf(" ");
++num;
}
printf("\n");
}
}
if(!hasGang){
printf("None");
}
return 0;
}
AC代码2:
#include<cstdio>
#include <cmath>
#include <vector>
using namespace std;
int G[1005][1005];
int frequency[1005];// 统计每一个人打电话不超过5分钟的次数
int callback[1005];// 每一个嫌疑人被回电话的人数
vector<int> suspecters;// 所有的嫌疑人
bool visited[1005];// 标记每一个嫌疑人是否被访问
int father[1005];
void init(){
for(int i=0;i<1005;++i){
father[i] = i;
}
}
int getFather(int a){
while(a!=father[a]){
a = father[a];
}
return a;
}
void Union(int a,int b){
int fa = getFather(a);
int fb = getFather(b);
if(fa!=fb){
father[fb] = fa;
}
}
int main(){
init();
int K,N,M;//阈值,顶点数目,边数目
scanf("%d %d %d",&K,&N,&M);
for (int i = 0; i < M; ++i) {
int a,b,time;
scanf("%d %d %d",&a,&b,&time);
G[a][b] += time;
}
for(int i=1;i<=N;++i){
for (int j = 1; j <=N ; ++j) {
if(G[i][j]!=0&&G[i][j]<=5){
++frequency[i];
if(G[j][i]!=0){
// j给i回电话了
++callback[i];
}
}
}
if(frequency[i]>K&&callback[i]<=floor(0.2*frequency[i])){
// 打不超过5分钟的电话的次数超过了阈值,并且回电话的人数不超过20%
suspecters.push_back(i);
}
}
// 合并所有的嫌疑人
for(int i:suspecters){
for(int j:suspecters){
if(i!=j&&G[i][j]!=0&&G[j][i]!=0){
// i和j是一个gang
Union(i,j);
}
}
}
// 嫌疑人是从小到大加入的集合,从小到大直接输出即可
for(int i=0;i<suspecters.size();++i){
if(!visited[suspecters[i]]){
visited[suspecters[i]] = true;
printf("%d",suspecters[i]);
for(int j=i+1;j<suspecters.size();++j){
if(!visited[suspecters[j]]&&getFather(suspecters[i])==getFather(suspecters[j])){
visited[suspecters[j]] = true;
printf(" %d",suspecters[j]);
}
}
printf("\n");
}
}
if(suspecters.empty()){
printf("None");
}
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。