【PAT甲级】1034 Head of a Gang

简介: 【PAT甲级】1034 Head of a Gang

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

题意

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


如果 A 和 B 之间有通话,我们就说 A 和 B 是相关的。并且关联具有传递性,即如果 A 与 B 关联,B 与 C 关联,那么 A 与 C 也是关联的。


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


一个“帮派”是一个由至少 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;
}


目录
相关文章
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17120 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
6天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1284 3
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2356 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3831 22
|
20天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。