1034 Head of a Gang (30 分)

PAT甲级真题 1034 Head of a Gang (30分)(用map数组模拟,注意测试点4) 题目 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 ... 阅读详情

知识点:并查集 DFS

版本4

注意:顶点数可能达到2000,另外权值是所有边的总和,不同的思路有可能计算成了两倍,需要除2后再与K比较。
由于节点在插入的过程中,每两个点的通话时间的动态变化的,所以需要先把通话时长计算出来以后再进行合并。
同时按照权值最大的节点作为根节点的原则。
总结:这道题用并查集也可以在做,但是推荐使用DFS。先用邻接矩阵存起来,同时需要记录点权。然后直接深搜,把所有条件都放在形参中。简单易操作。

#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <queue> 
#include <unordered_map>
#include <algorithm>
using namespace std;
unordered_map<string, int> SI;
unordered_map<int, string> IS;
typedef pair<string, int> PSI;
const int N = 2005;
int cost[N], member[N], root[N], total[N];
struct node{
	int u, v;
}E[N];
int find(int x){
	if(x != root[x]) root[x] = find(root[x]);
	return root[x];
}
int main(){
	int n, K, t, u, v, id = 0;
	scanf("%d%d", &n, &K);
	string p, q;
	memset(cost, 0, sizeof cost);
	for(int i = 0; i < N; i++){
		member[i] = 1;
		root[i] = i;
	}
	for(int i = 0; i < n; i++){
		cin>>p>>q>>t;
		//第一个人 
		if(SI.count(p)) u = SI[p];
		else{
			u = id++;
			SI[p] = u;
			IS[u] = p;
		}
		//第二个人
		if(SI.count(q)) v = SI[q];
		else{
			v = id++;
			SI[q] = v;
			IS[v] = q;
		}
		cost[u] += t;
		cost[v] += t;
		E[i]= {u, v};		
	}
	for(int i = 0; i < N; i++) total[i] = cost[i];
	for(int i = 0; i < n; i++){
		u = E[i].u , v = E[i].v;
		int faU = find(u);
		int faV = find(v);
		if(faU != faV){
			if(cost[faU] > cost[faV]){
				root[faV] = faU;
				member[faU] += member[faV];
				total[faU] += total[faV];
			}else {
				root[faU] = faV;
				member[faV] += member[faU];
				total[faV] += total[faU];
			}
		}
	}
	int num = 0;
	vector<PSI> res;
	for(int i = 0; i < id; i++){
//		printf("%dth root = %d, member = %d, cost = %d, total = %d\n", i, root[i], member[i], cost[i], total[i]);
		if(root[i] == i && member[i] > 2 && total[i]/2.0 > K){
			res.push_back({IS[i], member[i]});
			num++;
		}
	}
	cout<<num<<endl;
	sort(res.begin(), res.end());
	for(auto it: res){
		cout<<it.first<<" "<<it.second<<endl;
	}
	return 0;
} 

版本3

还有2个测试点没过
22分
求指点

#include<bits/stdc++.h>
using namespace std;
const int  maxn = 3005;
int f[maxn],val[maxn];
map<int, string> IS;
map<string, int> SI;
int person=1;
struct node{
	int a,cnt,total,tag,mm;
}E[maxn];
void init(){
	for(int i=0;i<maxn;i++){
		f[i] = i;
		E[i].a=E[i].cnt=E[i].total=E[i].mm=0;
		val[i] = 0;
	}
}
int find(int x){
	if(x == f[x]) return x;
	return f[x] = find(f[x]);
}
bool cmp(node a, node b){
	if(a.tag != b.tag) return a.tag > b.tag;
	else return a.a < b.a;
}
int change(string str){ 
	if(SI.find(str) != SI.end()) return SI[str];
	else{
		SI[str] = person;
		IS[person] = str;
		return person++;
	}
}
int main(){
	int n,m,x,a,b;
	string str1,str2;
	init();
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>str1>>str2>>x;
		a = change(str1);
		b = change(str2);
		int fa = find(a);
		int fb = find(b);
		val[a] += x;  
		val[b] += x;
		if(fa != fb) f[fa] = fb;
			
	}

	for(int i=0;i<maxn;i++){ //筛选 
		int d = find(i);  
		if(val[i]!=0){ 
			if(E[d].mm < val[i]){
				E[d].mm = val[i];
				E[d].a = i;
			} 
			E[d].total += val[i];
			E[d].cnt++;
		}
	}
	
	int k=0;
	for(int i=0;i<maxn;i++){
		if(E[i].cnt > 2 && E[i].total/2 > m){
			k++;
			E[i].tag=1;
		}else 
			E[i].tag = 0;
	}
	sort(E,E+maxn,cmp);
	cout<<k<<endl;
	for(int i=0;i<k; i++){
		cout<<IS[E[i].a]<<" "<<E[i].cnt<<endl;
	}
	return 0;
}

版本2

DFS

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#define N 2010
using  namespace std;
map<string,int> string2int;
map<int,string> int2string;
map<string,int> gang;
int G[N][N]={0},w[N]={0};
bool vis[N] = {false};
int idNum = 1;
int k;
int change(string s){
	if(string2int[s] == 0){
		string2int[s] = idNum;
		int2string[idNum]=s;
		return idNum++;
	}else{
		return string2int[s];
	}
}
void DFS(int u,int &h,int &m,int &t){
	m++;
	vis[u] = true;
	if(w[u] > w[h]){
		h = u;
	}
	for(int v=1;v < idNum;v++){
		if(G[u][v] > 0){ //如果有边 
			t += G[u][v];
			G[u][v]=G[v][u] = 0;
			if(vis[v]==false){
				DFS(v,h,m,t);
			}
		}
	}
}
void DFSTrave(){
	for(int i=1; i<idNum;i++){
		if(vis[i]==false){
			int head = i,numMem=0,totalV=0;//头目,总人数,总边权
			DFS(i,head,numMem,totalV);
			if(numMem > 2 && totalV > k){
				gang[int2string[head]] = numMem;
			} 
		}
	}
}
int main(){	
    int n,d;
	string str1,str2;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>str1>>str2>>d;
		int id1 = change(str1);
		int id2 = change(str2);
		w[id1] += d;  //点权 
		w[id2] += d;  
		G[id1][id2] += d; //边权 
		G[id2][id1] += d; 	
		}
	DFSTrave(); 
	cout<<gang.size()<<endl;
	map<string,int>::iterator it;
		for(it = gang.begin();it !=gang.end();it++){
			cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}

版本1

DFS

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#define N 2010
using  namespace std;
map<string,int> string2int;
map<int,string> int2string;
map<string,int> gang;

int G[N][N]={0},w[N]={0};

bool vis[N] = {false};
int idNum = 1;
int k;
int change(string s){
	if(string2int[s] == 0){
		string2int[s] = idNum;
		int2string[idNum]=s;
		return idNum++;
	}else{
		return string2int[s];
	}
}
void DFS(int i, int &cnt, int &mxi, int& cost){
	cnt++; cost += w[i];
	if(w[i] > w[mxi]) mxi = i;
	for(int j = 1; j < idNum; j++){
		if(!vis[j] && G[i][j]){
			vis[j] = 1;
			DFS(j, cnt, mxi, cost);
		}
	}
}
void DFST(){
	for(int i = 1; i < idNum; i++){
		if(!vis[i]) {
			vis[i] = 1;
			int cnt = 0, mxi = i, cost = 0;
			DFS(i, cnt, mxi, cost);
			if(cnt > 2 && cost/2.0 > k) gang[int2string[mxi]] = cnt;
		}
	}
}

int main(){	
    int n, d;
	string str1,str2;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>str1>>str2>>d;
		int id1 = change(str1);
		int id2 = change(str2);
		w[id1] += d;  //点权 
		w[id2] += d;  
		G[id1][id2] += d; //边权 
		G[id2][id1] += d; 	
		}
	DFST(); 
	cout<<gang.size()<<endl;
	map<string,int>::iterator it;
		for(it = gang.begin();it !=gang.end();it++){
			cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}


并查集模拟_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... 阅读详情

相关推荐

APO的全面进化之路(2):走向IBP、建设端到端供应链计划体系

前一篇记录阐述了在供应链计划(Supply Chain Planning)领域里SAP的关键产品APO的不断进化之路,即部分产品从APO进化到S/4HANA,本篇将继续阐述进化之路上的另一大中坚产品力量SAP-IBP(Integrated Business Planning)。作为SAP数字化供应链(Digital Supply Chain)中供应链计划SCP的左膀右臂,S/4HANA与IBP共同完善了计划域的完整闭环。本篇作为收尾,在先谈对IBP产品的个人认知之前,特别是在当今这个IT产品迭代多、技术革新

ChampaignWolf的专栏 5232

1034 Head of a Gang (30 分,附详细注释,逻辑分析)

写在前面 实现思路

kngines 726

/lib//libclntsh.so: file format not recognized; treating as linker script

centos7.7+oracle19.3 建库过程报错:预检查都是通过的 /oracle/product/19.3.0/db_1/lib/libasmperl19.a -Wl,--no-whole-archive -lclntsh -lclntshcore -lasmclntsh19 -lhasgen19 -lskgxp19 -locr19 -lcell19 -lirc INFO: /usr/bin/ld:/home/app/oracle/product/19.3.0/db_1/li...

sqlora的专栏 9914

1034 Head of a Gang (30分)测试点分析

简要分析 这个题目想对复杂一点的,dfs以及bfs也没那么容易写,因为他要统计好几个量,单独一个比较难实现。我用dfs和bfs分别实现了,但是都是拿了20分,有三个测试点没过。分别是两个答案错误和一个段错误。 测试点3,这个原因是数组开小了,因为1000条通话记录最多可以产生2000个不同的人。 测试第2,这个原因是没有给答案排序正确,意思是你的答案可能是对的,但是输出顺序不对。 测试点5,这个是...

ZiYuTongXue的博客 1320

基于matlab的车牌识别(含子程序)

基于matlab的车牌识别系统一、对车辆图像进行预处理1.载入车牌图像:function [d]=main(jpg) [filename, pathname] = uigetfile({'*.jpg', 'JPEG 文件(*.jpg)'}); if(filename == 0), return, end global FILENAME %定义全局变量 FILENAME = [pathname f...

TuTu998的博客 4万+

PCAN-Explorer5使用教程

在PCAN中新建工程,导入DBC文件(右键apply),导入采集的数据选择对应的CAN通道点击红框中的标记可选择想要查看的信号在plot界面跳出属性,可调整信号显示的样式和最大最小值调出cursor,选择需要观测的变量,选中右键style——Period 可显示间隔时间增加cursor,可观测多个变量。

weixin_43997043的博客 6914

【1034】Head of a Gang (30 分)

#include<iostream> #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string> #include<algorithm> #include<map> #include<vector> #include...

发现问题,并解决问题,批判性思维 868

【Coze】在Coze平台使用源码创建工作流

Coze平台提供低代码工作流搭建功能,支持通过图形化界面快速构建自动化任务流程。用户可通过资源库创建工作流,导入JSON配置生成节点结构,连接起始节点并配置输入/输出字段(需定义数据类型)。平台提供试运行功能验证流程执行,支持可视化调试与发布上线。该方案适用于快速部署标准化业务逻辑,实现端到端的自动化处理。

Mr数据杨 2432

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

利用Dify平台打造大学生专属AI模拟面试助手:从零到精通的完整指南

在这个充满竞争的就业市场中,拥有一个24小时在线的AI面试教练无疑是大学生求职路上的秘密武器。通过本文的指导,你已经掌握了使用Dify平台构建专业级模拟面试助手的全套技能。从环境部署、模型选择,到Prompt设计、知识库集成,再到高级功能开发和效果优化,我们一步步打造了一个能够适应不同岗位、提供专业反馈的智能面试系统。无论是技术岗的算法问答,还是咨询业的案例分析,或是外企的英语面试,你的定制助手都能提供针对性训练。更重要的是,这个方案具有极强的可扩展性。

专注于编程、AI、人工智能等多个领域内容研究和分析 2792

2019年高教社杯全国大学生数学建模竞赛 C 题:《机场的出租车问题》真题解析与 MATLAB 解决方案

🏆 本文已收录于专栏:《滚雪球学数学建模(含历年真题)》,持续更新最新及历年数学建模竞赛资料,每篇内容力求做到“问题讲透、模型讲清、代码可用、论文可参考”,帮助你从零开始逐步建立完整的数学建模知识体系。

**My Coding Family** 3453

1034 Head of a Gang (30分)

1 题目 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 o...

韩江雪de 小屋 696

第四章:无感FOC的平滑启动艺术:告别“咔哒”声与抖动

无感FOC启动技术面临"先有位置还是先有旋转"的核心矛盾,导致常见故障包括位置失准、负载突变等。基础方案采用开环I/F控制,但存在重载启动困难等局限性。进阶方案结合IPD位置检测技术,通过脉冲电压注入法精准定位转子初始位置。关键技术在于开环到闭环的平滑切换,采用混合观测器设计和相位对齐技术。量产优化包括自适应参数整定和故障恢复机制,确保启动可靠性和一致性。该系统工程方案可有效解决启动抖动、异响等问题,提升用户体验。

汽车软件开发 741

1034 Head of a Gang (30 分)

1034Head 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 betweenAandB, we say thatAandBis related. The weight of a...

weixin_42105789的博客 404

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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 betweenAandB, we say thatAandBis related. The weight of a relation is defined to b...

SourDumplings的博客 205

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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 relation is defined to be the total time length of all .

小渣渣的博客 434

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

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

大山喵写博客的地方 1295

PAT 1034 Head of a Gang (30分)

原题链接:1034 Head of a Gang (30分) 关键词:dfs、并查集 现在给出通话记录,请你找出所有帮派(输出头目名称)以及帮派里的人数。

cwtnice的博客 239
上一篇: 1076 Forwards on Weibo (30 分)
下一篇: 迷路的牛牛
SYaoJun
博客等级 码龄11年 287粉丝 1327原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值