PAT 2019-3 7-3 Telefraud Detection

Description:

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. Amakes 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 (≤, the threshold(阈值) of the amount of short phone calls), N (≤, the number of different phone numbers), and M (≤, 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

Keys:

  • 模拟题

Attention:

  • 很像A1034那题

Code:

 1 #include<cstdio>
 2 #include<vector>
 3 #include<set>
 4 #include<algorithm>
 5 using namespace std;
 6 const int M=1e3+10;
 7 int grap[M][M],gang[M],cal[M]={0},bck[M]={0};
 8 vector<int> per,grup[M];
 9 
10 int Find(int x)
11 {
12     int s=x;
13     while(gang[x] != x)
14         x=gang[x];
15     while(gang[s] != s)
16     {
17         int v=gang[s];
18         gang[s]=x;
19         s = v;
20     }
21     return x;
22 }
23 
24 void Union(int v1, int v2)
25 {
26     int f1 = Find(v1);
27     int f2 = Find(v2);
28     if(f1 < f2)
29         gang[f2]=f1;
30     else
31         gang[f1]=f2;
32 }
33 
34 int main()
35 {
36 #ifdef ONLINE_JUDGE
37 #else
38     freopen("Test.txt", "r", stdin);
39 #endif // ONLINE_JUDGE
40 
41     int n,m,k,v1,v2,w;
42     scanf("%d%d%d", &k,&n,&m);
43     fill(grap[0],grap[0]+M*M,0);
44     for(int i=1; i<=n; i++)
45         gang[i]=i;
46     for(int i=0; i<m; i++)
47     {
48         scanf("%d%d%d", &v1,&v2,&w);
49         grap[v1][v2]+=w;
50     }
51     for(v1=1; v1<=n; v1++)
52     {
53         for(v2=1; v2<=n; v2++)
54         {
55             if(grap[v1][v2]!=0 && grap[v1][v2]<=5)
56             {
57                 cal[v1]++;
58                 if(grap[v2][v1]!=0)
59                     bck[v1]++;
60             }
61         }
62         if(cal[v1]>k && bck[v1]*5<=cal[v1])
63             per.push_back(v1);
64     }
65     for(int i=0; i<per.size(); i++)
66         for(int j=i+1; j<per.size(); j++)
67             if(grap[per[i]][per[j]]!=0 && grap[per[j]][per[i]]!=0)
68                 Union(per[i],per[j]);
69     set<int> head;
70     for(int i=0; i<per.size(); i++)
71     {
72         v1 = Find(per[i]);
73         head.insert(v1);
74         if(v1 != per[i])
75             grup[v1].push_back(per[i]);
76     }
77     for(auto it=head.begin(); it!=head.end(); it++)
78     {
79         printf("%d", *it);
80         for(int i=0; i<grup[*it].size(); i++)
81             printf(" %d", grup[*it][i]);
82         printf("\n");
83     }
84     if(head.size()==0)
85         printf("None");
86 
87     return 0;
88 }

猜你喜欢

转载自www.cnblogs.com/blue-lin/p/11443352.html
7-3