PAT - 1034 Head of a Gang (30 分) 图的DFS

深入解析磁盘性能测试:vdbench与fio的实战对比 本文深入对比了磁盘性能测试两大工具vdbench与fio的核心定位与实战应用。vdbench擅长多客户端、可编排的存储系统集成测试,而fio则以灵活的单机深度定制见长,是测试硬件极限和模拟特定应用IO模型的利器。文章通过安装指南、参数解析和典型场景对比,帮助读者根据需求选择合适工具,精准评估磁盘性能。 阅读详情

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

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

#include<iostream>
#include<string>
#include<map>
using namespace std;
const int maxn = 2010;
const int INF = 1E9;

map<int, string> intToString;
map<string, int> stringToInt;
map<string, int> Gang; // head and number

int G[maxn][maxn] = {0}, weight[maxn] = {0};
int n, k, numPerson = 0;
bool vis[maxn] = {false};

void DFS(int nowVisit, int& head, int& numMember, int& totalValue){
    numMember++;
    vis[nowVisit] = true;
    if(weight[nowVisit] > weight[head])
      {  head = nowVisit;}
    for(int i = 0; i < numPerson; i++){//扫描每条边,发现,则进入
        if(G[nowVisit][i] > 0){
            totalValue += G[nowVisit][i]; // edge weight
            G[nowVisit][i] = G[i][nowVisit] = 0;
            if(vis[i] == false)
                DFS(i, head, numMember, totalValue);
        }
    }
}


void DFSTrave(){
    for(int u = 0; u < numPerson; u++){
        if(vis[u]==false){
            int head = u, numMember = 0, totalValue = 0;
            DFS(u, head, numMember, totalValue);
            if(numMember > 2 && totalValue > k){
                Gang[intToString[head]] = numMember;
            }
        }
          
    }
}

int change(string str){
    if(stringToInt.find(str) != stringToInt.end()){//已经出现过
        return stringToInt[str];
    }
    else{  
        stringToInt[str] = numPerson;
        intToString[numPerson] = str;
        return numPerson++;
    }
  
}
int main(){
    string string1,string2;
    int w;
    cin>>n>>k;
    for(int i = 0 ;i < n; i++){
        cin>>string1>>string2>>w;
        int id1 = change(string1);
        int id2 = change(string2);
        weight[id1] += w;
        weight[id2] += w;
        G[id1][id2] += w;
        G[id2][id1] += w;
    }
    //找到头目及总权值
    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. stringToInt,将人名转换成整数,然后存储到邻接矩阵中,整数可以直接采用【总人数】进行计数。
  2. 存储好点的权值和边的权值,还有一个连通分量中的总权值,在DFS中对这些变量进行更新。
Claude Code 100个真实案例 - 用AI搭建GIS地理信息系统(地图标注+热力图) 本案例展示如何使用 Claude Code 从零搭建一个完整的 GIS 地理信息系统。我们将使用 Python + Flask 作为后端,Leaflet.js 作为前端地图引擎,实现地图标注、热力图展示、区域绘制、坐标搜索等功能。全程通过与 Claude Code 对话完成,让你感受 AI 在地理信息领域的强大开发能力。 阅读详情

相关推荐

51单片机IAP在线升级

51单片机IAP在线升级 爱矽半导体E85F3325单片机IAP在线升级教程,此处可查看更新及demo下载 文章目录前言一、ROM资源二、KEIL有关知识1.BL51连接器:2.LX51连接器:三、Keil其他相关项说明:四、MCU启动文件说明:五、例程BOOTLOADER使用说明六、例程使用可更改说明:七、程序使用通信方式:XMODEM八、APP程序说明九、APP代码升级说明十、代码合并烧写 前言 扩展-IAP 主要用于产品出厂后应用程序的更新作用 ,类似于其他引导装载升级程序的方法,在系统中必须始终

qq_42415953的博客 6668

PAT (Advanced Level) Practice A1034 Head of a Gang (30 分)(甲级)(C++)(图、BFS,map)

原题链接:A1034 Head of a Gang #include&lt;algorithm&gt; #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; #include &lt;ctime&gt; #include &lt;cmath&gt; #include &lt;vector&gt; #inclu

izhxxx的博客 251

深入理解DQN中的Q值:核心概念和应用

在强化学习中,Q值(或称为动作价值函数)是核心概念之一。Q值函数 ( Q(s, a) ) 表示在给定状态 ( s ) 下,采取动作 ( a ) 并遵循某策略后,智能体从当前状态到未来状态所能获得的预期总回报。简言之,Q值是评估特定状态和动作对的好坏的指标。

qlkaicx的博客 2977

驾驶人科目一模拟考试系统破解(驾照模拟考试软件破解)纪要

驾驶人科目一模拟考试系统破解(驾照模拟考试软件破解)纪要 1,驾驶人科目一模拟考试系统,一个VB的程序,看下注册界面。 没有注册时软件也可以使用,只是题目数量和时间都有限制,题目只能做30题。 2,由于不用脱壳,简要分析代码: 00422DAE   .  8B15 64D04200 mov     edx, dword ptr [42D064] 00422DB

zhw309的专栏 9898

MD01和MD01N比较

摘要:MD01(传统MRP)与MD01N(MRP Live)是SAP系统不同版本的物料需求计划事务码。核心差异包括:MD01N取消MRP列表、简化计划规则、合并MPS/MRP操作、采用BAdI技术等,体现S/4HANA平台简化、标准化和智能化的演进方向。MD01N通过优化参数配置、增强筛选功能、利用HANA实时计算能力,实现了更高效的物料计划流程,代表了新一代计划引擎的技术革新。

SAP_LES_COM的博客 893

jetson nano网络配置(有线、无线、USB)

注意:本节内容使用Jetson nano B01 版本或Jetson nano ALT 版本安装方式相同,在安装无线网卡等设备时,由于线材和设备比较脆弱,需要轻拿轻放,接口连接不上时,需要重新对准接口在进行连接,不要强行连接。Jetson nano 需要连接网络来进行更多的操作,本节就来讲解Jetson nano 的三种网络连接方法。有线:我们可以使用网线连接Jetson nano 的RJ45 接口,实现联网。

m0_73841621的博客 1万+

db2中通过设置DB2_COMPATIBILITY_VECTOR 注册表变量启用DB2兼容性功能

DB2_COMPATIBILITY_VECTOR注册表变量启用一个或多个 DB2® 兼容性功能。这些功能使您可轻松完成将为 DB2 产品之外的关系数据库产品编写的应用程序迁移至 DB2 产品的任务。 注册表变量设置 值如下所示: NULL(缺省值)。这表示不支持兼容性功能。 0000-FFFF的十六进制值。 变量值中的每一位启用单独的兼容性功能。针对每个位的含义,请参见表 1。 ...

bfhai的博客 1899

基于深度学习的柠檬等级检测系统 —— YOLOv8 + UI界面 + 数据集

本项目的目标是构建一个柠檬等级检测系统,通过深度学习模型自动检测柠檬的等级。自动检测柠檬等级:通过图像中的柠檬特征,自动判断其成熟度和质量。实时视频检测:通过摄像头实时监控柠檬的质量等级。用户友好的UI界面:为用户提供简洁直观的操作界面,支持图像上传、实时视频检测等功能。本项目展示了如何基于YOLOv8模型开发一个柠檬等级检测系统,结合PyQt5实现了用户友好的UI界面。通过该系统,用户可以快速上传柠檬图像并自动识别其等级,极大提升了检测效率和准确性。数据集的扩展。

YOLO 1113

pat甲级A1034 Head of a Gang (30分) 图DFS

A1034 开始建图的时候考虑用字符串哈希,但后来看算法笔记发现map也能过就直接用map写了。 #include <bits/stdc++.h> using namespace std; const int N = 1e4 + 10, M = 2e8 + 10; int h[N], e[M], ne[M], w[N], idx; int n, k; bool st[N]; void...

qq_43732110的博客 132

7.6 GPU页表更新机制

本文分析了AMDGPU驱动中BO到VA映射的页表更新实现机制。核心流程通过amdgpu_vm_bo_update()函数触发,支持CPU直接写入和SDMA异步命令两种更新模式。系统根据VRAM可见性自动选择最优更新策略,处理三种主要场景:命令提交、BO迁移和BO销毁。关键实现包括DMA地址数组处理、权限标志合并和BO迁移状态转换,最终通过amdgpu_vm_update_range()完成硬件页表更新。该机制有效平衡了更新延迟与吞吐量,为GPU虚拟内存管理提供基础支撑。

图形/计算GPU的驱动与虚拟化技术分享园地 2471

RK3588,V4l2 读取Gmsl相机, Rga yuv422转换rgb (dma), 实现零拷贝

RK3588, 使用V4l2 读取 gmsl 相机,获得yuv422格式图像, 使用 rga 转换 rgb 图像。减少cpu占用率. 内存管理方式采用 dma,实现零拷贝.

J。 1263

【PAT】A1034 Head of a Gang (30分)(图的遍历dfs)

1034 Head of a Gang (30分) 题目链接 思路:图的深搜。 #include<iostream> #include<map> #include<cstring> #include<algorithm> using namespace std; const int maxn = 2005; const int inf = 0x3...

醉酒诗人的博客 180

实现两端固支梁热力耦合的有限元分析

在本文中,我们将使用Matlab实现两端固支梁的热力耦合有限元分析。我们假设梁的材料是均匀的,梁的截面积为1,长度为1,两端固支,且受到均匀分布的热荷载。接下来,我们需要组装全局刚度矩阵和热传导矩阵。对于两个相邻的元素,它们共享一个节点,因此需要使用连接矩阵将局部矩阵组装成全局矩阵。接下来,我们需要定义元素刚度矩阵和热传导矩阵。然后,我们需要定义载荷向量。然后,我们需要定义边界条件。在这个例子中,梁的两端固支,因此我们需要将两端节点的位移限制为0。最后,我们需要解决线性方程组,得到节点的位移和温度分布。

CyberSparkZ的博客 396

1034 Head of a Gang (30 分) PAT甲级(图的遍历,DFS)

1034 Head of a Gang (30 分) PAT甲级(详细分析) 一、原题 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 tim

weixin_51566032的博客 355

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

【PAT甲级】1034 Head of a Gang (30分):图的遍历DFS

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

u012161251的博客 196

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

PAT A 1034 Head of a Gang (30 分)(图的DFS遍历的应用)

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

小魚兒 183

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

PAT 1034 Head of a Gang (30分) 图的连通分量 + DFS

题目 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 betw

余生愿你常欢笑 282

*PAT_甲级_1034 Head of a Gang (30分) (C++)【图的遍历/DFS】

目录 1,题目描述 题目大意 输入 输出 2,思路 数据结构 自定义函数 注意 算法 3,代码 1,题目描述 gang:一帮,一伙(罪犯); threthold:门槛; 门口; 阈; 界; 起始点; 开端; Sample Input 1: 8 59 AAA BBB 10 BBB AAA 20 AAA CCC 40 DDD EEE 5 EEE DDD 70 FFF...

&再见萤火虫&的博客 387

PAT 甲级 1034 Head of a Gang(30 分)图的遍历,DFS

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

qq_29762941的博客 297

PAT (Advanced Level) Practice 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 AAA and BBB, we say that AAA and BBB is related. The weight of a relation is defined...

一只小羊 285

PAT甲级 1034 Head of a Gang (30 分)(图的dfs遍历,map映射)

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

Joyceyang_999的博客 542

A1034 Head of a Gang(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【DFS+图的遍历】

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 the total time length of all the phone calls made between...

qq_47677800的博客 210

1034. Head of a Gang (30)-PAT甲级真题(图的遍历dfs)

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

柳婼 の blog 4271

【PAT】1034 Head of a Gang (30) [graph][dfs]

1034 Head of a Gang (30分) 牛客 1034 Head of a Gang (30分) 测试点3排序后正确 其他测试点…快被整疯了,牛客上可以过PAT过不了 测试点1和2应该是输出0,其他的应该是输出格式问题? #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<string> #include<vector> #include<map> using namespa

马甲都掉光了 229

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

题目:http://pat.zju.edu.cn/contests/pat-a-practise/1034 题解: 代码: #include #include #include #include #include #include #include #include #include using namespace std; #define INF 0x6fffffff map weight

Ted's 游乐园 1660
上一篇: PAT- 并查集-好朋友
下一篇: PAT - 1076 Forwards on Weibo (30 分) 图的BFS遍历
DedicateToAI
博客等级 码龄9年 6粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值