HUD 2473 - Junk-Mail Filter

Junk-Mail Filter

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

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.

给定n个点,m个操作。
M a b 表示 a与b属于同一集合
S a 表示 将a从集合删去
所有操作完成后输出集合的个数

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 ≤ 10 6), 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.
输入包含多组,每组两个整数n,m(1<=n<=100000,1<=n<=1000000)
接下来m行M a b或S a表示操作

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

并查集删点。
建立一个 f 数组来做并查集,建立一个 replace 数组来存代替的点。
f 和 replace 数组一开始都指向自身。
下面说明一下怎么用replace来代替点。

比如一共5个点,1为0,2,3的根节点,然后我们S 1,f 数组不动,replace[1]的值从 1 变成 5。然后接下来的和 点1 有关的操作 和 replace[1] (也就是新创造出来的点5)进行,0,2,3还是以1为根节点。

#include<iostream>
using namespace std;
#include<set>

int f[1000010] = {};//f 数组用来做并查集 
int replace[1000010] = {};//replace 数组用来存代替被替换的点 

int find(int x)
{
    int t = x;
    while (f[x] != x) x = f[x];
    while (t != f[t])
    {
        int temp = f[t];
        f[t] = x;
        t = temp;
    }
    return x;
}

void insert(int x,int y)
{
    int fx = find(x);
    int fy = find(y);
    if (fx != fy)
    {
        f[fy] = fx;
    }
}

int main()
{
    int count = 0,n,m;
    while (scanf("%d%d",&n,&m))
    {
        if (n == 0 && m == 0) break;
        for (int i = 0;i<=n-1;i++)//初始化,一开始都指向自身。 
        {
            f[i] = i;
            replace[i] = i;
        }
        char c;
        int v,u,len = n;//新的结点 
        for (int i = 1;i<=m;i++)
        {
            getchar();
            scanf("%c",&c);
            if (c == 'M')
            {
                scanf("%d%d",&u,&v);
                insert(replace[u],replace[v]);
            }
            else
            {
                scanf("%d",&u);
                f[len] = len;//新建一个点并且作为根节点,也就是指向自身 
                replace[u] = len++;//把删除的点指向新的点。接下来的和旧的点有关操作都由这个新的点来代替。 
            }
        }
        int num = 0;
        set<int> s;
        for (int i = 0;i<=n-1;i++)
        {
            int t = find(replace[i]);
            if (s.count(t) == 0)
            {
                num++;
                s.insert(t);
            }
        }
        printf("Case #%d: %d\n",++count,num);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
android-junk-code,指的是在安卓应用程序中存在的垃圾代码。垃圾代码是指那些对程序功能没有直接影响,或者不被正式使用的代码片段。 垃圾代码的存在有几个原因。首先,开发者在开发过程中可能临时添加了一些测试代码或者调试代码,但是忘记在最后的版本中移除它们。这些代码虽然不会影响程序功能,但是会占用额外的内存和存储空间,增加应用的体积。 其次,由于需求变更或者代码重构,一些原本有用的代码可能变得无效。然而,为了防止引发新的问题,开发者可能选择保留这些无用的代码,而不进行清理。 另外,一些广告库、统计库或者第三方SDK可能会在应用中添加一些无用的代码。这些库的开发者为了实现既定的功能,可能会添加一些额外的代码,而这些代码对于应用来说是无用的。 垃圾代码的存在会对应用的性能和用户体验产生一定的影响。首先,它会增加应用的启动时间和响应时间。其次,垃圾代码会增加应用的内存占用和存储空间占用,从而影响设备的性能和电池寿命。此外,由于垃圾代码的存在,应用的维护和升级也会变得更加困难。 为了解决android-junk-code问题,开发者可以采取以下措施。首先,定期进行代码审查和清理,剔除无用的代码。其次,选择优质的第三方库,避免集成过多无用功能的库。另外,在开发过程中,尽量避免添加无用的测试或调试代码。 总之,android-junk-code是一种常见的问题,需要开发者注意并进行及时清理。清理垃圾代码可以提升应用的性能和用户体验,减少占用的存储空间和设备资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值