题目来源:

pid=2473">HDU 2473 Junk-Mail Filter

题意:2中操作 M x, y 将x,y 合并到一个集合 S x 将x从所在的集合去掉 自己成为一个集合 最后求有多少个集合

思路:删点不好做 能够假设0 1 2在一个集合 能够定义个数组映射 就是每一个点所相应实际的点 開始是a[0] = 0 a[1] = 1 a[2] = 2

如今要去掉2 能够定义一个新的点 原来的要删的点留着 原来a[2] = 2 a[2] = 3 如今的3就是原来的第二个2点 仅仅只是名字换成3了 a[2] = 3 以后訪问第二个点 都訪问a[2]

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1100010;
int f[maxn], a[maxn], flag[maxn];
int cnt; void init(int n)
{
for(int i = 1; i <= n; i++)
f[i] = a[i] = i;
cnt = n;
memset(flag, 0, sizeof(flag));
} int find(int x)
{
if(x != f[x])
return f[x] = find(f[x]);
return f[x];
} void merge(int x, int y)
{
x = find(x);
y = find(y);
if(x != y)
f[x] = y;
}
void del(int x)
{
find(a[x]);
f[++cnt] = cnt;
a[x] = cnt;
}
int main()
{
int cas = 1;
int T;
int n, m;
while(scanf("%d %d", &n, &m) && (n||m))
{
init(n);
while(m--)
{
char s[10];
scanf("%s", s);
if(s[0] == 'S')
{
int x;
scanf("%d", &x);
x++;
del(x);
}
else
{
int x, y;
scanf("%d %d", &x, &y);
x++, y++;
merge(a[x], a[y]);
}
}
int ans = 0;
for(int i = 1; i <= n; i++)
{
int x = find(a[i]);
if(!flag[x])
{
ans++;
flag[x] = 1;
}
}
printf("Case #%d: %d\n", cas++, ans);
}
return 0;
}

HDU 2473 Junk-Mail Filter 删点并查集的更多相关文章

  1. hdu 2473 Junk-Mail Filter (暴力并查集)

    Problem - 2473 为什么标题写的是暴力并查集?因为我的解法跟网上的有所不同,方法暴力很多. 先解释题意,这是一个模拟处理垃圾邮件的问题.垃圾邮件要根据它们的性质进行分类.对于10w个邮件, ...

  2. HDU - 5438 Ponds(拓扑排序删点+并查集判断连通分量)

    题目: 给出一个无向图,将图中度数小于等于1的点删掉,并删掉与他相连的点,直到不能在删为止,然后判断图中的各个连通分量,如果这个连通分量里边的点的个数是奇数,就把这些点的权值求和. 思路: 先用拓扑排 ...

  3. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  4. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  5. HDU(1856),裸的带权并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1856 题意:朋友圈问题,A和B是朋友,B和C是朋友则A和C也是朋友,依次类推,题目的意思就是求最大的朋 ...

  6. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  7. HDU 1213 How Many Tables(模板——并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...

  8. hdu 1811 Rank of Tetris - 拓扑排序 - 并查集

    自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...

  9. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

随机推荐

  1. jsp导出table数据excel表

    <html> <head> <meta http-equiv="content-Type" content="text/html;chars ...

  2. Linux脚本中调用SQL,RMAN脚本

    Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle的相关工作,也是DBA必不可 ...

  3. 【CF1043B】Lost Array(枚举)

    题意:给定n与数组a,求所有的k使得存在x数组能按以下规则构造出a n<=1e3,a[i]<=1e6 思路: #include<cstdio> #include<cstr ...

  4. FckEditor2.65使用范例源码

    原文发布时间为:2009-10-12 -- 来源于本人的百度文章 [由搬家工具导入] 下载地址:http://www.xmaspx.com/Services/FileAttachment.ashx?A ...

  5. [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  6. android中与Adapter相关的控件----ViewFlipper

    ViewFlipper(翻转视图) 一.ViewFlipper是一个多页面管理的控件,与ViewPager不同,ViewPager的是一页一页的的,而ViewFlipper则是一层一层的.图片轮播或者 ...

  7. manjaro中okular中的pdf文件无法显示中文

    最近发现Manjaro下用Okular打开含有中文的PDF文档时,许多地方显示空白, 开始怀疑是中文字体问题,但是其他的软件中文显示都是正常的. 接着,调整Okular中的中文字体,显示还是不正常. ...

  8. VIM使用技巧5

    查找并手动替换.例如有如下一段文字: We' re waiting for content before the site can go live If you are content with th ...

  9. ubuntu16.04安装docker CE

    如需开始在 Ubuntu 上使用 Docker CE,请确保您满足先决条件,然后再安装 Docker. 如需安装 Docker 企业版 (Docker EE),请转至获取适用于 Ubuntu 的 Do ...

  10. Python Challenge 第九关

    第九关只有一幅图,上面有一些黑点.网页名字叫:connect the dots.可能是要把这些点连起来. 查看源代码,果然有两个整数集合 first 和 second.并且有个提示:first+sec ...