HDU 2473 junk-mail Filter "and check set + set virtual parent (vest)"

Source: Internet
Author: User
Tags cas

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=2473


Original question:

Problem Description Recognizing junk mails is a tough task. The method used here consists of the 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 e-mail is a spam.

We want to extract the set of common characteristics from the N sample junk emails available at the moment, and thus havin G 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 so the characteristics of spam X and Y are the same. Note that the relationship defined are transitive, so
Relationships (other than, the one between X and Y) need to be created if they is 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 have when the This command is received; After that, spam X would 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 Ti Me is N.
Keep track of any necessary information to solve US problem.
Input there is multiple test cases in the input file.
Each test case starts with 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 of which is one of the formats described above.
Successive test cases is separated by a blank line. A case with N = 0 and M = 0 indicates the end of the input file, and should not being processed by your program.
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


Analysis and Summary:

There are n messages, and then two operations, if M x y, indicate that x and Y are the same message. If it is S X, then the judgement on X is wrong, X is not the current set of x, to separate x, so that x becomes a separate one.


It is obvious to use and look up the set to do. One of the things that began to confuse me was, suppose the following

M 0 2

M 1 2

S 2

Then follow the set to do, 0 points to 2, 1 points to 2, i.e.

0-->2

1-->2,

After deleting 2, I thought the title meant that all the relationships with 2 would have to be deleted, then the two relationships would have to be removed and become independent of the 3.

but my understanding is wrong . After merging is a collection {0,1,2}, if you delete 2, {0,1} is still a collection.


After understanding the test instructions, we know that it is easy to construct the set using the and check set, but it is not easy to delete one of the sets. Through this problem, I learned the so-called virtual parent node method.

The key process is to assume that you want to delete the x point, then not really delete the x point, but instead of a mapping (here with an array of majia[n]), turn the X into a new point i.e. majia[x] = NewNode. So the original collection is still the same, just a few x points.



Code:

#include <cstdio> #include <cstring> #define N 1100000 int f[n],rank[n],majia[n],flag[n],id,n,m;
    inline void init () {for (int i=0; i<n; ++i) f[i]=majia[i]=i;
    memset (rank, 0, sizeof (rank));
Id=n;
    } int find (int x) {int i, j=x;
    while (J!=f[j]) j=f[j];
    while (x!=j) {i=f[x];f[x]=j;x=i;}
Return J;
    } void Union (int x,int y) {int a=find (x), B=find (y);
    if (a==b) return;
    if (rank[a]>rank[b]) f[b]=a;
        else{if (rank[a]==rank[b]) ++rank[b];
    F[a]=b;   
    }} void Delete (int x) {f[id]=id;
majia[x]=id++;
    } int main () {char cmd[3];
    int A,b,cas=1;
        while (~SCANF ("%d%d", &n,&m) &&n+m) {init ();
            for (int i=0; i<m; ++i) {scanf ("%s", cmd);
                if (cmd[0]== ' M ') {scanf ("%d%d", &a,&b);
            Union (Majia[a],majia[b]);
                } else{scanf ("%d", &a);
Delete (a);            }} memset (flag, 0, sizeof (flag));
        int ans=0;
            for (int i=0; i<n; ++i) {a=find (majia[i]);
                if (!flag[a]) {++ans;
            Flag[a]=1;
    }} printf ("Case #%d:%d\n", cas++, ans);
} return 0; }


-The meaning of life is to give it meaning. original http://blog.csdn.net/shuangde800 , by d_double (reprint please indicate)






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.