PAT甲级1034. Head of a Gang

题意:

警方找到一个帮派的头的一种方式是检查人民的电话。如果A和B之间有电话,我们说A和B是相关的。关系的权重被定义为两人之间所有电话的总时间长度。

“帮派”是超过2人的群体,彼此相关,总关系权重大于给定的阈值K.在每个帮派中,最大总重量的人是头。现在给了一个电话列表,你应该找到帮派和头。

输入规格:

每个输入文件包含一个测试用例。

对于每种情况,第一行分别包含两个正数N和K(均小于或等于1000),电话号码和权重。然后N行遵循以下格式:

Name1 Name2时间

其中Name1和Name2是呼叫两端的人员的姓名,“时间”是呼叫的长度。

一个名字是从A-Z中选出的三个大写字母的字符串。时间长度为不超过1000分钟的正整数。

输出规格:

对于每个测试用例,首先在一行中列出组合的总数。然后对于每个帮派,一行打印头的名字和成员的总数。

保证每个帮派的头是独一无二的。输出必须按照头部名称的字母顺序进行排序

思路:

就是相当于找有几个符合gang条件的连通分量。要求分量member2个以上。总共通话时间为k以上。dfs可以求解。

ac代码:

C++

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<string>
  4. #include<algorithm>
  5. #include<queue>
  6. #include<vector>
  7. #include<cstring>
  8. #include<stdio.h>
  9. #include<unordered_map>
  10. #include<map>
  11. using namespace std;
  12. int n, k;
  13. unordered_map<string, int> timecount;
  14. unordered_map<string, vector<string> > link;
  15. map<string, int> out;
  16. map<string,bool> flag;
  17. int idx, allweight;
  18. string st;
  19. void dfs(string str)
  20. {
  21. flag[str] = true;
  22. allweight += timecount[str];
  23. if (timecount[str] > timecount[st]) st = str;
  24. for (int i = 0; i < link[str].size(); i++)
  25. {
  26. if (!flag[link[str][i]])
  27. {
  28. dfs(link[str][i]);
  29. }
  30. }
  31. idx++;
  32. }
  33. int main()
  34. {
  35. cin >> n >> k;
  36. char a[5], b[5];
  37. int time;
  38. while (n--)
  39. {
  40. scanf("%s %s %d", a, b, &time);
  41. string aa = string(a), bb = string(b);
  42. if (timecount.find(aa) == timecount.end()) timecount[aa] = 0;
  43. if (timecount.find(bb) == timecount.end()) timecount[bb] = 0;
  44. timecount[aa] += time;
  45. timecount[bb] += time;
  46. link[aa].push_back(bb);
  47. link[bb].push_back(aa);
  48. flag[aa] = false;
  49. flag[bb] = false;
  50. }
  51. for (auto it = flag.begin(); it != flag.end(); it++)
  52. {
  53. if (it->second == false)
  54. {
  55. idx = 0;
  56. allweight = 0;
  57. st = it->first;
  58. dfs(st);
  59. if (idx > 2 && allweight / 2 > k)
  60. out[st] = idx;
  61. }
  62. }
  63. cout << out.size() << endl;
  64. for (auto it = out.begin(); it != out.end(); it++)
  65. cout << it->first << " " << it->second << endl;
  66. return 0;
  67. }

PAT甲级1034. Head of a Gang的更多相关文章

  1. pat 甲级 1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

  2. PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

    1034 Head of a Gang (30 分)   One way that the police finds the head of a gang is to check people's p ...

  3. pat 甲级 1034 ( Head of a Gang )

    1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's pho ...

  4. PAT甲级1034 Head of a Gang【bfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 题意: 给定n条记录(注意不是n个人的 ...

  5. PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

    题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

  6. PAT 1034 Head of a Gang[难][dfs]

    1034 Head of a Gang (30)(30 分) One way that the police finds the head of a gang is to check people's ...

  7. PAT 1034. Head of a Gang

    1034. Head of a Gang (30) One way that the police finds the head of a gang is to check people's phon ...

  8. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

随机推荐

  1. 夜神安卓模拟器adb命令详解

    https://www.yeshen.com/faqs/H15tDZ6YW 一.如何找到adb? 安装夜神安卓模拟器后,电脑桌面会有"夜神模拟器"的启动图标,鼠标右键--打开文件所 ...

  2. AspNet Core 发布到Linux系统和发布IIS 注意项

    AspNet Core 发布到Linux系统和发布IIS 注意项 1.发布时需要注意的 2.Windows Server 2012 api-ms-win-crt-runtime-l1-1-0.dll ...

  3. 3.rabbitmq 发布/订阅

    1. 发布者 #coding:utf8 import pika import json import sys message = ''.join(sys.argv[1:]) or "hell ...

  4. CSU 1356 Catch

    原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1356 题目需要我们判断给定图在某一步是否会有可能出现在所有节点.首先,我们不妨假设给定图 ...

  5. 在JAVA中生成RSA秘钥对实现SSH互信

    https://blog.csdn.net/u014196729/article/details/51496262 https://blog.csdn.net/u013066244/article/d ...

  6. Python写网络爬虫爬取腾讯新闻内容

    最近学了一段时间的Python,想写个爬虫,去网上找了找,然后参考了一下自己写了一个爬取给定页面的爬虫. Python的第三方库特别强大,提供了两个比较强大的库,一个requests, 另外一个Bea ...

  7. [前端神器]handlebars+requirejs基本使用方法

    最近在某网站看到了handlebars.js,出于好奇就百度了下这是神马玩意,结果让我很是欢喜,于是就开始自学下,handlebars就几个方法,蛮简单,言归正传! 以下是基本教学逻辑演示,会附完整代 ...

  8. [转]基于Protel DXP软件的PCB高级编辑技巧大全

    来源:基于Protel DXP软件的PCB高级编辑技巧大全 一.放置坐标指示 放置坐标指示可以显示出PCB板上任何一点的坐标位置. 启用放置坐标的方法如下:从主菜单中执行命令 Place/Coordi ...

  9. loadrunner中并发数与迭代的区别

    你的理解的虚拟用户应该是 迭代次数 ,录制脚本时只会有1个虚拟用户,1个虚拟用户可以有多次 迭代,也就是 重复执行 Action里面的内容,在场景设置的时候,如果你说的10时在runtime-sett ...

  10. LeetCode 118. 杨辉三角

    118. 杨辉三角 给定一个非负整数numRows,生成杨辉三角的前numRows行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例 输入: 5 输出: [ [1], [1,1], [1,2 ...