【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;
}


目录
相关文章
|
存储 Kubernetes 网络协议
【K8S系列】深入解析有状态服务
【K8S系列】深入解析有状态服务
518 1
|
人工智能 自然语言处理 搜索推荐
AI原生企业级Agent构建平台具备哪些特性?一篇文章看明白
AI原生企业级Agent构建平台有哪些特性?澜码正式发布AskXBOT平台为业界揭晓答案。
994 0
|
11月前
|
消息中间件 运维 监控
从开源到创业:掌握 Websoft9 托管平台上的开源工具,就业到创业的路径
在云原生与低代码技术驱动下,开源工具已成为企业数字化转型的核心引擎。本文以Websoft9(集成200+开源应用)为案例,探讨从技术学习到商业实践的完整路径。内容分为四个阶段:技术筑基(场景化部署)、业务解构(需求洞察)、创业孵化(MVP构建与验证)及规模化扩张(架构升级与商业化)。通过低成本部署、数据驱动优化及生态共建,展示开源工具如何助力个体与团队实现能力跃迁和商业创新,证明开源是技术自由与商业加速的双重杠杆。
156 0
|
Java 开发者 UED
【揭秘Java编程新境界】事件驱动:如何在Java中捕捉每一个关键瞬间?
【8月更文挑战第30天】事件驱动编程是一种编程范式,使程序能在事件发生时响应,而非按严格顺序执行。本文介绍Java中的事件驱动编程,包括基本概念、优势及其实现方法。通过事件监听器和事件对象,Java能够高效处理GUI、网络编程和游戏开发中的各种事件。文中还提供了创建事件监听器、自定义事件及处理多个事件源的示例代码,帮助读者更好地理解和应用这一强大的编程范式。
336 1
|
敏捷开发 运维 Prometheus
构建高效运维体系:从基础架构到自动化管理
本文探讨了如何通过优化基础架构、引入自动化工具和流程,以及加强团队协作,构建高效的运维体系。通过案例分析和实践建议,帮助运维人员实现系统的稳定性、可靠性和可维护性。
354 22
|
人工智能
在stable diffussion中完美修复AI图片
无论您的提示和模型有多好,一次性获得完美图像的情况很少见。修复小缺陷的不可或缺的方法是图像修复(inpainting)
在stable diffussion中完美修复AI图片
|
人工智能 自然语言处理 并行计算
【AI大模型】Transformers大模型库(六):torch.cuda.OutOfMemoryError: CUDA out of memory解决
【AI大模型】Transformers大模型库(六):torch.cuda.OutOfMemoryError: CUDA out of memory解决
1163 0
【AI大模型】Transformers大模型库(六):torch.cuda.OutOfMemoryError: CUDA out of memory解决
|
监控 Cloud Native 测试技术
云原生之使用Docker部署ServerBee服务器监控工具
【5月更文挑战第6天】云原生之使用Docker部署ServerBee服务器监控工具
311 3
Mybatis-Plus实现Service封装
Mybatis-Plus实现Service封装
589 1
|
设计模式
带你造轮子,自定义一个随意拖拽可吸边的悬浮View组件
在开发中,随意拖拽可吸边的View还是比较常见的,这种功能网上也有各种各样的轮子,其实写起来并不复杂,看完本文,你也可以手写一个,不到400行代码就能实现一个通用的随意拖拽可吸边的View组件。
955 1

热门文章

最新文章