1034 Head of a Gang——PAT甲级真题


1034 Head of a Gang

One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N lines follow, each in the following format:

Name1 Name2 Time

where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:

For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1:

8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 1:

2
AAA 3
GGG 3

Sample Input 2:

8 70
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 2:

0

题目大意:有一组团伙,团伙成员之间会相互通话,一组团伙的总通话时间>K并且团伙成员的数目大于2人,这组团伙才算合格。并且一组团伙中的头目是通话时间最长的人。现在给出N组通话记录和K,让你按照字典序输出每组团伙头目并输出这组团伙有多少人。

大致思路:由题意可知这道题要求我们用并查集来划分集合。先给每一个人一个编号方便后续操作,定义两个数组 sizes和weight 分别用来维护每一个人的通话时间和这组集合的通话时间。用 vector> v 来记录每一个集合的成员编号。在进行集合的合并操作时更新 weight 的值。最后找到符合条件的集合以及集合中通话之间最长的人。

代码:

#include 

using namespace std;

const int N = 1e4 + 10;
typedef pair PII;
vector ans;
int n, k;
int fa[N * 2];
map mp;  //给每个人名一个编号
map ump;
int weight[N * 2];
vector> v;
int sizes[N * 2];

inline int find(int x) {
    if (x != fa[x]) fa[x] = find(fa[x]);
    return fa[x];
}

bool cmp(PII a, PII b) { return a.first <= b.first; }

int main() {
    // freopen("in.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    v.resize(n + 1);
    memset(sizes, 0, sizeof(sizes));
    for (int i = 1; i <= 2 * n; i++) fa[i] = i;
    int idx = 0;
    for (int i = 1; i <= n; i++) {
        string str1, str2;
        int w;
        cin >> str1 >> str2 >> w;
        if (mp[str1] == 0) {
            mp[str1] = ++idx;
            ump[idx] = str1;
        }
        if (mp[str2] == 0) {
            mp[str2] = ++idx;
            ump[idx] = str2;
        }
        int a = mp[str1], b = mp[str2];
        sizes[a] += w, sizes[b] += w;
        if (find(a) > find(b)) {
            fa[find(a)] = find(b);
            weight[find(b)] += w;
        } else {
            fa[find(b)] = find(a);
            weight[find(a)] += w;
        }
    }
    //这一步卡在导数第二个测试点很久,注意一定要写的是find(i)而不是fa[i]
    for (int i = 1; i <= idx; i++) {
        if (weight[find(i)] > k)
            v[find(i)].push_back(i);
    }
    for (int i = 0; i < v.size(); i++) {
        if (v[i].size() <= 2) continue;
        string name = "";
        int maxx = 0;
        for (int j = 0; j < v[i].size(); j++) {
            if (sizes[v[i][j]] >= maxx) {
                if (sizes[v[i][j]] == maxx && name != "") {
                    name = min(name, ump[v[i][j]]);
                } else {
                    name = ump[v[i][j]];
                }
                maxx = sizes[v[i][j]];
            }
        }
        ans.push_back({name, v[i].size()});
    }
    cout << ans.size() << endl;
    sort(ans.begin(), ans.end(), cmp);
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}