PAT 2019年春季 7-3 Telefraud Detection (25 分)
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
实现思路:
挺搞心态的这题,题意如下:
是诈骗嫌疑犯的满足条件为:
向不同的人打了K个短电话,短电话定义是诈骗犯单方面向某人总共打了5分钟以下,并且这些人中少于20%的人给回电(回电总时间不在乎)。
若是嫌疑人互相有通话记录则算是一个同伙(不管通话多少时间,大于0即可),团伙按照他们第一个成员来id升序输出。
这种题目和之前的一道题1034的Head of Gang有相似之处,在最后运用并查集或者dfs来合并一个团伙,这里自己一开始犯了错误,用了双循环来判断是否是团伙,被自己气死,卡了许久,A->B,B->A,B->C,C->B这种情况用双循环是无法判断ABC是一个团伙的,有间接通话的联系,必须要有并查集思维或者用DFS去实现。
AC代码:
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <set>
using namespace std;
const int N=1010;
int k,n,m,cnt=0;
vector<int> G[N],temp,ans[N];
unordered_map<int,int> mp;
bool v[N]= {false};
set<int> st;
void dfs(vector<int> &sq,int x) {
v[x]=true;
st.insert(x);
for(int i=0; i<sq.size(); i++) {
if(!v[sq[i]]&&mp[sq[i]*N+x]>0&&mp[x*N+sq[i]]>0) {
dfs(sq,sq[i]);
}
}
}
int main() {
cin>>k>>n>>m;
int a,b,time;
while(m--) {
scanf("%d%d%d",&a,&b,&time);
if(mp.count(a*N+b)==0)
G[a].push_back(b);
mp[a*N+b]+=time;
}
for(int i=1; i<=n; i++) {
int num=0,pNum=0;//num统计短电话数 pNum统计回电的人数
for(int j=0; j<G[i].size(); j++) {
if(mp[i*N+G[i][j]]<=5) {
num++;
if(mp[G[i][j]*N+i]>0) pNum++;
}
}
if(num>k&&pNum*5.0<=num) temp.push_back(i);
}
if(temp.size()==0) {
printf("None");
} else {
for(int i=0; i<temp.size(); i++) {
if(!v[temp[i]]) {
st.clear();
dfs(temp,temp[i]);
bool flag=false;
for(auto it : st) {
if(flag) printf(" ");
flag=true;
printf("%d",it);
}
printf("\n");
}
}
}
return 0;
}
方法二 并查集(未提交测试 只通过了样例)
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
const int N=1010;
int W[N][N]= {0},k,n,m,father[N];
vector<int> G[N],sq[N];
int findFather(int x) {
int a=x;
while(x!=father[x]) {
x=father[x];
}
while(a!=father[a]) {
int z=a;
a=father[a];
father[z]=x;
}
}
void Union(int a,int b) {
int faA=findFather(a);
int faB=findFather(b);
if(faA<faB) father[faB]=faA;
else father[faA]=faB;
}
int main() {
for(int i=0; i<N; i++) father[i]=i;
cin>>k>>n>>m;
int a,b,t;
while(m--) {
scanf("%d%d%d",&a,&b,&t);
W[a][b]+=t;
G[a].push_back(b);
}
vector<int> ans;
for(int i=1; i<=n; i++) {
int call=0,back=0;
for(int j=0; j<G[i].size(); j++) {
int id=G[i][j];
if(W[i][id]<=5) {//当通话不大于5分钟
call++;
if(W[id][i]>0) back++;//有回电的次数
}
}
if(call>k&&back*5.0<=call) ans.push_back(i);//当短电话大于k个并且回电少于20%就是嫌疑犯
}
if(ans.size()==0) {
printf("None\n");
return 0;
}
for(int i=0; i<ans.size(); i++) {
for(int j=i+1; j<ans.size(); j++) {
if(W[ans[i]][ans[j]]>0&&W[ans[j]][ans[i]]>0)//嫌疑犯互通电话属于一个团伙
Union(ans[i],ans[j]);//合并
}
}
for(int i=0; i<ans.size(); i++)
sq[findFather(ans[i])].push_back(ans[i]);//寻炸团伙
for(int i=0; i<ans.size(); i++) {
int idx=ans[i];
if(sq[idx].size()>0) {
for(int j=0; j<sq[idx].size(); j++) {
printf("%d",sq[idx][j]);
if(j<sq[idx].size()-1) printf(" ");
else printf("\n");
}
}
}
return 0;
}