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. Eureka停更了?试试Zookpper和Consul

    在Spring Cloud Netflix中使用Eureak作为注册中心,但是Eureka2.0停止更新,Eureka1.0 进入了维护状态.就像win7一样,同样可以用,但是官方对于新出现的问题并不 ...

  2. SpringBoot是如何实现自动配置的?--SpringBoot源码(四)

    注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 助力SpringBoot自动配置的条件注解ConditionalOnXXX分析--SpringBoot源码(三 ...

  3. flask修改flask_wtf使其支持json数据的validation验证

    flask默认是前后端不分离策略,前端通过flask+wtf表单来传递post,put...等数据. 现在前后端分离是趋势,那么对flask进行一定的修改,变为前后端分离,在前端页面中请求后端,那么请 ...

  4. 个人项目作业(wc.exe)

    1.GitHub项目地址 https://github.com/QiuBin666/WC 项目介绍: 题目描述 Word Count1. 实现一个简单而完整的软件工具(源程序特征统计程序).2. 进行 ...

  5. celery异步任务 定时任务

    以前项目中用到过 celery ,但是没怎么记笔记,现在在记一下,方便以后用.   Celery.png 问:Celery 是什么? 答:Celery 是一个由 Python 编写的简单.灵活.可靠的 ...

  6. Python模块三

    collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict. ...

  7. Js中的window.parent ,window.top,window.self 了解

    在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...

  8. Python生态_turtle库

    Python生态_turtle库: 绘制状态函数: pendown():别名pd(),落下画笔,之后,移动画笔将绘制形状 penup():抬起画笔 pensize():画笔粗细大小 颜色控制函数: p ...

  9. 【Weiss】【第03章】练习3.13:桶排序

    [练习3.13] 利用社会安全号码对学生记录构成的数组排序.编写一个程序进行这件工作,使用具有1000个桶的基数排序并且分三趟进行. Answer: 首先,对社会安全号码不了解的就把它当成一个不超过9 ...

  10. Fluent算例精选|02瞬态滑移网格分析叶轮机械内部流动

    本算例使用的软件:fluent.icem 通过学习本算例您将获得? 1.学会周期区域创建 2.学会瞬态求解器及滑移网格边界条件设置 3.学会周期面.滑移面设置 4.学会如何监测压力脉动(声学仿真) 5 ...