链接:https://www.nowcoder.com/questionTerminal/6572f529c966464c88ccf75cc0062a8c
来源:牛客网

One way that the police finds the head of a gang is to check people'sphone calls. If there is a phone call between A and B, we say that Aand B is related. The weight of a relation is defined to be the totaltime length of all the phone calls made between the two persons. A"Gang" is a cluster of more than 2 persons who are related toeach other with total relation weight being greater than a giventhreshold K. In each gang, the one with maximum total weight is thehead. Now given a list of phone calls, you are supposed to find thegangs and the heads.

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

输出

2
AAA 3
GGG 3

题目意思:

找到犯罪团伙的头目,犯罪团伙的头目是超过2个人的团伙,并且通话时间满足大于一个阈值,所以两个点之间的权重可以考虑为点权 (index,weight) 的形式。

思路:

DFS 每个连通子图,由于要求按名字的字母顺序来排序,所以考虑用map,因为map的key是排好序的(按字母升序)

还有一点,统一用name(string类型)来唯一地标记每一个人(节点)。

#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
//用map 的结构建立图中人名之间的对应关系
map<string,vector<string> >adjlist; // store the relationship with each person
map<string,int> weight;// store the weight each person
map<string,int> visited;// store the visited person
map<string,int> ans;// store the answer
int cnt,sum;
string head;void DFS(string str)
{visited[str]=1;cnt++;sum+=weight[str];if(weight[str]>weight[head])head=str;else if(weight[str]==weight[head]&& str<head)head=str;        vector<string>::iterator it;for(it=adjlist[str].begin();it!=adjlist[str].end();it++){if(visited[*it]==0)DFS(*it);}
}int main()
{int n,k;cin>>n>>k;for(int i=0;i<n;i++){string name1,name2;int time;cin>>name1>>name2>>time;adjlist[name1].push_back(name2);adjlist[name2].push_back(name1);visited[name1]=0;visited[name2]=0;weight[name1]+=time;weight[name2]+=time;}map<string,int>::iterator it;for(it=visited.begin();it!=visited.end();it++){if(visited[it->first]==0){cnt=0; // 对于每一个分量,初始化时候要注意对DFS中的sum 和个数 赋 0 sum=0;head=it->first;DFS(it->first); //遍历每个连通分量 if(cnt > 2 && sum/2>k)ans[head]=cnt;}}cout<<ans.size()<<endl;map<string,int>::iterator iter;for(iter=ans.begin();iter!=ans.end();iter++)cout<<iter->first<<" "<<iter->second<<endl;return 0;
}

PAT-Head of Hangs相关推荐

  1. PAT 甲级-算法初步

    阅读原文 接上一篇 PAT 甲级-入门模拟 ,自我感觉这部分才是真正的算法入门,对基础的数据结构提供了很好的类型题进行匹配练习 包括分类:排序.散列.贪心.二分.双指针.打表.递推 排序 思想解释 排 ...

  2. 【PAT甲级】A1001-A1050刷题记录

    文章目录 A1001 A+B Format (20 分) 0.25 ★(一元多项式加法) A1002 A+B for Polynomials (25 分) 0.21 (单源最短路Dijkstra+边权 ...

  3. PAT练习笔记——4.1 排序

    2019年9月PAT - 练习笔记--4.1 以下页码标注的是阅读器中实际页码,而不是书本身自印的页码. 第4章 入门篇(2)--算法初步 4.1 排序 注意 优先队列 头文件 < queue ...

  4. PAT甲级1001~1025

    PAT甲级1001~1025 1001 A+B Format (20 分) 1002 A+B for Polynomials (25 分) 1003 Emergency (25 分) 1004 Cou ...

  5. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

    PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025 ...

  6. 1093 Count PAT‘s

    这题出现在"活用递推"专题下面,所谓递推就是这一步的结果和上一步的结果有直接联系.对于本题来说,从左到右,记到当前位置,一共出现的P的个数,如果当前位置是P,则个数就是上一位的加1 ...

  7. 网络地址转换(PAT)

    一.静态NAT 1.实验环境 GNS3模拟软件 2.网络拓扑及IP地址规划如图 3. 通过配置PAT实现IP地址的多路复用,达到节省IP地址的目的 4.实验步骤(IP地址配置步骤省略) R1上的配置 ...

  8. PAT甲级(Advanced Level)真题--1046 Sharing

    PAT甲级(Advanced Level)真题–1046 Sharing 通过:648 提交:1138 通过率:56% To store English words, one method is to ...

  9. PAT甲级(Advanced Level)真题-- 1062 To Buy or Not to Buy

    PAT甲级(Advanced Level)真题-- 1062 To Buy or Not to Buy 通过:643 提交:1220 通过率:52% Eva would like to make a ...

  10. PAT甲级真题 1018 A+B in Hogwarts--python解法

    PAT甲级真题 1018 A+B in Hogwarts 提交:2638 通过:1559 通过率:59% If you are a fan of Harry Potter, you would kno ...

最新文章

  1. linux开了多个sendmail,Linux中的Sendmail问题及其解决办法
  2. python求列表最大值下标_切片,丝滑的字符串 | Python基础连载(三)
  3. 字符串转换成ascii码
  4. java 后台输出_将后台输出动态打输出到前台并且显示
  5. Oracle数据库应用系统结构
  6. excel 粘贴到web_使用EXCEL导入参考历年高考大数据,为2019年高考志愿填报做参考...
  7. Mac支持NTFS两款软件
  8. android studio或者IntelliJ代码样式的设置
  9. protues 快捷键和元件
  10. 关于“善念科技”的思考
  11. 计算机组成原理头歌实验
  12. python乘法函数_乘积(python乘法函数)
  13. 《炬丰科技-半导体工艺》 硅光电子器件上的单片砷化铟量子点
  14. 代码审计[java安全编程]
  15. Python操作Excel自动插入图片
  16. Java项目:SSM医院病历信息管理系统
  17. 什么是高耦合低内聚?
  18. css单行文本两端对齐
  19. 【植物大战僵尸-2】实现一炮秒杀僵尸
  20. Idea中Translation翻译插件失败问题解决

热门文章

  1. 爬虫学习笔记--爬取静态网页
  2. php mysql字符串截取比较读取_MySQL字符串截取 和 截取字符进行查询
  3. 快速打开命令行方法集合
  4. 爆款短视频速成技巧之视频发布篇,短视频介绍文案怎么写(下)
  5. excel插入行 uipath_UIPath入門系列三之操作Excel表格
  6. 优化篇-图片动态转图
  7. 定向光流直方图是什么_OpenCV计算机视觉编程攻略(第3版)
  8. 正大国际期货:做期货交易,怎么样才能成功?
  9. vue百度地图标记多个marker和marker点击事件处理
  10. ubuntu 20.04 | 常用软件 必要配置