题干:

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 ≤ 106), 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 33 1
M 1 20 0

Sample Output

Case #1: 3
Case #2: 2

解题报告:

并查集删点问题,虚拟一个节点,f数组开到n+m,用来找real点的父节点,real开到n,用来存输入数据的真实值(输入的也都是小于n的,但是real数组可以将其映射到>n上,换句话说,real的下标一定小于n(此题取不到等号),但是real的内容可以大于n,其实也就是 可能是那个从n+1开始的id值)。注意一些细节以及背过这一个模板即可。

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
const int MAXn = 1e5 + 5;//点数
const int MAXm = 1e6 + 5;//指令数
using namespace std;
int n,m;
int cnt,id;
char op[5];
int f[MAXn+MAXm+5];
int real[MAXn+5];//real不需要开到MAXm!!
int bk[MAXn+MAXm+5];
int getf(int v) {if(f[v]!=v)f[v]=getf(f[v]);return f[v];
}
void init() {cnt=0;id=n+1;for(int i = 1; i<=n; i++) {real[i]=i;f[i]=i;}memset(bk,0,sizeof(bk) );
}void merge(int u,int v) {int t1=getf(u);int t2=getf(v);if(t1!=t2) f[t2]=t1;
}
void del(int u) {real[u]=id;f[id]=id;//必须要有,因为f数组只赋值到n,所以要赋值! id++;
}int main()
{int u,v;int iCase=0;while(~scanf("%d %d",&n,&m) ) {if(m==0 && n==0) break;init();while(m--) {scanf("%s",op);if(op[0]=='M') {scanf("%d %d",&u,&v);merge(real[u],real[v]);}else {scanf("%d",&u);del(u);//?有疑问? //必须u!!不能real[u]! 想想也知道啊因为del里面real[i]其中i要<n的(因为开的数组real[]开到了n,就是 记录输入的点的真实值,而输入的点最大是n)!所以你传的i肯定要<n啊所以不能是real[u]! }                                               //其实如果 传real[u],你想找到他就需要real[real[u]]了!显然不符合了。。整个结构都改变了,因为你可能两层real嵌套就可能三层四层、、代码没法实现了就。 }for(int i = 0; i<n; i++) {//此题规定了!从 0 开始! if(bk[getf(real[i] ) ] ==0) {//别忘加getf!! bk[getf(real[i] ) ]=1;cnt++;}} printf("Case #%d: %d\n",++iCase,cnt);}return 0 ;
}

ac代码2:(可以再看看?)

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

#include <iostream>
#include <stdio.h>
#include <algorithm>
#define MAXNUM1 100005
#define MAXNUM2 1000005using namespace std;
int f[MAXNUM1*2+MAXNUM2],num[MAXNUM1];int getf(int a)
{if(f[a]==a)return a;else return f[a]=getf(f[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++)f[i]=i+n;for(int i=n;i<=n+n+m;i++)f[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 = getf(x);int ry = getf(y);if(rx!=ry) f[rx]=ry;}else{scanf("%d",&x);f[x] = total++;}}for(int i = 0; i < n; i++)num[i] = getf(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;
}

总结:

1.初始化的时候啊  i<n写成i<m了,,粗心?

2.看清题目啊本题要求了从0开始的点,你最后遍历的时候还从1开始,,,读题?

3.其他要注意的点都写在代码里了,值得注意的就是加深一下对f数组和real数组的理解!

*【HDU - 2473】Junk-Mail Filter (并查集--删点操作)相关推荐

  1. HDU 2473 Junk-Mail Filter(并查集的删除操作)

    题目地址:HDU 2473 这题曾经碰到过,没做出来. .如今又做了做,还是没做出来. ... 这题涉及到并查集的删除操作.想到了设一个虚节点,可是我把虚节点设为了要删除的点的父节点.一直是栈溢出,目 ...

  2. hdu 3234 Exclusive-OR 题解(并查集,思维)

    该死的期末复习终于结束了... 暑假来了\color{#ff0000}{暑假来了}暑假来了!!! 所以我就珂以非常开心的写博客了. 原题链接: hdu 题意简述 多组数据.你有一个没有确定的数列.有一 ...

  3. How Many Answers Are Wrong HDU - 3038(带权并查集经典题,满满的都是注释)

    How Many Answers Are Wrong HDU - 3038  点击打开链接 题意:现在有n个数(你并不知道这n个数是什么),m次查询,每次查询给出u,v,w.表示从第u个数到第v个数的 ...

  4. hdu 1232 畅通工程 最小生成树 并查集

    1232的连接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 #include <iostream>#include <cstdio& ...

  5. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  6. HDU 1272 小希的迷宫 (并查集)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  7. hdu 1811 Rank of Tetris (并查集+拓扑排序)

    Problem - 1811 感觉这题的并查集以及拓扑排序并不难,但是做题的时候必须理解到矛盾(CONFLICT)与不确定(UNCERTAIN)直接的优先关系. 做这题的时候,构图什么的很简单,就是没 ...

  8. hdu 3047 Zjnu Stadium(并查集)

    题意: 300个座位构成一个圈. 有N个人要入座. 共有M个说明 :A B X ,代表B坐在A顺时针方向第X个座位上.如果这个说明和之前的起冲突,则它是无效的. 问总共有多少个无效的. 思路: 并查集 ...

  9. HDU - 2874 Connections between cities(并查集+LCA)

    题目链接:点击查看 题目大意:给出n个点代表城市,再给出m条边将其连接,每条边都有边权,题目保证给出的图无环,现在给出两个点,首先询问两个点是否互相连通,若可以连通,询问两点之间的距离 题目分析:判断 ...

最新文章

  1. 防抖 节流_关于防抖和节流
  2. NOI2015 题解
  3. 看看这14家科技前沿公司 原来最牛的天使投资是它
  4. 内存分配详解 malloc, new, HeapAlloc, VirtualAlloc,GlobalAlloc
  5. 什么是CSS?你真的理解?
  6. 计算机专业论文设计与实现,计算机专业论文 计算机网络的设计与实现.doc
  7. SAP License:赛锐信息访谈启示录(三)
  8. 普通版Mobaxterm查看保存的密码明文
  9. c语言 error c2562,C语言之关键字(二) void,const
  10. loj6225「网络流 24 题」火星探险问题
  11. python输入一个整数和一个字符_【python零基础入门】基础语法之变量、字符串、数字、规则。...
  12. 基于嘉立创双层板E5071C的低成本TRL校准
  13. 软件架构设计 :VO,BO,PO,DO,DTO的理解
  14. 敏捷项目的自动化单元测试的6大好处
  15. 中兴N880S使用SuperOneClick2.1.1出现waiting device
  16. 以json格式输出 bro(zeek)日志
  17. 百度api智能识别图片
  18. 手把手编译基于恩智浦MCAL的工程
  19. 中兴oltc320用户手册_中兴C320C300 V2版本OLT开局配置手册.doc
  20. 免费SSL证书(https网站)申请

热门文章

  1. 【数据结构与算法】二分查找
  2. [Java基础][Java]toString()方法
  3. mysql5.6.25密码_安装压缩版mysql5.6.25/ 5.7.14
  4. Educational Codeforces Round 112 (Rated for Div. 2)(A-D)
  5. c语言作业重庆科技学院,C语言程序设计学生上机报告-NO3.doc
  6. matlab解带参数的积分方程组,方程组求解问题:方程组中有带参数的积分函数,求参数...
  7. 微软的平板电脑_Microsoft 微软 Surface Go 2 10.5英寸二合一平板电脑(m3-8100Y、8GB、128GB、LTE) 5788元...
  8. 禅道——需要我们斟酌
  9. linux C之access函数
  10. C/C++ 通过初始化列表和构造函数内赋值初始化成员变量的区别