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 K 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 A A makes a short phone call to B B B means that the total duration of the calls from A A A to B B 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 K K (≤500, the threshold(阈值) of the amount of short phone calls), N N N (≤ 1 0 3 10^3 103, the number of different phone numbers), and M M 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 N 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

题意

给某个人的短电话是指与这个人的总共时长不超过5分钟。嫌疑人是那些每天打出超过K个短电话,而且这些人中不超过20%打回来的人。如果两个嫌疑人互相通话,就认为他们属于一个团伙。给出通话记录,计算所有的嫌疑人和团伙。

思路

用结构体表示一人的所有记录,包括这个人给其他人打出的电话总时间和所有打给他的电话,这个要支持快速查找,可以使用set。第一步先按照题意找出所有可疑的人。然后对于每一对嫌疑人判断他们是否互相打过电话,用并查集建立团伙信息。最后对团伙进行排序。
注意并查集合并时的写法。

代码

#include <iostream>
#include <set>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

struct record {
    set<int> callBack;  // 别人打给他的电话
    int calls[1005];  // 打出的电话
};

struct gan {
    vector<int> member;  // 团伙成员

    bool operator<(gan &x) {  // 对gang排序之前先把member排序
        if (!is_sorted(member.begin(), member.end()))
            sort(member.begin(), member.end());
        if (!is_sorted(x.member.begin(), x.member.end()))
            sort(x.member.begin(), x.member.end());
        return member[0] < x.member[0];
    }
};

int k, n, m;
gan gangs[1005];
int father[1005];
record records[1005];
vector<int> suspects;  // 所有可疑人员

int findFather(int x) {
    int f;
    if (father[x] == x) f = x;
    else f = findFather(father[x]);
    return f;
}

void unin(int a, int b) {
    int fa = findFather(a);
    int fb = findFather(b);
    if (fa != fb) father[fa] = fb;  // 注意是fa
}

int main() {
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    cin >> k >> n >> m;
    for (int i = 0; i < m; ++i) {
        int caller, receiver, duration;
        cin >> caller >> receiver >> duration;
        records[caller].calls[receiver] += duration;
        records[receiver].callBack.insert(caller);
    }

    // 找出所有可疑的人
    for (int i = 1; i <= n; ++i) {
        int shortCall = 0, callback = 0;
        for (int j = 1; j <= n; ++j) {
            if (records[i].calls[j] > 0 && records[i].calls[j] <= 5) {
                ++shortCall;
                if (records[i].callBack.count(j))
                    ++callback;
            }
        }
        if (shortCall > k && callback * 5 <= shortCall) {
            suspects.push_back(i);
        }
    }

    if (suspects.size() == 0) {
        cout << "None\n";
        return 0;
    }

    // 通过并查集计算团伙
    for (int i = 1; i <= n; ++i) father[i] = i;
    for (int i = 0; i < suspects.size(); ++i) {
        for (int j = i + 1; j < suspects.size(); ++j) {
            if (records[suspects[i]].calls[suspects[j]] != 0 and records[suspects[j]].calls[suspects[i]] != 0)
                unin(suspects[i], suspects[j]);
        }
    }

    int gangsCnt = 0;
    map<int, int> head2index;
    for (int i = 0; i < suspects.size(); ++i) {
        int head = findFather(suspects[i]);
        if (!head2index.count(head)) head2index[head] = gangsCnt++;
        gangs[head2index[head]].member.push_back(suspects[i]);
    }

    sort(gangs, gangs + gangsCnt);

    for (int i = 0; i < gangsCnt; ++i) {
        for (int j = 0; j < gangs[i].member.size(); ++j) {
            cout << gangs[i].member[j];
            if (j != gangs[i].member.size() - 1) cout << " ";
        }
        cout << "\n";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值