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.数组
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.函数
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.this
If no one is detected, output None
instead.spa
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
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.code
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
None
这个题目的本意就是在全部嫌疑人组成的网络中找到每个帮派的成员,而每个帮派能够认为是一个连通份量,这样就可使用DFS来进行求解,同时也能够认为是一个集合,这样就可使用并查集的方式来求解,这里给出两种不一样的实现方法。orm
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。blog
使用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"); } }
#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; }
#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; }