【PAT甲级】1034 Head of a Gang

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1034 Head of a Gang (pintia.cn)
🔑中文翻译:团伙头目
📣专栏定位:为想考甲级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
题意

警察找到团伙头目的一种方法是检查人们的通话。

如果 AB 之间有通话,我们就说 AB 是相关的。并且关联具有传递性,即如果 AB 关联,BC 关联,那么 AC 也是关联的。

关联权重定义为两人之间所有通话的总时间长度。

一个“帮派”是一个由至少 3 个相互关联的人组成的群体,并且其总关联权重大于给定的阈值 K

在每个帮派中,总权重最大的就是头目,数据保证每个帮派中总权重最大的人是唯一的。

你需要确定各个帮派以及帮派头目。

思路

这道题可以将所有人的关系网当成图,每个有联系的人之间双向连通,并且由于可能存在不同帮派,这个图可能会分成多个连通块,具体思路如下:

  1. 首先,我们可以用哈希表 g 来存储所有有联系人的信息,key 表示该人名字,value 是一个容器,存储与该人通话的另一个人的名字已经通话时长。另外,可以再用一个哈希表 total 来存储每个人的通话时长。
  2. 遍历每个人,如果该人没被遍历过,就去计算其所在连通块中所有人员的通话总时长,并用一个容器将所有人员的名字记录下来。注意,最终获得的总通话时长要除以 2 ,因为 g 中记录的是双向的边,故每个边都会被计算两次。
  3. 判断该连通块是否满足帮派条件,一个帮派至少要有两个人且帮派中总通话时长要大于 k 。如果是帮派,还需要找出该帮派中的头目,头目是该帮派中通话时间最长的人。
  4. 先输出帮派的数目,然后依次输出每个帮派的信息,注意需要按照头目名字的字典序输出。
代码
#include<bits/stdc++.h>
using namespace std;

int n, k;
unordered_map<string, vector<pair<string, int>>> g;
unordered_map<string, int> total;
unordered_map<string, bool> st;

//找到该人所在连通块的所有成员
int dfs(string ver, vector<string>& nodes)
{
    nodes.push_back(ver);
    st[ver] = true;

    int sum = 0;  //计算总边权
    for (auto& it : g[ver])    //遍历与其相关的其他人
    {
        sum += it.second;
        string cur = it.first;
        if (!st[cur])    sum += dfs(cur, nodes);
    }

    return sum;
}

int main()
{
    cin >> n >> k;
    for (int i = 0; i < n; i++)    //输入通话信息
    {
        string a, b;
        int t;
        cin >> a >> b >> t;
        g[a].push_back({ b,t });
        g[b].push_back({ a,t });
        total[a] += t, total[b] += t;
    }

    vector<pair<string, int>> res;
    for (auto& it : total)
    {
        string ver = it.first;
        if (st[ver]) continue;	//如果该人已经计算过,说明该连通块已被计算,直接跳过
        
        vector<string> nodes;
        int sum = dfs(ver, nodes) / 2; //获取总边权以及该连通块所有人

        if (nodes.size() > 2 && sum > k)   //如果满足帮派要求
        {
            string boss = nodes[0];   //找到该帮派的头目
            for (auto& no : nodes) //总通话时间最长的就是头目
                if (total[no] > total[boss])
                    boss = no;
            res.push_back({ boss,nodes.size() });
        }
    }

    sort(res.begin(), res.end());    //按头目名字字典序排序

    //输出答案
    cout << res.size() << endl;
    for (auto& it : res)
        cout << it.first << " " << it.second << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值