首页 > 试题广场 >

Head of a Gang

[编程题]Head of a Gang
  • 热度指数:5619 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
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 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.

输入描述:
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.


输出描述:
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.
示例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
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

输出

2
AAA 3
GGG 3
0
头像 健康快乐最重要
发表于 2020-02-21 15:35:01
使用并查集和dfs都可以解决这个问题。如下是并查集解决的过程。相对于普通问题,主要是Union函数发生了变化 #include<iostream> #include<map> #include<string> using namespace std; //头是要点 展开全文
头像 Lucifer.Dark
发表于 2022-02-12 12:17:25
思路就是并查集,但是使用map构造 #include <iostream> #include <cstdio> #include <map> #include <set> #include <string> using namespace 展开全文
头像 Huster水仙
发表于 2023-02-03 22:35:35
并查集解决团队划分问题(连通集) 总结:主要思路还是并查集,并查集的操作模板化,只用修改结构体即可 个人结点信息较多,故用结构体; 团伙由于包含人数、头目名字,且要按照字典序输出,故也用结构体 个人结点:名字、通话时长 团伙结点:人数、头目名字 map<string,int>Geti 展开全文
头像 牛客120497586号
发表于 2022-04-12 10:39:16
实战 - Head of gang - 并查集 输入一串序列:Name1 Name2 Time表示A和B的通话时间,假定存在通话的人所在圈子大于2个人,认为是一个黑帮,在这个黑帮中,打电话时间最长的是老大 思路:使用并查集的思想构建节点的边关系,同时需要保存每个节点的通话时间 注意:①因为在初始化 展开全文
头像 猫猫有脾气
发表于 2020-04-04 14:31:29
我使用的并查集name1,name2,time;思路:用并查集将name1,name2合并,同时每个连通子图的根,负责记录本连通子图的总权重、总成员数量-----(于是我修改了Union函数),这里分为2种情况:(1)name1,name2所在的连通子图不是同一个(即需要合并)合并之后的本连通子图的 展开全文
头像 葱葱葱
发表于 2024-03-05 16:23:30
另辟蹊径,不使用并查集实现,但效率低下,代码复杂,仅供参考 #include <iostream> #include <map> #include <vector> using namespace std; struct team { int total 展开全文
头像 Coming680
发表于 2022-03-27 20:55:17
这题,麻烦的并查集问题,虽然能过,但代码又臭又长。。。 #include<iostream> #include<set> #include<map> #include<vector> #include<unordered_map> usin 展开全文
头像 黑乎乎的酱猪肉
发表于 2024-03-28 21:13:25
详细注释 #include <iostream> #include "string" #include "map" const int MAXN = 1000 + 10; using namespace std; struct Relation 展开全文
头像 generalll
发表于 2022-08-27 10:50:03
/* 这题有点复杂,思路: 1.先存下所有人的电话总时间,并且将他们分为集合; 2.遍历集合,得到集合的数量,记录集合标识 3.对于每个集合,遍历所有人判断他们是否属于这个集合,是则贡献集合的总weight,member++,且随时记录weight最大的人, 若最后weight>k*2且mem 展开全文
头像 loveC--
发表于 2024-03-16 16:50:59
超级无敌复杂的并查集,先将所有输入保存排序,再合并已实现权值最大为根节点 #include<iostream> #include<vector> #include<map> using namespace std; int father[27]; //用1到26 展开全文

问题信息

难度:
57条回答 6390浏览

热门推荐

通过挑战的用户

查看代码