1034 Head of a Gang

通过并查集算法解决帮派成员识别及头目寻找的问题,输入为一系列电话通话记录,输出帮派总数及各帮派头目的姓名与成员数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

解题思路:一道并查集的题目,名字用hash的办法变成int类型,所以开个26*26*26的s数组就行。用一个数组记录每个人总的通话时间,用于最后找出head。并查集找出团队,再在团队中找到通话时间最长的人就是结果。因为结果需要按字母序输出,所以我先将结果保存下来,排序之后再输出的。
做题的时候并查集的压缩路径的地方写错了,导致测试点2 4一直答案错误,还是太粗心。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int group[26 * 26 * 26];
int ti[26 * 26 * 26];
vector<int> groupCount[26 * 26 * 26];
struct res{
    int name;
    int size;
};
bool cmp(const res&r1, const res&r2){
    return r1.name < r2.name;
}
int find(int x){
    int res = x;
    while (group[res] != res){
        res = group[res];
    }
    while (x != res){
        int temp = group[x];
        group[x] = res;
        x = temp;
    }
    return res;
}
void join(int x, int y){
    int fx = find(x);
    int fy = find(y);
    if (fx != fy){
        group[fy] = fx;
    }
}
int main(){
    for (int n, k; scanf("%d%d", &n, &k) != EOF;){
        for (int i = 0; i < 26 * 26 * 26; i++){
            group[i] = i;
            ti[i] = 0;
            groupCount[i] = vector<int>();
        }
        for (int i = 0; i < n; i++){
            char p1[4];
            char p2[4];
            int temp;
            scanf("%s %s %d", p1, p2, &temp);
            int index1 = (p1[0] - 'A') * 26 * 26 + (p1[1] - 'A') * 26 + (p1[2] - 'A');
            int index2 = (p2[0] - 'A') * 26 * 26 + (p2[1] - 'A') * 26 + (p2[2] - 'A');
            join(index1, index2);
            ti[index1] += temp;
            ti[index2] += temp;
        }
        for (int i = 0; i < 26 * 26 * 26; i++){
            find(i);
        }
        for (int i = 0; i < 26 * 26 * 26; i++){
            if (ti[i]>0){
                groupCount[group[i]].push_back(i);
            }
        }
        vector<res>out;
        for (int i = 0; i < 26 * 26 * 26; i++){
            if (groupCount[i].size() > 2){
                int name = groupCount[i][0];
                int maxTime = ti[groupCount[i][0]];
                int totalTime = 0;
                for (int j = 0; j < groupCount[i].size(); j++){
                    totalTime += ti[groupCount[i][j]];
                    if (ti[groupCount[i][j]] >= maxTime){
                        maxTime = ti[groupCount[i][j]];
                        name = groupCount[i][j];
                    }
                }
                if (totalTime > k*2){
                    res temp;
                    temp.name = name;
                    temp.size = groupCount[i].size();
                    out.push_back(temp);
                }
            }
        }
        sort(out.begin(), out.end(), cmp);
        printf("%d\n", out.size());
        for (int i = 0; i < out.size(); i++){
            printf("%c%c%c %d\n", (char)(out[i].name / (26 * 26) + 'A'), (char)((out[i].name % (26 * 26)) / 26 + 'A'), (char)(out[i].name % 26 + 'A'), out[i].size);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值