Junk-Mail Filter

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

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 ≤ 105 , 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 3

3 1
M 1 2

0 0

 
Sample Output
Case #1: 3
Case #2: 2
 题解:由于考虑到有时会删除父节点,所以再开一个数组,更新节点时直接改变当前节点的父节点,使其等于大于n的父节点,这样就把其节点删除了。。。和南阳合纵连横那题一样;注意退出时只退出的是当前那个人,并不是这个子树;例如测试数据:
4 4
M 0 1
M 0 2
M 0 3
S 1
Case #1: 2

代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=1e6+;
int pre[MAXN],prt[MAXN],num[MAXN];
int find(int x){
int r=x;
while(r!=pre[r])r=pre[r];
pre[x]=r;
return r;
}
void merge(int x,int y){
int f1,f2;
f1=find(x);f2=find(y);
if(f1!=f2){
pre[f2]=f1;
}
}
int n;
void initial(){
for(int i=;i<=n;i++)pre[i]=i,prt[i]=i;
memset(num,,sizeof(num));
}
int main(){
int m;
char s[];
int a,b,flot=;
while(~scanf("%d%d",&n,&m),n|m){
initial();
int k=n;
while(m--){
scanf("%s",s);
if(s[]=='M'){
scanf("%d%d",&a,&b);
merge(prt[a],prt[b]);
}
else{
scanf("%d",&a);
prt[a]=k;
pre[k]=k;
k++;
}
}
int cnt=;
for(int i=;i<n;i++){
int t=find(prt[i]);
if(!num[t]){
cnt++;
num[t]=;
}
}
printf("Case #%d: %d\n",++flot,cnt);
}
return ;
}

合纵连横

时间限制:1000 ms  |           内存限制:65535 KB
难度:3
描述

乱世天下,诸侯割据。每个诸侯王都有一片自己的领土。但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法吞并那些实力弱的,让自己的领土 面积不断扩大。而实力弱的诸侯王为了不让自己的领土被吞并,他会联合一些其他同样弱小的诸侯国,组成联盟(联盟不止一个),来共同抵抗那些强大的诸侯国。 强大的诸侯国为了瓦解这些联盟,派出了最优秀的间谍来离间他们,使一些诸侯国退出联盟。最开始,每个诸侯国是一个联盟。

有两种操作

1、U x y 表示x和y在同一个联盟。(0≤x,y<n)

2、D x   表示x退出联盟。

输入
多组测试数据
第一行两个数,n和m(1 ≤ n≤ 10^5, 1 ≤ m ≤10^5),分别表示诸侯国的个数和操作次数。
接下来有m行操作
输出
输出联盟的个数
样例输入
5 7
U 0 1
U 1 2
U 0 3
D 0
U 1 4
D 2
U 0 2
10 1
U 0 9
 
样例输出
Case #1: 2
Case #2: 9

Junk-Mail Filter(并差集删点)的更多相关文章

  1. 2016.09.14,英语,《Using English at Work》全书笔记

    半个月时间,听完了ESLPod出品的<Using English at Work>,笔记和自己听的时候的备注列在下面.准备把每个语音里的快速阅读部分截取出来,放在手机里反复听. 下一阶段把 ...

  2. [Windows Azure] Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5.

    Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5. This ...

  3. django 操作数据库--orm(object relation mapping)---models

    思想 django为使用一种新的方式,即:关系对象映射(Object Relational Mapping,简称ORM). PHP:activerecord Java:Hibernate C#:Ent ...

  4. 4.1HTML和Bootstrap css精华

    1.HTML 2.理解Bootstrap HTML元素告诉浏览器,他要表现的是什么类型的内容,当他们不提供任何关于如何显示内容的信息.如何显示内容的信息,由CSS提供. 本书仅包含足够的信息,让你查看 ...

  5. syslog及syslog-ng详解 日志服务器

    服务器的日志对系统工程师来说是至关重要的,一旦服务器出现故障或被入侵,我们需要查看日志来定位问题的关键所在,所以说对于线上跑的服务器而言日志应该合理的处理及管理.下面来   服务器的日志对系统工程师来 ...

  6. 网络-05-端口号-F5-负载均衡设-linux端口详解大全--TCP注册端口号大全备

    [root@test1:Standby] config # [root@test1:Standby] config # [root@test1:Standby] config # [root@test ...

  7. CentOS 7 服务端口表

    # Note that it is presently the policy of IANA to assign a single well-known# port number for both T ...

  8. 日志文件系统syslog,syslog-ng

    日志文件系统syslog,syslog-ng   余二五 2017-11-07 20:37:00 浏览127 评论0 日志 LOG 配置 主机 正则表达式 syslog 表达式 source file ...

  9. syslog-ng应用详解

    syslog-ng应用详解   科技小能手 2017-11-07 02:43:00 浏览136 评论0 日志 LOG 配置 主机 syslog source file varchar 摘要: 最近做一 ...

随机推荐

  1. php 输出带变量字符串

    (一) <?php $a=50;echo "Hello World 我有"."$a"."元";?> 看此例子,变量a的输出,在p ...

  2. [LeetCode]题解(python):155-Min Stack

    题目来源: https://leetcode.com/problems/min-stack/ 题意分析: 实现一个小的栈,包括初始化,push,pop,top,和getMin. 题目思路: 私用是用两 ...

  3. c 函数传入数组。

    php 里面 直接 count($arr); 一个函数搞定, c里面想判断下数组的个数却非常困难. 想到php是C写的,那看看他的函数怎么写的不就行了... 哦,天啊,,事实比我想的要复杂的多... ...

  4. 製程能力介紹(SPC introduction) ─ 製程能力的三種表示法

    製程能力的三種表示法 Ck: 準度指標 (accuracy)   Ck=(M-X)/(T/2) Cp: 精度指標 (precision)   Cp=T/(6σp) 規格為單邊時:Cp=(Tu-X)/3 ...

  5. 如何在Delphi中调用VC6.0开发的COM

    上次写了如何在VC6.0下对Delphi写的COM进行调用,原本想马上写如何在Delphi中调用VC6.0开发的COM时,由于在写事例程序中碰到了个很怪的问题,在我机子上用VC写的接口程序编译能通过. ...

  6. (13)[Xamarin.Android] 不同分辨率下的图片使用概论

    原文 [Xamarin.Android] 不同分辨率下的图片使用概论 设计Android App的时候,其尺寸众多也是一个挑战之一.要针对不同尺寸设计Android App时,就要先来了一下dpi(d ...

  7. 转:onkeypress、onkeydown、onkeyup 区别

    在使用JavaScript做WEB键盘事件侦听捕获时,主要采用onkeypress.onkeydown.onkeyup三个事件进行出来.该三个事件的执行顺序如下:onkeydown -> onk ...

  8. Day4_代码重用与函数

    知识点速记: 重用代码的方法:脚本包含require().include(); 全局配置文件php.ini(auto_prepend_file/auto_append_file); 目录配置文件.ht ...

  9. 错误解决一_call time pass-by-reference removed

    我的操作: 定义function my_function(&$param) 调用 my_function(&$value)错误来了:Call-time pass-by-referenc ...

  10. 【Deep Learning】genCNN: A Convolutional Architecture for Word Sequence Prediction

    作者:Mingxuan Wang.李航,刘群 单位:华为.中科院 时间:2015 发表于:acl 2015 文章下载:http://pan.baidu.com/s/1bnBBVuJ 主要内容: 用de ...