【HDOJ】2473 Junk-Mail Filter
并查集删除结点,方法是构建虚拟点,做映射。
#include <stdio.h>
#include <string.h> #define MAXNUM 1000050 int bin[MAXNUM], assist[MAXNUM];
char visit[MAXNUM];
int n, ext; int find(int x) {
int r = x;
int i = x, j; while (r != bin[r])
r = bin[r]; while (i != r) {
j = bin[i];
bin[i] = r;
i = j;
} return r;
} void merge(int x, int y) {
int fx, fy; fx = find(x);
fy = find(y); if (fx != fy) {
bin[fx] = fy;
}
} void extract(int x) {
bin[ext] = ext;
assist[x] = ext++;
} int main() {
int m, t=;
int i, x, y;
char ch; while (scanf("%d %d", &n, &m)!=EOF && (n||m)) {
memset(bin, , sizeof(bin));
memset(assist, , sizeof(assist));
memset(visit, , sizeof(visit));
for (i=; i<n; ++i) {
bin[i] = i;
assist[i] = i;
}
ext = n;
while (m--) {
scanf("%*c%c", &ch);
if (ch == 'M') {
scanf("%d %d", &x, &y);
x = assist[x];
y = assist[y];
merge(x, y);
} else {
scanf("%d", &x);
extract(x);
}
}
m = ;
for (i=; i<n; ++i) {
x = find(assist[i]);
if (visit[x] == ) {
++m;
visit[x] = ;
}
}
printf("Case #%d: %d\n", t++, m);
} return ;
}
【HDOJ】2473 Junk-Mail Filter的更多相关文章
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
- 【HDOJ】1362 The Bermuda Triangle
1. 题目描述给定几个三角形拼成一个百慕大三角形. 2. 基本思路基本思路肯定是搜索,关键点是剪枝.(1) 若存在长度为$l$的边,则一定可以拼成长度为$k \cdot l$的三角形,则可拼成长度为$ ...
随机推荐
- 转载---SQL Server XML基础学习<1>之--FOR XML PATH
--> 测试数据:#tbIF OBJECT_ID('TEMPDB.DBO.#tb') IS NOT NULL DROP TABLE #tbGO CREATE TABLE #tb ...
- SqlServer优化博客网址
CareySon Sql Server MVP : http://www.cnblogs.com/CareySon/
- 总结Qt中经常出现的一些问题
1.Qt中用高版本打开低版本的工程 编译时出现错误 : C1189: #error : "This file was generated using the moc from 4.7.0. ...
- cognos 10.2.2 搭建网关做负载均衡
最近要设计cognos服务器灾备模式,所以想到了cognos10自带的gateway负载均衡模式,搭建起来还是挺简洁的 设计背景: cognos主服务器:231 cognos灾备服务器:238 gat ...
- MyEclipse 多项目对应配置多个Tomcat
在MyEclipse的安装目录下,有D:\Program Files\MyEclipse 6.5\myeclipse\eclipse\plugins 的插件路径. 里边很多插件的配置文件包. 找到 ...
- HDU 4627 The Unsolvable Problem(简单题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4627 题目大意:给定一个整数n(2 <= n <= 109),满足a+b=n并且[a,b] ...
- UVA 10881 Piotr's Ants(等效变换 sort结构体排序)
Piotr's AntsTime Limit: 2 seconds Piotr likes playing with ants. He has n of them on a horizontal po ...
- Library string type(2)——关于String的操作
关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827 string的操作 s.empty() //Return ...
- C#中的Collections命名空间
System.Collections命名空间包含可使用的集合类和相关的接口. 该命名空间下的.NET非泛型集合类如下所示: — System.Collections.ArrayList:数组集合类,使 ...
- Linux 进程编程
Linux通过维护者五个状态来调度进程的运行.这五个状态分别为:运行.可中断.不可中断.僵死.停止 . PID来标识不同的进程的,Linux中每一个进程都有一个唯一的进程号 . PCB块就是一个进程资 ...