A1158-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 (≤10​3​​, the number of different phone numbers), and M (≤10​5​​, 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
/*
 * @Descripttion: 
 * @version: 
 * @Author: iDestro
 * @Date: 2019-09-01 01:03:03
 * @LastEditors: iDestro
 * @LastEditTime: 2019-09-01 14:28:02
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#define maxn 1005

using namespace std;
vector<int> temp;
map<int, set<int> > ans;
int G[maxn][maxn];
int c[maxn] = {0}, r[maxn] = {0};
bool vis[maxn] = {false};

// 并查集基本操作
int father[maxn];
void init() {
    for (int i = 0; i < maxn; i++) {
        father[i] = i;
    }
}

int Find(int x) {
    while (x != father[x]) {
        x = father[x];
    }
    return x;
}

void Union(int a, int b) {
    int fa = Find(a);
    int fb = Find(b);
    if (fa != fb) {
        father[fb] = fa;
    }
}

int main() {
    init();
    fill(G[0], G[0]+maxn*maxn, 0);
    int k, n, m, u, v, t;
    cin >> k >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> u >> v >> t;
        // 题中注明total
        G[u][v] += t;
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            // 求存在通话,且时长小于等于5的个数
            if (G[i][j] > 0 && G[i][j] <= 5) {
                c[i]++;
                if (G[j][i] > 0) {
                    r[i]++;
                }
            }
        }
    }
    
    // 筛选出suspects
    for (int i = 1; i <= n; i++) {
        if (c[i] > k && r[i]*1.0/c[i] <= 0.2) {
            temp.push_back(i);
        }
    }

    // 如果没有怀疑的人,直接退出
    if (temp.size() == 0) {
        cout << "None";
        return 0;
    }
    for (int i = 0; i < temp.size(); i++) {
        for (int j = i+1; j < temp.size(); j++) {
            int u = temp[i], v = temp[j];
            // 题中注明each other, 要相互打电话,则属于一个gang
            if (G[u][v] > 0 && G[v][u] > 0) {
                Union(u, v);
            }
        }
    }

    // 利用map构造各个gang,其中key对应gang中最小的index
    for (int i = 0; i < temp.size(); i++) {
        int u = temp[i];
        if (!vis[u]) {
            ans[u].insert(u);
            for (int j = i+1; j < temp.size(); j++) {
                int v = temp[j];
                if (!vis[v]) {
                    int fu = Find(u), fv = Find(v);
                    if (fu == fv) {
                        ans[u].insert(v);
                        vis[v] = true;
                    }
                }
            }
        }
    }
    
    // 遍历各个团伙
    map<int, set<int> > :: iterator map_it = ans.begin();
    while (map_it != ans.end()) {
        set<int> s = map_it -> second;
        set<int> :: iterator set_it = s.begin();
        while (set_it != s.end()) {
            cout << *set_it;
            set_it++;
            if (set_it != s.end()) {
                cout << " ";
            }
        }
        map_it++;
        if (map_it != ans.end()) {
            cout << endl;
        }
    }
    return 0;
}
发布了20 篇原创文章 · 获赞 5 · 访问量 1772

猜你喜欢

转载自blog.csdn.net/iDestro/article/details/100183031