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 (≤103, the number of different phone numbers), and M (≤105, 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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
题目大意:就是打电话给不同的人超过一定值并且是短途电话(小于5分),如果接受电话的人回电话的人数达到有效人数的0.2,那么就不算嫌疑人,有效的人数就是短途电话的人数,不是有效人数也回话的也不用算。
思路:保留每个人给每个人打电话的时长,由测试样例可以知道,算的是总时长,所以我们用两层unordered_map,这样存储排除了重复的元素,同时又有了总人数,然后因为可能出现1和2是通犯,然后2和3又是同犯,所以我们需要用到并查集,测试点2的话应该就是并查集用的错了。
代码
#include <bits/stdc++.h>
using namespace std;
int pre[2000];
vector<int>man[1010];
int find(int x){
if(pre[x]!=x){
pre[x]=find(pre[x]);
}
return pre[x];
}
void join(int x,int y){
int fx=find(x);
int fy=find(y);
if(fx<fy)swap(fx,fy);
if(fx!=fy){
pre[fx]=pre[fy];
}
return ;
}
int main(){
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
int i;
for(i=0;i<1010;i++)pre[i]=i;
unordered_map<int,unordered_map<int,int>>mymap;
int a[m+1][m+1];
fill(a[0],a[0]+(m+1)*(m+1),0);
for(i=0;i<k;i++){
int from,to;
int detention;
scanf("%d%d%d",&from,&to,&detention);
a[from][to]=detention;
mymap[from][to]+=detention;
}
int j;
unordered_map<int,int>::iterator that;
unordered_map<int,unordered_map<int,int>>::iterator it;
vector<int>ans;
for(it=mymap.begin();it!=mymap.end();it++){//存储嫌疑犯
int cnt=0;
int amount=0;
for(that=it->second.begin();that!=it->second.end();that++){
if(that->second>5)continue;
if(a[that->first][it->first])amount++;
cnt++;
}
if(cnt>n&&(double)amount<=cnt*0.2){
ans.push_back(it->first);
}
}
if(ans.size()){
sort(ans.begin(),ans.end());
map<int,vector<int>>gang;
for(i=0;i<ans.size();i++){
for(j=i+1;j<ans.size();j++){
if(a[ans[i]][ans[j]]&&a[ans[j]][ans[i]]){
join(ans[j],ans[i]);
}
}
}
for(i=0;i<ans.size();i++){//一定等把所有元素都归好类之后再存储。并查集一大忌就是边合并边存储。
gang[find(ans[i])].push_back(ans[i]);
}
map<int,vector<int>>::iterator p;
for(p=gang.begin();p!=gang.end();p++){
printf("%d",p->first);
for(i=1;i<p->second.size();i++){
printf(" %d",p->second[i]);
}
printf("\n");
}
}
else printf("None\n");
system("pause");
}
测试样例:
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 1
1 15 2
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
输出:
1 3 5 6