Hdu2473 junk-Mail Filter (and query set (Virtual father) + Delete point)

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/u012860063

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2473


Problem descriptionrecognizing 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. inputthere 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. outputfor 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 6M 0 1M 1 2M 1 3S 1M 1 2S 33 1M 1 20 0
Sample output
Case #1: 3Case #2: 2
Source2008 Asia Regional Hangzhou

M a, B Represents a, B is a set, s a represents deleting a from the set, and finally there are several types of output

Train of Thought: Check the set, M indicates merging, and s indicates deleting. The following describes the delete operation.

We all know that the merge operation is to find the father of the two nodes and modify the father. If it is deleted, it is to reset the father of the node to himself. Will this work?

This cannot be done. For example, if the father of 1, 2, and 3 is both 1, the father of 1, 1 is deleted, and the father of 1, 2 is, and 3 is also 1. The correct one should be 2.

The father who deletes a node does not set himself to apply for a new node as the Father. For example, the father of 1, 2, and 3 is 1. In a set, delete 1 now, I applied for 4 as the father of 1, and both father and father of 2 were 1. Then find (2) to find the father of 2.

The father of 2 is 1, but the father of 1 is 4. Therefore, the father of 2 is updated to 4 and 3.

The correct method is to set up a virtual father at every point, for example, the father of 1, 2, and 3 is 4, 5, and 6 respectively. Now, merging 1, 2, and 3 is in a collection, and their father is 4, delete 1 now, apply for a node 7 again for 1

Now 2 and 3 fathers are 4 and 1 fathers are 7, and the deletion is successful.

(This idea is reproduced in detail)
The Code is as follows:
# Include <iostream> # include <cstring> using namespace STD; # define N records 47int father [N], flag [N]; int n, m, ID; int find (int x) {return father [x] = x? X: Father [x] = find (father [x]);} void Union (int x, int y) {int F1 = find (X ); int F2 = find (y); If (F1! = F2) Father [F2] = F1;} void Init () {int I; for (I = 0; I <n; I ++) {FATHER [I] = I + N; // virtual father} for (I = N; I <= N + m; I ++) {// n + N + M: It is possible to delete up to M nodes father [I] = I ;}} void delet (INT X) {FATHER [x] = ID ++;} int main () {int I, j, A, B, temp; int CAS = 0; char C [5]; while (~ Scanf ("% d", & N, & M) {id = N + N; Init (); If (n = 0 & M = 0) break; for (I = 0; I <m; I ++) {scanf ("% s", c); If (C [0] = 'M ') {scanf ("% d", & A, & B); Union (a, B);} else if (C [0] ='s ') {scanf ("% d", & temp); delet (temp) ;}} int cont = 0; memset (flag, 0, sizeof (FLAG )); for (I = 0; I <n; I ++) {int x = find (I); If (flag [x] = 0) // No co-parent {cont ++; flag [x] = 1 ;}} printf ("case # % d: % d \ n", ++ cas, cont);} 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.