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 (≤ 1 0 3 10^3 103, the number of different phone numbers), and M (≤ 1 0 5 10^5 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:
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:
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.
思路
这题考察并查集的常规应用,先用二维数组存边,二维数组的值为边的权。然后遍历数组,判断是不是嫌疑人,把嫌疑人放到back数组里。因为题目要求每个团伙先输出最小的成员,因此在并查集合并的过程把小的成员编号作为编号大的成员编号的祖宗。最后用map整理输出即可。
cpp代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
const int N=1010;
int g[N][N];
int k,n,m;
int back[N],cnt;
int p[N];
int find(int k){
if(p[k]==k)return k;
else return p[k]=find(p[k]);
}
int main(){
cin>>k>>n>>m;
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
g[a][b]+=c;
}
int res=0;
for(int i=1;i<=n;i++){
int ant=0,peo=0;
for(int j=1;j<=n;j++){
if(g[i][j]&&g[i][j]<=5){//统计跟别人打短电话的人数
ant++;
if(g[j][i]>0)peo++;//统计有多少人回电话
}
}
if(ant>k&&(1.0*peo/ant)<=0.2){
back[cnt++]=i;//确定是嫌疑人
}
}
if(!cnt){
puts("None");
return 0;
}
for(int i=1;i<=n;i++)p[i]=i;
for(int i=0;i<cnt;i++){
for(int j=i+1;j<cnt;j++){
int x=back[i],y=back[j];
if(g[x][y]&&g[y][x]){
x=find(x),y=find(y);
if(x!=y){
x>y?p[x]=y:p[y]=x;
}
}
}
}
map<int,vector<int>> mp;//题目要求每个团伙按第一个成员升序排列
for(int i=0;i<cnt;i++){//遍历每个嫌疑人,看看他们的祖先是谁
int x=back[i];
mp[find(x)].push_back(x);
}
for(auto i:mp){
auto x=i.second;
sort(x.begin(),x.end());//题目要求每个团伙内部成员是升序排列
cout<<i.second[0];
for(int j=1;j<i.second.size();j++)cout<<" "<<i.second[j];
puts("");
}
}