1034 Head of a Gang (30 point(s))

政府科技管理者如何提升科技成果转化项目进场交易的备案效率?.docx 政府科技管理者如何提升科技成果转化项目进场交易的备案效率? 立即下载

1034 Head of a Gang (30 point(s))

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

考察点:并查集的灵活运用

题意:

给出一组人的通话记录(两个参与人以及时长), 一个帮派的定义是其中的一个簇,它至少有2个人且它们的总共通话时间超过阈值K,其中通话时间最长的是帮派的老大。求出每个帮派的人数以及老大。

分析(问题抽象):

直观地看,通话记录可以看成图,这是一个帮派就是一个连通分支,人数是这个连通分支的结点数,总通话时间是分支内路径的权重和。帮派内拥有最多边权重的结点是老大。

因此Tree[]记录父亲结点(对于根节点Tree[root]==-1;初始化为-1,即每个结点都是根节点)。在并查集的基础上,num[]记录每个帮派的人数(初始化为1,每个帮派都是1人,当两个簇合并的时候需要更新),total[]记录每个帮派的通话时长(初始化为0,当两个簇合并的时候需要更新)。

注意到最终每个帮派的根节点,并不一定是老大(通话时长最多的)。因此还需要维护数组w[]记录每个人的通话时间。在最终判断的时候,找出每个分支的人,进而找出老大。

注意点:

1. memset不可以将数组初始化为1,只能初始化为0(内存为0x00000000)或者-1(内存为0xffffffff)或者ASCii为0~255的字符。

2. 利用数组和map<string,int>mp 实现字符和数组的相互转换。

3. 在遍历边的时候注意,代码中Tree[na]=nb意味着结点nb是na的父节点了,因此更新时total[]和num[]都应该加在nb上。

并查集的题目都可以用DFS完成,参见https://blog.youkuaiyun.com/richenyunqi/article/details/79575328

#include<iostream>
#include<cstring>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX = 2007;
int Tree[MAX];
int w[MAX];//每个成员的权重 
int total[MAX];//帮派的权重 
int num[MAX];//帮派人数 
int findRoot(int x){
	if(Tree[x]==-1) return x;
	else{
		int temp = findRoot(Tree[x]);
		Tree[x] = temp;
		return temp;
	}
} 
struct Gang{
	string headName;int n;
	Gang(string h,int num):headName(h),n(num){}
	bool operator < (const Gang &other) const{
		return headName<other.headName;
	}
};
string name[MAX];
map<string,int> mp;//用于编号和姓名之间相互转换 
int main(void){
	int N,K;cin>>N>>K;
	for(int i=1;i<=MAX;i++){//memset不可以赋值1 !!! 
		num[i]=1;
	}
	memset(Tree,-1,sizeof(Tree));
	memset(w,0,sizeof(w));
	memset(total,0,sizeof(total)); 
	string s1,s2;int time;
	int idx = 1;//从1开始编号 
	while(N--){
		cin>>s1>>s2>>time;
		int a,b;
		if(mp.find(s1)==mp.end()){//如果没有出现过这个名字,赋予它数字编号 
			a = idx;
			mp.insert({s1,a});
			name[a]=s1;
			idx++;
		}
		else a = mp[s1];//否则直接从map中获得数字编号 
		if(mp.find(s2)==mp.end()){
			b = idx;
			mp.insert({s2,b});
			name[b]=s2;
			idx++;
		}
		else b = mp[s2];
		w[a]+=time; w[b]+=time;
		int na = findRoot(a);//返回根节点的编号,应当清楚Tree[na/nb] 
		int nb = findRoot(b);
		if(na!=nb){ 
			Tree[na] = nb;//na的父节点是nb 
			total[nb]+=(total[na]+time);//因此总的时间要加到total[nb]上 
			num[nb]+=num[na];//同上 
		}
		else{
			total[nb]+=time; 
		}
	}
	vector<Gang> G; 
	for(int i=1;i<=idx;i++){
		if(Tree[i]==-1&&total[i]>K&&num[i]>2){//如果帮派满足要求 
			int head = i;int maxCall = w[i];
			for(int j=1;j<=idx;j++){//寻找老大 
				if(findRoot(j)==i&&w[j]>maxCall){
					maxCall = w[j];
					head = j;
				}
			}
			G.push_back(Gang(name[head],num[i]));
		}
	}
	sort(G.begin(),G.end());
	cout<<G.size()<<endl;
	for(int i=0;i<G.size();i++){
		cout<<G[i].headName<<" "<<G[i].n<<endl;
	} 
	return 0;
}

 

一文读懂 SAP ERP / SAP S/4HANA 里的 Price List:概念、配置、定价流程与可运行 ABAP 示例 SAP ERP/S4HANA中的Price List解析 本文系统阐述了SAP定价体系中Price List的核心概念和实现机制。Price List并非独立的价目表,而是通过条件技术(条件类型、访问序列、条件记录)与Price List Type(PLTYP)共同作用的定价维度。文章详细说明了: Price List本质上是客户主数据中的PLTYP字段,作为定价查找键使用 定价流程中条件访问序列如何利用PLTYP查找特定价格 S4HANA与ECC版本在价格维护入口的差异 实际业务中如何通过不同PLTYP实 阅读详情

相关推荐

别再死记硬背GNN公式了!用PyTorch Geometric从零实现一个GraphSAGE(附完整代码)

本文通过PyTorch Geometric从零实现GraphSAGE模型,深入解析图神经网络的核心原理与实战应用。文章提供完整代码示例,涵盖环境配置、邻居采样、信息聚合等关键步骤,帮助开发者掌握图表示学习技术,避免陷入公式推导的困境。适合希望将GNN应用于实际项目的中级开发者和学生。

weixin_42530570的博客 348

1034 Head of a Gang (30)(30 point(s)(cj)

1034 Head of a Gang (30)(30 point(s)) 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 ...

Cute_jinx的博客 252

瑞芯微RK3588开发板VSCode远程开发全攻略:从SSH配置到插件优化

本文详细介绍了如何在瑞芯微RK3588开发板上配置VSCode远程开发环境。从Ubuntu系统下的SSH服务设置、VSCode Remote-SSH扩展安装,到连接建立、基础工具链部署,再到针对ARM平台的性能优化与调试配置,提供了一套完整的实战指南,帮助开发者高效利用本地资源进行嵌入式开发。

weixin_28831341的博客 625

1034 Head of a Gang (30 point(s))

题解 dfs。技巧性在于string与int之间的匹配,毕竟dfs只能遍历节点呀。你说对不对。   #include&lt;iostream&gt; #include&lt;map&gt; #include&lt;vector&gt; #include&lt;algorithm&gt; using namespace std; const int MAXN = 2e3 + 10; //...

徐伯莱 228

高通平台Camera sensor bring up!

Qualcomm Camera sensor bring up!0. 准确1. Kernel 底层驱动配置1.1 dtsi1.2 pinctrl2. Chi-Cdk XML 配置2.1 sensormodule2.2 sensor3. CHECK 本文主要针对高通平台SM7250进行描述,当然高通平台可能基本都差不多【第一个项目】 —only for sensor probe sucess. 0....

Understand-image 9241

5分钟搞定CST周期性结构仿真:共面波导色散曲线快速生成技巧

本文提供了一套在CST Studio Suite中快速仿真周期性结构并生成色散曲线的标准化流程。针对共面波导设计,重点解析了周期性边界条件的核心设置、本征模求解器(JDM算法)的优化技巧,以及通过参数扫描与后处理模板实现自动化,旨在帮助射频与微波工程师在5分钟内高效完成关键性能验证。

blue的专栏 1249

1034 Head of a Gang

1034 Head of a Gang (30 point(s)) 题意: ​ 警察通过查看通话记录把一些人分成一个一个的帮派。 ​ 判断是否是帮派的条件: ​ 1.ALLcost&gt; K ​ 2.帮派成员大于2 ​ 于是这道题可以用 有向图 来存储题目给的数据,然后DFS 遍历 的同时 求得连通分量(帮派个数),以及整个帮派的通话时间。 ​ 由于题目给的每个顶点的信息是字符串。所以需要...

sologala的博客 661

WorkBuddy入门到 多Agent团队协作完全指南(附实操代码与配图)

WorkBuddy的Agent Teams功能支持多智能体协作,通过创建AI团队提升复杂任务处理效率。该功能支持自然语言创建团队、任务分配与依赖管理、成员间消息通信(包括直接消息和广播),并提供任务审批机制。典型应用场景包括软件开发、内容创作等协作任务。环境要求WorkBuddy 2026年3月及以上版本,支持Windows和macOS系统。用户可快速创建包含不同角色的AI团队,通过共享任务列表和信箱系统实现协作。高级功能包括委派模式、任务依赖管理和成员自主认领等,适合各类协作场景。

三棱球的专栏 6485

1034 Head of a Gang (30 point(s)) - C语言 PAT 甲级

设计思路: DFS、连通分量 1. 因为名字给的都是字母,所以在 C 中,还要考虑如何用数字保存和映射名字: 2. 用数组储存边和节点的权值,DFS 遍历图,记录下符合条件的团伙 3. 团伙输出前,依据头目名排序

有三只土豆 297

STM32CubeMX和华为LiteOS Studio开发STM32

传统的STM32的开发通常使用Keil/IAR这类开发软件,随着物联网时代的到来,传统嵌入式的发展不断向物联网方向靠拢,而Keil/IAR这类开发软件无法满足IoT一站式的开发,华为LiteOS Studio结合STM32CubeMX可以快速,高效地进行物联网开发,本文提供一种开发思路及环境的配置方法。...

weixin_45680600的博客 2833

CTF实战:手把手教你用Python破解转轮机加密(附完整脚本)

本文详细介绍了如何使用Python逆向破解转轮机加密,特别针对CTF比赛中的Crypto类题目。通过解析转轮机加密原理、实战Python脚本编写及调试技巧,帮助参赛者快速掌握破解方法,提升在攻防世界等CTF比赛中的解题效率。

silver的专栏 1117

PAT----A1034 Head of a Gang (30point(s))

A1034 Head of a Gang (30point(s)) 图或者并查集 题意 寻找可疑的犯罪团伙按照字母顺序输出首领的name。 可疑的犯罪团伙指在一个边权加起来大于K并且节点个数大于2的连通图。 首领是边权总和最大的那个。 思路 首先是名字和节点对应的问题,这里用map<string,int> 解决。 然后边权直接加到点权上面,判断时除以二即可。 显然,也可以用并查集。 总...

qq_43578870的博客 179

《UDS协议从入门到精通》系列——图解0x27:安全访问

什么是安全访问服务?应用场景和基本原理是什么?全网最直观详细的图解UDS-0x27:安全访问服务!!

车载系统攻城狮 7238

1034 Head of a Gang (30point(s))

One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call betweenAandB, we say thatAandBis related. The weight of a relation is defined to be t...

张蔡辉 332

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

PAT(甲级)1034 Head of a Gang (30point(s))

题目 题目链接 思路 题目大意:给一张网络图,找到连通块,并找到每个连通块中权值最大的元素,作为头领输出; 这道题本身并不难,但有几个点要注意: 1、给的名字是string型的,并不是下标,所以要用map转化一下; 2、使用DFS进行遍历时,对于当前的节点,连接其边的权值在对当前节点进行DFS时已经加入了,为了避免有环图边值重复加的情况,每一条边在遍历过后就要删去,避免走回头路; 3、每个人都有一个权值,所以需要开一个额外的weight数组记录每个用户的权值; 代码 #include <iostrea

hjyp的博客 191

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

(邻接矩阵DFS实现)1034 Head of a Gang (30 分)

1034 Head of a Gang (30 分) 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 ...

HelloWorld 536

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

1034 Head of a Gang (30)

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

ysq96的博客 278

A1034. Head of a Gang (30)

时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One way that the police finds the head of a gang is to check people's phone

shuest的博客 346

1034 Head of a Gang (仅得24分)

求连通分量 1.dfs 2.并查集 用并查集的时候,因为接下来可能还会更新通话分钟数u,我又不想把对应的一类存储再进行判断......所以就在合并时进行判断操作,寻找最大的点(pre) 实时更新 只需要确保更新完后后续的也一并压入即可。 4 5测试点没过 暂时不清楚哪里遇到问题了 #include<iostream> #include<cstdio> #in...

❂ 312

1034 Head of a Gang (30 分)(算法笔记)

需要解决的问题: 建立邻接矩阵的时候下标所用的是数字,而题目给的是字符串,所以需要按照一定的规则将字符串对应为相应的序号 将人名的编号对应为出现的顺序,再建立两个map函数负责映射:将字符转换为编号,编号转化为字符 剩下的就是DFS图遍历算法的问题 由于题目要求输出的总权值超过threshold(阈值),并且每个帮派的人数大于2人,所以DFS函数中的参数为,“现在访问的成员编号” “&amp...

ostrich的博客 680

PAT---甲级1034 Head of a Gang 30分 并查集使用练习题

&amp;amp;amp;amp;amp;emsp题目大意:警察找犯罪头目的一种方式是检查人们的通话,如果两个人可以应为通话关系直接或间接关联,那么这两个人就在同一个团队里。如果一个团队的人数和总通话量大于给定值,那么这个团队就是一个gang,并且gang里面单人通话量最多的人是head,题目会给出通话关系,要求找出所有的head 输入格式:先输入两个int型,分别表示即将给出的通话关系数量和满足gang的通话量阈...

tothee的博客 1406

pat1034Head of a Gang (30)

题意分析: (1)给出若干条通话记录(包括通话的两端的ID、时长),通过通话记录找出其中有多少个团伙,以及每个团伙的首领和团伙人数,所谓团伙就是指通过话的“联通”组织超过三人,可以理解为连通分量的人数超过三人,且集体通话时长超过一个给定的阈值。团伙首领是指在这个团伙中,这个人的通话时长最长 (2)此题的目标是寻找满足权值超过阈值、节点数超过2的联通分量,最简单的方法就是通过深度优先搜索遍历整个

LostStephon的博客 1548

1034 Head of a Gang (30 分)--PAT甲级

1034 Head of a Gang (30 分) 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 ...

laplacebh的博客 1058

Head of a Gang

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

weixin_34162228的博客 210
上一篇: 1088 Rational Arithmetic (20 point(s))
下一篇: 1032 Sharing (25 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值