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.
思路
题意是一个人和不同的k个人进行了不超过5分钟的通话,且回拨的概率不超过20%,那么就可以认为是嫌疑人。如果两个嫌疑人互相通话了,那么认为他们是一个团伙。
这题看上去挺麻烦的,但是实际上也不难,思路捋清楚了就好做。用二维矩阵存储通话时间,判断是否是嫌疑人,再合并所有互相通话的嫌疑人,最后输出所有嫌疑人。通过findFather函数判断两个嫌疑人是不是同伙,如果是,就输出在同一行。要掌握并查集的findFather函数以及合并函数!!!
代码
//来源https://blog.csdn.net/allisonshing/article/details/104361040
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
const int maxn=1005;
int k,n,m,g[maxn][maxn],father[maxn],visit[maxn];
vector<int> gang;
int findFather(int v){
return v==father[v]?v:(father[v]=findFather(father[v]));
}
void u(int a,int b){
int faA=findFather(a);
int faB=findFather(b);
if(faA<faB){
father[faA]=faB;
}else if(faA>faB){
father[faB]=faA;
}
}
int main(){
scanf("%d%d%d",&k,&n,&m); //k是阈值,n是不同的电话号码,m是电话记录条数
int c,r,d;
memset(g,0,sizeof(g));
memset(visit,0,sizeof(visit));
for(int i=1;i<=n;i++){
father[i]=i;
}
while(m--){
scanf("%d%d%d",&c,&r,&d);
g[c][r]+=d;
}
for(int i=1;i<=n;i++){
int sc=0,bc=0;
for(int j=1;j<=n;j++){
if(i==j) continue;
if(g[i][j]>0&&g[i][j]<=5){
sc++;
if(g[j][i]>0)
bc++;
}
}
if(sc>k&&bc*5<=sc)
gang.push_back(i);
}
for(int i=0;i<gang.size();i++){
for(int j=0;j<gang.size();j++){
if(i==j)continue;
int x=gang[i],y=gang[j];
if(g[x][y]!=0&&g[y][x]!=0)
u(x,y);
}
}
for(int i=0;i<gang.size();i++){
int x=gang[i];
if(visit[x]==1) continue;
visit[x]=1;
printf("%d",x);
for(int j=i+1;j<gang.size();j++){
int y=gang[j];
if(visit[y]==0&&findFather(x)==findFather(y)){
visit[y]=1;
printf(" %d",y);
}
}
printf("\n");
}
if(gang.size()==0) printf("None\n");
return 0;
}