PAT甲级 1034 Head of a Gang

博客围绕根据通话记录找出犯罪团伙及头目展开。给出若干人通话长度,定义组总边权和人点权,设定阈值 K,成员超 2 人则视为团伙,点权最大者为头目。要求输出团伙个数、头目姓名及成员人数,边稠密用邻接矩阵,用两个 map 做双向映射。

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

https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624

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### Aand### B, we say that### Aand### Bis 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 threthold### 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
whereName1andName2are the names of people at the two ends of the call, andTimeis the length of the call. A name is a string of three capital letters chosen fromA-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,则将该组视为“犯罪团伙(Gang)”,而该组内点权最大的人视为头目。
要求输出“犯罪团伙的个数”,并按头目名字字典序从小到大的顺序输出每个犯罪团伙头目姓名和成员人数

边稠密,用邻接矩阵
使用map做名字到编号与编号到名字的映射,双向映射不好做就用两个map

#include <iostream>
#include <map>
using namespace std;

const int maxn = 2010;

map<int, string> int2str;
map<string, int> str2int;
map<string, int> Gang;
int E[maxn][maxn], weight[maxn];
int n, k, tot;
bool vis[maxn] = {false};

// 当前访问编号、头目、成员数、总边权
void dfs(int cur, int &head, int &numMember, int &totValue)
{
    numMember ++;
    vis[cur] = true;
    if (weight[cur] > weight[head])
        head = cur;
    for (int i = 0; i < tot; i ++ )
    {
        if (E[cur][i] > 0)
        {
            totValue += E[cur][i];
            E[cur][i] = E[i][cur] = 0;
            if (!vis[i]) dfs(i, head, numMember, totValue);
        }
    }
}

void dfsTrave()
{
    for (int i = 0; i < tot; i ++)
    {
        if (!vis[i])
        {
            // 头目、成员数、总边权
            int head = i, numMember = 0, totValue = 0;
            dfs(i, head, numMember, totValue);
            if (numMember > 2 && totValue > k)
                Gang[int2str[head]] = numMember;
        }
    }
}
int change(string str)
{
    if(str2int.find(str)!=str2int.end()) 
    return str2int[str];
    else
    {
        str2int[str] = tot;
        int2str[tot] = str;
        return tot ++;
    }
}
int main()
{
    int w;
    string str1, str2;
    cin >> n >> k;
    for (int i = 0; i < n; i ++)
    {
        cin >> str1 >> str2 >> w;
        int id1 = change(str1);
        int id2 = change(str2);
        weight[id1] += w;
        weight[id2] += w;
        E[id1][id2] += w;
        E[id2][id1] += w;
    }
    dfsTrave();
    cout << Gang.size() << endl;
    for (auto &g : Gang) 
        cout << g.first << " " << g.second << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值