题目来源:

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. MySQL服务器端&客户端常见错误

    目录(?)[+] 客户端 服务器端 客户端 1.ERROR 2013 (HY000) at line 1: Lost connection to MySQL server during query 使 ...

  2. spring rest 请求怎样添加Basic Auth请求頭

    请自行揣摩代码 package com.hudai.platform.manager.util; import java.net.URI; import java.net.URISyntaxExcep ...

  3. BZOJ 3910: 火车

    3910: 火车 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 358  Solved: 130[Submit][Status][Discuss] D ...

  4. MVP MVVM MVC

    上一篇得到大家的关注,非常感谢.由于自己对于这些模式的理解也是有限,对于MVC,MVP,MVVM这些模式的比较,是结合自己的理解,一些地方不一定准确,需要的朋友可以参考下 上一篇得到大家的关注,非常感 ...

  5. [LeetCode] Search Insert Position 二分搜索

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---10

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  7. 教学生jni编程(一)

    我的所有文件的目录在/home/michelle/WORKM/DAYCODE/day0426/,请根据个人情况适当调整 1)创建一个HelloWorld.Java文件,如下: public class ...

  8. hdu 4517(递推枚举统计)

    小小明系列故事——游戏的烦恼 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)To ...

  9. BZOJ1801 [Ahoi2009]chess 中国象棋(DP, 计数)

    题目链接 [Ahoi2009]chess 中国象棋 设$f[i][j][k]$为前i行,$j$列放了1个棋子,$k$列放了2个棋子的方案数 分6种情况讨论,依次状态转移. #include <b ...

  10. Atcoder Contest 015 E

    题目大意 给定一条数轴. 数轴上有\(n\)个点, 它们的初始位置给定, 移动速度也给定. 从0时刻开始, 所有点都从其初始位置按照其移动速度向数轴正方向移动. 这些点开始时可能是红色的, 也可能是黑 ...