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++

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<unordered_map>
#include<map> using namespace std; int n, k;
unordered_map<string, int> timecount;
unordered_map<string, vector<string> > link;
map<string, int> out;
map<string,bool> flag;
int idx, allweight;
string st; void dfs(string str)
{
flag[str] = true;
allweight += timecount[str]; if (timecount[str] > timecount[st]) st = str;
for (int i = 0; i < link[str].size(); i++)
{
if (!flag[link[str][i]])
{
dfs(link[str][i]);
}
}
idx++;
} int main()
{
cin >> n >> k;
char a[5], b[5];
int time;
while (n--)
{
scanf("%s %s %d", a, b, &time);
string aa = string(a), bb = string(b);
if (timecount.find(aa) == timecount.end()) timecount[aa] = 0;
if (timecount.find(bb) == timecount.end()) timecount[bb] = 0;
timecount[aa] += time;
timecount[bb] += time; link[aa].push_back(bb);
link[bb].push_back(aa); flag[aa] = false;
flag[bb] = false;
} for (auto it = flag.begin(); it != flag.end(); it++)
{
if (it->second == false)
{
idx = 0;
allweight = 0;
st = it->first;
dfs(st);
if (idx > 2 && allweight / 2 > k)
out[st] = idx;
}
} cout << out.size() << endl;
for (auto it = out.begin(); it != out.end(); it++)
cout << it->first << " " << it->second << endl;
return 0;
}

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. imperva系统升级遇见的错误(配置文件的导入导出)

    今天心态有点炸了 今天去东兴证券做waf升级.浪费了两天才弄完.把客户都弄得有点急了.好歹原厂的工程师耐心的讲解这才弄完.感谢路哥.... 赶紧总结一下. 事情是这样的.东兴 证券的imperva是v ...

  2. numpy 简介

    .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...

  3. 在新版linux上编译老版本的kernel出现kernel/timeconst.h] Error 255

    在使用ubuntu16.4编译Linux-2.6.31内核时出现这样的错误 可以修改timeconst.pl的内容后正常编译. 以下是编译错误提示的内容: Can't use 'defined(@ar ...

  4. SurfaceFlinger 讲解

    SurfaceFlinger是Android multimedia的一个部分,在Android 的实现中它是一个service,提供系统 范围内的surface composer功能,它能够将各种应用 ...

  5. uWSGI+Nginx+Flask在Linux下的部署

    搞了一天多,终于搞通了uWSGI的部署原理,下面总结一下遇到的一些坑,希望给读者能够少走弯路.        简单来说,uWSGI是一个web服务器,Nginx进行反向代理的其实跟这些服务器可以说没有 ...

  6. Spring中的@Transactional事务注解

    事务注解方式 @Transactional 当标于类前时, 标示类中所有方法都进行事物处理 , 例子: @Transactional public class TestServiceBean impl ...

  7. java版云笔记(二)

    云笔记 基本的环境搭建好了,今天做些什么呢,第一是链接数据库(即搭建Spring-Batistas环境),第二是登录预注册. 注:这个项目的sql文件,需求文档,需要的html文件,jar包都可以去下 ...

  8. 数据库连接池(c3p0与druid)

    1.数据库连接池概念 其实就是一个容器(集合),存放数据库连接的容器.当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归 ...

  9. 完全禁用Wordpress的升级功能

    wordpress自己带有一个自动升级的功能,也就是说,如果wp检测到官方已经有新的升级可用的话他就会自己升级上去.这可能对于某些场合是个不错的功能,但是对于一些已经对系统大量魔改或者对插件稳定性不抱 ...

  10. Visual Studio 中如何同时注释多行和取消注释多行

    注释多行:先按 Ctrl - K 组合键,再按 Ctrl - C 组合键 取消注释多行:先按 Ctrl - K 组合键,再按 Ctrl - U 组合键