HDU 2473 Junk-Mail Filter [query set + create virtual parent node (vest)]

Source: Internet
Author: User

Original question:
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 wocould be helpful. the tool shocould 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 shocould remove all relationships that spam X has when this command is already ed; 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 shocould 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
 

Analysis and Summary:
There are N emails, and then two other operations. If it is m x y, X and Y are the same emails. If it is s x, it indicates that the judgment on X is incorrect. X does not belong to the current set of X, and X should be separated to make X a separate one.

It is obvious that the query set is used. One of the reasons for obfuscation is that, assuming the following situations:
M 0 2
M 1 2
S 2
Then follow the parallel query set. 0 points to 2, 1 points to 2, that is
0 --> 2
1 --> 2,
After deleting 2, I thought the question meant that all the things related to 2 should be deleted, and the two relationships should be removed and changed to three independent ones.
However, this is wrong. After merging, it is a set {0, 1, 2}. If 2 is deleted, {0, 1} is the set.

After understanding the meaning of the question, we know that it is easy to construct a set by using and querying the set, but it is not easy to delete one of the sets. Through this question, I learned how to set up a parent node.
The key process is to delete x points. Instead of deleting x points, a ing is used (the array majia [N] is used here). convert x into a new vertex, that is, majia [x] = newNode. the original sets remain unchanged, but an x point is missing.


Code:
[Cpp]
# 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] =;
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", & n, & m) & n + m ){
Init ();
For (int I = 0; I <m; ++ I ){
Scanf ("% s", cmd );
If (cmd [0] = 'M '){
Scanf ("% d", & a, & B );
Union (majia [a], majia [B]);
}
Else {
Scanf ("% d", & );
Delete ();
}
}
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;
}

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.