1034 Head of a Gang (30分)(dfs 利用map)
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)的更多相关文章
- 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 ...
- 【PAT甲级】1034 Head of a Gang (30 分)
题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=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 ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 1034. Head of a Gang (30)
分析: 考察并查集,注意中间合并时的时间的合并和人数的合并. #include <iostream> #include <stdio.h> #include <algor ...
- 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 ...
- 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 ...
- PAT 1034. Head of a Gang (30)
题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...
- 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 ...
随机推荐
- 黑科技神器-uTools
Hello,各位小伙伴们好,又到周末了,小黑哥给大家分享一款神器:『utools』. 官网地址:https://u.tools/ uTools 是一个极简.插件化.跨平台的现代桌面软件.通过自由选配丰 ...
- 建议5:比较function语句和function表达式
在Javascript语言中,既有function语句,也有函数表达式,这是令人困惑的.因为它们看起来是相同的.一个function语句就是一个值为一个函数的var语句的简写形式. 下面语句: fun ...
- $api 回调函数then应用
getReceiveListAPI (param) { return new Promise(resolve => { let params = { // idCard: this.idCard ...
- 关于WDK开发内核签名之WHQL签名认证流程简介
WHQL简介: WHQL全称Windows Hardware Quality Labs testing(Windows硬件质量实验室测试)它是对所涉及的第三方硬件或软件进行一系列测试,然后提交测试的日 ...
- 【Weiss】【第03章】练习3.4、3.5:有序链表求交、并
[练习3.4] 给定两个已排序的表L1和L2,只使用基本的表操作编写计算L1∩L2的过程. [练习3.5] 给定两个已排序的表L1和L2,只使用基本的表操作编写计算L1∪L2的过程. 思路比较简单,测 ...
- 个人项目(Word Count)
一.Github项目地址 https://github.com/AllForward/GP_Homework/tree/master/个人项目 二.题目叙述 这个项目要求写一个命令行程序,模仿已有wc ...
- nested exception is java.lang.StackOverflowError解析
背景介绍: 项目是微服务的,使用docker容器,使用jenkins部署.测试环境有个公共服务一直以来都能正常发布,突然有一天不行了,经常发布失败,然后多发布几次就好了. 报错如下: 是栈溢出了,一般 ...
- Core + Vue 后台管理基础框架8——Swagger文档
1.前言 作为前后端分离的项目,或者说但凡涉及到对外服务的后端,一个自描述,跟代码实时同步的文档是极其重要的.说到这儿,想起了几年前在XX速运,每天写完代码,还要给APP团队更新文档的惨痛经历.给人家 ...
- mysql8 修改root密码
Navicat工具里选中mysql数据库 执行: ALTER user 'root'@'localhost' IDENTIFIED BY 'newpassward'; //newpassward 新密 ...
- iOS开发:Swift/Objective-C高效生成随机字符串
原文连接 Objective-C版 // 随机生成字符串(由大小写字母.数字组成) + (NSString *)random: (int)len { char ch[len]; for (int in ...