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. u-boot启动第二阶段以及界面命令分析

    u-boot第一阶段完成了一些平台相关的硬件的配置,第一阶段所做的事情也是为第二阶段的准备,我们知道在第一阶段最后时搭建好C运行环境,之后调用了start_armboot(),那么很显然第二阶段从st ...

  2. Java开发必用的工具包

    Java是最流行的开源语言之一. 有赖于Java的开源,涌现出一大批优秀的开源框架,基本涵盖了开发中的方方面面,让程序员可以专注于自己的业务逻辑. ​ 今天,我们就来聊聊在开发中,经常被我们所忽略的[ ...

  3. git clone的

    git clone git@e.coding.net:wudi360/*******.git

  4. Webmin忘记密码解决方法,及配置文件介绍

    Webmin忘记Web登陆时候的密码,无法登陆了,Google了一下,基本方法是通过changepass.pl可以修改密码 首先找到changepass.pl这个文件目录 $sudo locate c ...

  5. sql函数应用例子

    select p.province, data.existUserCount, data.addUserCount, data.cancelUserCount, data.threedayCancel ...

  6. Django 中form的用法

    form的主要作用:1.在html中生成表单框架,2.验证数据(实话实说,很简洁,但不实用,灵活性差) from django.db import models # Create your model ...

  7. Pylint在项目中的使用

    需求背景: Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码. Pylint 是一个 Python 工具,除了平常代码分析 ...

  8. 服务器或普通PC裸机安装 ESXI6.5

    ESXI :安装包 http://pan.baidu.com/s/1c2gM0Xq (包含注册机和其他套件,驱动打包工具) ESXI 6.5 在服务器安装比较方便,一般intel 的网卡都没多大问题, ...

  9. kubeadm高可用master节点部署文档

    kubeadm的标准部署里,etcd和master都是单节点的. 但上生产,至少得高可用. etcd的高可用,用kubeadm微微扩散一下就可以. 但master却官方没有提及. 于是搜索了几篇文档, ...

  10. python定制类(1):__getitem__和slice切片

    python定制类(1):__getitem__和slice切片 1.__getitem__的简单用法: 当一个类中定义了__getitem__方法,那么它的实例对象便拥有了通过下标来索引的能力. c ...