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 threshold 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.

Input Specification:

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.

Output Specification:

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.

Sample Input 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

Sample Output 1:

2
AAA 3
GGG 3

Sample Input 2:

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

Sample Output 2:

0

题目分析:刚开始想怎么才能利用字符串来表示图 觉得得使用map或者类似哈希函数将字符转化为数字 但一直想不到什么好方法 看了柳神的博客后...哇 还可以这样
具体就是利用 两个map来存节点与对应的值 其实也就是将每一个值对应为一个数字 这种想法感觉是哈希函数的一种实现
最后利用dfs来遍历图 找到需要的解
注意的是对于每条存在的边都需要纳入计算 而每个节点只需要且只能遍历一次
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
map<string, int>stringtoint;
map<int, string>inttostring;
map<string, int>ans;
int N, K;
int number = ;
int G[][];
int weight[];
int Collected[];
int STOI(string s)
{
if (stringtoint[s] == )
{
stringtoint[s] = number;
inttostring[number] = s;
return number++;
}
else
return stringtoint[s];
}
void dfs(int u, int& head, int& totalnumber, int& totalweight)
{
Collected[u] = ;
totalnumber++;
if (weight[head] < weight[u])
head = u;
for (int v = ; v < number; v++)
{
if (G[u][v])
{
totalweight += G[u][v];
G[u][v] = G[v][u] = ;
if (!Collected[v])
dfs(v, head, totalnumber, totalweight);
}
}
}
int main()
{
cin >> N >> K;
string s1, s2;
int w;
for (int i = ; i < N; i++)
{
cin >> s1 >> s2 >> w;
G[STOI(s1)][STOI(s2)]+=w;
G[STOI(s2)][STOI(s1)]+=w;
weight[STOI(s1)] += w;
weight[STOI(s2)] += w;
}
for (int i = ; i < number; i++)
{
int head = i;
int totalnumber = ;
int totalweight = ;
if (!Collected[i])
dfs(i, head, totalnumber, totalweight);
if (totalnumber > && totalweight > K)
ans[inttostring[head]] = totalnumber;
}
cout << ans.size() << endl;
for (auto it : ans)
cout << it.first << " " << it.second << endl;
return ;
}
 

1034 Head of a Gang (30分)(dfs 利用map)的更多相关文章

  1. 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 ...

  2. 【PAT甲级】1034 Head of a Gang (30 分)

    题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...

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

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

  4. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  5. 1034. Head of a Gang (30)

    分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...

  6. 1034 Head of a Gang (30)(30 分)

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

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

  8. PAT 1034. Head of a Gang (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...

  9. 1034. Head of a Gang (30) -string离散化 -map应用 -并查集

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

随机推荐

  1. 19.10.11学习日记随笔 mysql事务隔离性

    一天的感悟 学习事务的处理方式,其中反想自己学过的flask 默认是开启事务的,flask_sqlalchemy每次在提交时都是需要commit,或者失败是需要rollback回滚操作的,其实pyth ...

  2. 修改js文件,引发的404问题

    记录一个bug,本地测不出来,客户后台却404,web测试可参考.(不知道是不是通用的) 先介绍下背景 我们是web产品,存在发布机.管理机.js文件,页面会引用到这些js文件.出于安全考虑,规定js ...

  3. Elasticsearch构建全文搜索系统

    目录 前言 一.安装 1.安装elasticsearch 2.启动集群cluster 3.安装管理界面elasticsearch-head 4.安装分词插件elasticsearch-analysis ...

  4. HTML5&CCS3(3)基本HTML结构

    3.1 开始编写网页 每个HTML文档都应该包含以下基本成分: DOCTYPE: html元素(包含lang属性.该属性不是必需的,但推荐加上): head元素: 说明字符编码的meta元素: tit ...

  5. Asp.net textbox 控件 的 onTextChange 事件

    <asp:TextBox ID="txt_MailType" runat="server" OnTextChanged="exitMailTyp ...

  6. MySQL windows中的存储备份

    数据备份对于经常在运维部署方面的工作者来说,是一件相对简单的事情,都可以通过某一个SQL工具进行备份,但是如果在项目运行当中,我们需要对数据进行实时,或者是每隔一星期,一个月,等等进行数据的备份,这样 ...

  7. iOS开发:十六进制颜色转UIColor

    Objective-C UIColor * __nullable UIColorFromHexValue(NSUInteger hexValue) { CGFloat red = (hexValue ...

  8. Web_XML

    第1章 XML简介 “当 XML(扩展标记语言)于 1998 年 2 月被引入软件工业界时,它给整个行业带来了一场风暴.有史以来第一次,这个世界拥有了一种用来结构化文档和数据的通用且适应性强的格式,它 ...

  9. 趣学Spring:一文搞懂Aware、异步编程、计划任务

    你好呀,我是沉默王二,一个和黄家驹一样身高,刘德华一样颜值的程序员(不信围观朋友圈呗).从 2 位偶像的年纪上,你就可以断定我的码龄至少在 10 年以上,但实话实说,我一直坚信自己只有 18 岁,因为 ...

  10. 【codeforces】Educational Codeforces Round 80 D. Minimax Problem——二分+二进制处理

    题目链接 题目大意 有n个维度为m的向量,取其中两个进行合并,合并时每个维度取两者之间的较大者,得到的新的向量中,维度值最小者最大为多少 分析 首先最需要注意的是m的取值,m最大只有8 那么我们可以二 ...