hdu 2473 Junk-Mail Filter(并查集+虚拟节点)

简介:

Junk-Mail Filter

Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 169 Accepted Submission(s): 58

Problem Description
Recognizing junk mails is a tough task. The method used here consists of two steps:
1) Extract the common characteristics from the incoming email.
2) Use a filter matching the set of common characteristics extracted to determine whether the email is a spam.

We want to extract the set of common characteristics from the N sample junk emails available at the moment, and thus having a handy data-analyzing tool would be helpful. The tool should support the following kinds of operations:

a) “M X Y”, meaning that we think that the characteristics of spam X and Y are the same. Note that the relationship defined here is transitive, so
relationships (other than the one between X and Y) need to be created if they are not present at the moment.

b) “S X”, meaning that we think spam X had been misidentified. Your tool should remove all relationships that spam X has when this command is received; after that, spam X will become an isolated node in the relationship graph.

Initially no relationships exist between any pair of the junk emails, so the number of distinct characteristics at that time is N.
Please help us keep track of any necessary information to solve our problem.
 

Input
There are multiple test cases in the input file.
Each test case starts with two integers, N and M (1 ≤ N ≤ 10 5 , 1 ≤ M ≤ 10 6), the number of email samples and the number of operations. M lines follow, each line is one of the two formats described above.
Two successive test cases are separated by a blank line. A case with N = 0 and M = 0 indicates the end of the input file, and should not be processed by your program.
 

Output
For each test case, please print a single integer, the number of distinct common characteristics, to the console. Follow the format as indicated in the sample below.
 

Sample Input
5 6
M 0 1
M 1 2
M 1 3
S 1
M 1 2
S 3

3 1
M 1 2

0 0
 

Sample Output
Case #1: 3
Case #2: 2
 
 
复制代码
分析:这道题是一道典型的并查集的题目,关键是节点从集合中删除的s操作
这里使用了节点数的的下标+n作为父节点,这个位置只是标记父节点,并没其他含义,等于是虚拟的节点。
这样当删除一个节点时只用从这个节点中拿出来让其父节点重新放在一个虚拟的位置,即下标从n+n开始向后找。
最后是查找独立特点的集合。将这些父节点放在一个长度为n的num的数组中,里边放置的每个节点对应的父节点的位置,
然后对这个数组排序,找出其中不同父节点的数目即集合的个数。


#include <iostream> #include <stdio.h> #include <algorithm> #define MAXNUM1 100005 #define MAXNUM2 1000005 using namespace std; int father[MAXNUM1*2+MAXNUM2],num[MAXNUM1]; int find_father(int a) { if(father[a]==a)return a; else return father[a]=find_father(father[a]); } int main() { int n,m,ca,x,y,total; char op[3]; ca = 0; while(scanf("%d%d",&n,&m)!=EOF&&n) { for(int i=0;i<n;i++)father[i]=i+n; for(int i=n;i<=n+n+m;i++)father[i]=i; total = n+n; for(int t=0;t<m;t++) { scanf("%s",op); if(op[0]=='M') { scanf("%d%d",&x,&y); int rx = find_father(x); int ry = find_father(y); if(rx!=ry)father[rx]=ry; }else{ scanf("%d",&x); father[x] = total++; } } for(int i = 0; i < n; i++)num[i] = find_father(i); sort(num,num+n); int ans=1; for(int i = 1; i < n;i++) if(num[i]!=num[i-1])ans++; printf("Case #%d: %d\n",++ca,ans); } return 0; }
复制代码
本文转自NewPanderKing51CTO博客,原文链接: http://www.cnblogs.com/newpanderking/archive/2012/10/31/2748440.html  ,如需转载请自行联系原作者
相关文章
|
存储 缓存 Java
仅花200行代码,如何将60万行的RocksDB改造成协程
采用少量手动修改+自动代码转换的方式,将大型多线程程序改造成协程。在某些重IO、高并发的场景中,帮助业务取得了性能翻倍的效果。
56879 3
仅花200行代码,如何将60万行的RocksDB改造成协程
|
22天前
|
人工智能 IDE API
AI Agent 框架实战横评:通义灵码、OpenClaw、Hermes 三框架深度对比
AI Agent(智能体)是 2026 年最火的技术方向,但面对众多框架开发者往往无从选择。本文从真实项目需求出发,深度对比阿里云生态三大 Agent 框架——通义灵码(IDE 内智能体)、OpenClaw(开源 Agent 框架)、Hermes Agent(轻量级 Agent 平台),从架构设计、MCP 集成、Vibe Coding、部署方式、成本五个维度进行实战评测,并给出不同场景的选型建议。
|
6月前
|
CDN
2026最新阿里云CDN收费标准:不同计费模式价格表(基础服务费和增值服务费用整理)
阿里云CDN费用分基础费(必选)和增值费(按需使用)。基础费支持按流量、带宽峰值或月结95峰值三种计费模式,默认按流量阶梯计价(中国内地低至0.15元/GB);可购资源包享优惠。增值费含HTTPS、QUIC、WAF、实时日志等,仅启用才计费。
1503 10
|
2月前
|
机器学习/深度学习 弹性计算 人工智能
阿里云服务器支持哪些GPU卡?来看看,阿里云GPU加速卡及ECS实例规格族大全
阿里云GPU服务器支持L20、T4、A10、A30、A16、V100、P4、P100等NVIDIA加速卡,适配gn6i/gn7i/gn8is等ECS实例族,覆盖AI训练/推理、图形渲染、科学计算等场景。详情及报价请见官网:https://t.aliyun.com/U/tbBeAi
339 1
|
4月前
|
存储 消息中间件 人工智能
2026年阿里云最新个人360元,企业用户1728元优惠券领取和使用介绍
2026年阿里云推出AI焕新季活动,为个人和企业用户提供满减券礼包,个人满减券总额360元,企业满减券总额1728元。这些优惠券适用于千问图像生成模型、大语言模型推理等AI产品,也适用于云服务器ECS、云数据库等官方产品的新购和升级。活动面向已实名认证的用户,用户可在活动期间内领取并使用优惠券,享受新购和升级的优惠。
1350 2
|
SQL 容灾 关系型数据库
阿里云DTS踩坑经验分享系列|DTS打通SQL Server数据通道能力介绍
SQL Server 以其卓越的易用性和丰富的软件生态系统,在数据库行业中占据了显著的市场份额。作为一款商业数据库,外部厂商在通过解析原生日志实现增量数据捕获上面临很大的挑战,DTS 在 SQL Sever 数据通道上深研多年,提供了多种模式以实现 SQL Server 增量数据捕获。用户可以通过 DTS 数据传输服务,一键打破自建 SQL Server、RDS SQL Server、Azure、AWS等他云 SQL Server 数据孤岛,实现 SQL Server 数据源的流动。
1048 0
阿里云DTS踩坑经验分享系列|DTS打通SQL Server数据通道能力介绍
|
XML 机器学习/深度学习 JSON
|
边缘计算 人工智能 云计算
HIS系统的核心模块:医生工作站
医生工作站是HIS系统的核心模块,围绕诊疗流程一体化、电子病历智能化、医嘱闭环管理、辅助决策支持及移动化操作五大方面优化医疗流程。它整合患者信息,提供结构化病历生成与质控,实现医嘱全流程追踪,结合药品和诊疗知识库辅助决策,并支持多场景便捷操作。通过数据整合与智能工具,提升诊疗效率与质量,助力智慧医疗发展。
765 2
|
数据采集 前端开发 数据挖掘
利用 html_table 函数轻松获取网页中的表格数据
本文介绍了如何使用 R 语言中的 `html_table` 函数结合代理 IP 技术,轻松提取网页表格数据并规避反爬机制。通过设置代理和请求头,示例代码展示了如何从 58 同城采集租房信息并保存为 CSV 文件。该方法适用于需要频繁采集数据的场景,确保数据采集的高效和稳定性。
658 2
利用 html_table 函数轻松获取网页中的表格数据
|
机器学习/深度学习 人工智能 算法
【AI系统】模型压缩基本介绍
模型压缩旨在通过减少存储空间、降低计算量和提高计算效率,降低模型部署成本,同时保持模型性能。主要技术包括模型量化、参数剪枝、知识蒸馏和低秩分解,广泛应用于移动设备、物联网、在线服务系统、大模型及自动驾驶等领域。
924 4
【AI系统】模型压缩基本介绍