1034 Head of a Gang(30分)

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

题目翻译:

给定N条通话记录和一个限制K。每条通话记录包含了两个名字和一个通话时间,相互联系的人同属于一个帮派。题目要求,只有人数超过2人并且总通话时长超过K的集体才能算作帮派Gang,并且帮派内部通话时长最长的人是帮派首领Boss。

题解思路:

  1. 通过两个map,来完成name和id的互相映射,方便哈希查找。
  2. 再用一个map,来保存头领姓名和帮派人数的,保存答案。
  3. w[]数组来保存每个人的通话时间,g[][]来保存边的权重,并且方便dfs找到帮派
  4. 找到新的w更大的结点,head指向该结点,成为新的头领。
  5. totalweight来记录每个帮派的总通话时间。一定要记住,先遍历边,累加了通话时间,之后一定要把这条边删除。这样一方面避免出现环的情况,造成边权重漏加或者重复加的情况。

代码:

并查集:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 100;
const int M = N * 10 ;
const int mod = 1e9 + 7;
const double eps = 1e-9;
typedef pair<int, int> PII;
#define endl '\n'
#define x first
#define y second
#define INF 0x3f3f3f3f
#define ls(k) k << 1
#define rs(k) k << 1 | 1
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, total;
unordered_map<string, int> num;
unordered_map<int, string> name;
map<string, int> ans;
vector<int> gang[N];
int p[N], w[N], k[N];

void create(string x)
{
    if(!num[x])
    {
        num[x] = ++ total;
        name[total] = x;
    }
}

int find(int x)
{
    return p[x] == x ? x : p[x] = find(p[x]);
}

void merge(int a, int b, int t)
{
    int fa = find(a), fb = find(b);
    if(fa == fb)
    {
       k[fa] += t;
       return;
    }

    p[fa] = fb;
    k[fb] += k[fa] + t;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n >> m;

    for (int i = 1; i <= n; i ++) p[i] = i;

    for(int i = 1; i <= n; i ++)
    {
        string a, b;
        int t;
        cin >> a >> b >> t;
        create(a), create(b);
        w[num[a]] += t;
        w[num[b]] += t;
        merge(num[a], num[b], t);
    }

    for (int i = 1; i <= total; i ++) gang[find(i)].push_back(i);

    for (int i = 1; i <= total; i ++)
    {
        if(k[i] <= m || gang[i].size() <= 2) continue;

        int head = i;
        for (auto j : gang[i])
        {
            if(w[j] > w[head]) head = j;
        }
        ans[name[head]] = gang[i].size();

    }
    cout << ans.size() << endl;
    for (auto i : ans) cout << i.first << " " << i.second << endl;

    return 0;
}

DFS(柳神):

#include <bits/stdc++.h>
using namespace std;
 
const int maxn=2010;
const int inf=1e8;
 
map<string,int> nameToid;//姓名转编号
map<int,string> idToname;//编号转姓名
map<string,int> gang;    //头领->人数
int n,k,number=0;        //通话记录数量,最低时间,总人数
int w[maxn]={0},G[maxn][maxn]={0};
bool vis[maxn]={false};
 
//遍历节点
void DFS(int now,int &head,int &gangnum,int &totalweight){
    gangnum++;     //成员数量增加
    vis[now]=true; //已经访问过当前结点
    if(w[now]>w[head]){
        head=now;    //找到权重最大的点,成为新的首领
    }
    for(int i=0;i<number;++i){
        if(G[now][i]>0){
            totalweight+=G[now][i];  //累加这条边
            G[now][i]=G[i][now]=0;   //将这两个点之间的边删除
            if(!vis[i]){ //如果下一个点还没被访问过,就访问
                DFS(i,head,gangnum,totalweight);
            }
        }
    }
}
 
//遍历整个图
void DFSTrave(){
    for(int i=0;i<number;++i){
        if(!vis[i]){
            int head=i,gangnum=0,totalweight=0; //不要忘了初始化
            DFS(i,head,gangnum,totalweight);
            if(gangnum>2 && totalweight>k){
                    //gang是保存name对应人数的
                gang[idToname[head]]=gangnum;
            }
        }
    }
}
//完成名字和id的编号,方便查找
int change(string s){
    //找到的话,就不需要再添加姓名了
    if(nameToid.find(s)!=nameToid.end()){
        return nameToid[s];
    }else{
        nameToid[s]=number;
        idToname[number]=s;
        return number++;
    }
}
int main(){
    int weight;
    cin >> n>>k;
    string s1,s2;
    for(int i=0;i<n;++i){
        cin >> s1>>s2 >>weight;
        int id1=change(s1);
        int id2=change(s2);
        w[id1]+=weight;         //保存到对应的权重里
        w[id2]+=weight;
        G[id1][id2]+=weight;    //保存到邻接矩阵里面
        G[id2][id1]+=weight;
    }
    DFSTrave();
    cout << gang.size()<<"\n";
    //因为map是按照关键字从小到大输出的,所以不用排序
    map<string,int>::iterator it;
    for(it=gang.begin();it!=gang.end();++it){
        cout << it->first << " "<< it->second<<"\n";
    }
    return 0;
}

再贴一个看错题(以为姓名只能是AAA这种都一样的形式,最后导致只能过两个点)

 

#include<bits/stdc++.h>
using namespace std;
map<char, int> p;
map<int, int> c;
int N, K;
int f[1010];

int find(int x)
{
	if (x != f[x]) f[x] = find(f[x]);
	return f[x];
}

struct node {
	int num = 0, sum = 0, index = -1;
};
vector<node> m(26);

bool comp(node n1, node n2)
{
	return n1.index < n2.index;
}

int main()
{
	cin >> N >> K;
	for (int i = 0;i < 26;i++)
		p['A' + i] = i;
	for (int i = 0;i < 1010;i++)
		f[i] = i;
	for (int i = 0;i < N;i++)
	{
		string a, b;
		int temp;
		cin >> a >> b >> temp;
		c[p[a[0]]] += temp;
		c[p[b[0]]] += temp;
		int x = find(p[a[0]]), y = find(p[b[0]]);
		if (x != y) f[y] = x;
	}
	int cnt = 0;//集合个数
	for (int i = 0;i < m.size();i++)
	{
		m[find(i)].num++;
		m[find(i)].sum += c[i];
		if (m[find(i)].index == -1 || c[i] > c[m[find(i)].index])
			m[find(i)].index = i;
	}
	sort(m.begin(), m.end(), comp);
	for (int i = 0;i < 26;i++)
	{
		if (m[i].num >= 3 && m[i].sum > K*2)
			cnt++;
	}
	cout << cnt << endl; 
	for (int i = 0;i < 26;i++)
	{
		if(m[i].num >= 3 && m[i].sum > K*2)
			cout << char(m[i].index + 'A') << char(m[i].index + 'A') << char(m[i].index + 'A') << " " << m[i].num << endl;
	}
}

坑点:

无

软件各项会议评审意见模版 读梅老师主编的书,觉得评审意见模板很有参考价值和实用价值,帮梅老师安利一波,顺便记一下笔记。 《系统/子系统规格说明》评审意见模板 20XX年XX月XX日,XXX(主管机关)在XXX(会议地点)主持召开了“《XXX(产品名称)系统规格说明》评审会”。参加会议的有XXX机关,XXX院校,XXX研究所共XX名代表(名单见附件1),会议成立了评审组(名单见附件2),听取了XXX(单位)做的《XXXX报告》。经讨论质询,形成评审意见如下: 系统规格说明对系统的研制目标和范围、工作状态、运行方式以及所要满足的能力需 阅读详情

相关推荐

Unity 2022.3.3 LTS + Visual Studio 2022:手把手教你复刻《吸血鬼幸存者》核心战斗(附完整项目源码)

本文详细介绍了如何使用Unity 2022.3.3 LTS和Visual Studio 2022从零构建《吸血鬼幸存者》式战斗系统。通过图文开发教程,涵盖对象池优化、ECS架构雏形、2D物理碰撞控制等关键技术,帮助开发者高效复刻这款热门游戏的自动攻击与海量刷怪机制。

weixin_30721077的博客 465

【PAT(甲级)】1034 Head of a Gang

解题思路: 就是一个并查集,加一个权重判断的问题。可以用bfs来遍历所有节点,然后找到权重最大的节点就是帮派的头目。其中的难点就在于,节点不是常规的数字,而是string类型的名字。我的解决方法就是直接用map给他们匹配一下。...

weixin_55202895的博客 264

在Ansys Simplorer中设计三相逆变器,并与Maxwell FEA耦合,实现160kW PMSM

在这篇博文中,我们将探讨如何使用设计三相逆变器,并将其与Ansys Maxwell中的电磁有限元分析模型耦合,以获得准确的系统级结果。Ansys Electronics为多域仿真提供了强大的生态系统,可实现跨各种物理领域的无缝集成。Maxwell用于电磁有限元分析 (FEA),Simplorer(Twin Builder) 用于系统级逆变器和控制器设计,Icepak用于电子热仿真。例如,我们将使用 Simplorer 介绍160 kW、8 极永磁同步电机 (PMSM)的逆变器设计过程。让我们开始吧!

jkh920184196的博客 1433

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

PAT甲级1034题解

小染的学习笔记 927

AI气象预报:数值模式融合与短临预警的工程实践

气象预报正经历从物理推演到数据驱动的认知范式转变。AI气象预报并非替代传统数值模式,而是通过数值模式融合弥补其在参数化误差、初值敏感性和计算效率上的固有瓶颈。其技术价值在于提升短临预警时效性与空间精度,支撑航空调度、电力调控、防洪应急等关键场景。实践中需兼顾物理一致性、可解释性与业务系统兼容性,避免纯黑箱建模带来的外推失效与不确定性缺失风险。本文聚焦AI作为超分辨率引擎、误差矫正器和短临加速器的三类落地路径,解析HEALPix投影、多尺度图建模、轻量级U-Net等核心适配方法。

weixin_28221501的博客 306

Neo4j Desktop新手必看:3分钟搞定CSV数据导入(附常见报错解决方案)

本文详细介绍了在Neo4j Desktop中高效导入CSV数据的三种主流方法,包括图形化界面操作、命令行批量导入和Python驱动编程导入,并提供了常见报错解决方案和性能优化技巧,帮助新手快速构建知识图谱。

lambda的博客 652

「1034」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 betweenAandB, we say thatAandBis related. The weight of a relation is define...

weixin_52435933的博客 236

dxf-viewer 查看器

dxf-viewer SDK 的示例,它是一个基于 Three.js 和 Vue3 构建的基于 WebGL 的 BIM 模型查看器。它用于查看 DWG/DXF/PDF 文件。面积测量 - 角度测量 -标记。

qq_25547755的博客 879

1034. Head of a Gang (30)

【代码】1034. Head of a Gang (30)

2401_88085478的博客 1039

CodeX 登录报错 os error 10013?一键修复 CodeX 无法登录问题(亲测有效)

CodeX 登录时报 os error 10013?本文提供快速修复方案,实测有效,彻底解决登录报错:登录失败:failed to start login server: 以一种访问权限不允许的方式做了一个访问套接字的尝试。 (os error 10013)的问题💡

XiaoqiangClub的博客 2964

【AUTOSAR-DEM】-2.1-APP SWC设置UDS/OBD DTC检出和恢复

点击返回「《Autosar_BSW高阶配置》总目录」 案例背景(共6页精讲): DEM模块提供两种不同类型的Event: • BSW-related events:DEM_EVENT_KIND_BSW(通过C-API报告 - Dem_SetEventStatus) • SW-C-related events:DEM_EVENT_KIND_SWC(通过RTE operation - SetEventStatus) 下面我们讲讲在APP SWC中设置BusOff(总线关闭) DTC故障成立/恢复(其它

「汽车电子助手」的博客 3950

PAT 甲级 1034 Head of a Gang

语雀满了,只能暂时先用CSDN来记录一下了。这道题之前看王道书的时候做过一次,当时是照着查并集的思想做的。昨天刷PAT重做的时候,第一时间想到的还是查并集,然后尝试着自己独立去写,发现仅仅用查并集其实还是有很多的坑的。

2301_77209094的博客 169

12、认知地图的建模与学习

本博客系统介绍了认知地图的定义、背景及其在机器智能领域的应用,重点探讨了模糊认知地图(FCMs)的建模方法与优势。文章还对比分析了几种主流的认知地图模型(如Axelrod模型、Kosko模型等),并讨论了模型的稳定性条件及实际应用前景。通过智能制造和医疗诊断的具体案例,展示了认知地图在复杂因果推理中的强大能力,并提出了模型优化方向与未来研究展望。

plant的博客 218

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 小屋 700

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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的博客 405

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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

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

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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

SM20155201314的博客 421

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

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

大山喵写博客的地方 1297

PAT 1034 Head of a Gang (30分)

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

cwtnice的博客 240
上一篇: 1033 To Fill or Not to Fill(25分)
下一篇: 2-5(篇外)词向量Word2Vec代码实战篇
笨笨sg
博客等级 码龄5年 2万+粉丝 496原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值