7-3 Telefraud Detection (25 分)
作者: 陈越
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
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 (≤1000, the number of different phone numbers), and M (≤1e5, 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
考点:并查集
题意:给出一些通话记录,判断一个人是否是嫌疑人,如果嫌疑人之间通过话则是团伙作案,输出的时候在同一行输出(一行一个 团伙)。
思路:首先记录下所有人之间通话情况,用一个二维数组。
接着用ans1记录通话在0-5min的数量,ans2记录回拨数量,求出ans3,判断一下当前的人是否是嫌疑人。
此时我们已经将所有的嫌疑人处理出来放在一个vector中,接着用并查集处理他们是否的团伙。
非常简单只需要判断是否有互相的通话记录,如果有就进行合并操作。
最后按照格式输出即可。
比赛的时候是一发入魂,但是整整写了一个小时。。(中间妄想不用并查集处理)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3+7;
int mp[N][N];
vector<int> ans,stk[N];
int fa[N],flag[N];
void init()
{
for(int i=0;i<N;i++) fa[i]=i;
}
int Find(int x)
{
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
void Union(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy){
if(fx<fy) fa[fx]=fy;
else fa[fy]=fx;
}
}
int main()
{
int n,m,k; scanf("%d%d%d",&k,&n,&m);
for(int i=1;i<=m;i++){
int a,b,c; scanf("%d%d%d",&a,&b,&c);
mp[a][b]+=c;
}
for(int i=1;i<=n;i++){
int ans1=0,ans2=0;
for(int j=1;j<=n;j++){
if(i==j) continue;
if(mp[i][j]<=5 && mp[i][j]>0) {
ans1++;
if(mp[j][i]) ans2++;
}
}
double ans3=(ans2)*1.0/ans1;
if(ans1>k && ans3<=0.2){
ans.push_back(i);
}
}
if(ans.size()==0) puts("None");
else{
sort(ans.begin(),ans.end());
init();
for(int i=0;i<ans.size();i++){
for(int j=0;j<ans.size();j++){
if(i==j) continue;
int x=ans[i];
int y=ans[j];
if(mp[x][y] && mp[y][x]){
Union(x,y);
}
}
}
for(int i=0;i<ans.size();i++){
if(flag[i]) continue;
printf("%d",ans[i]);
for(int j=0;j<ans.size();j++){
if(i==j) continue;
if(flag[i]) continue;
if(Find(ans[i])==Find(ans[j])){
flag[j]=1;
printf(" %d",ans[j]);
}
}
puts("");
}
}
return 0;
}