Problem descriptionrecognizing 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.
Inputthere is multiple test cases in the input file.
Each test case starts with integers, N and M (1≤n≤105, 1≤m≤106), the number of email samples and the number O F 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.
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 1M 1 20 0
Sample Output
Case #1:3Case #2:2
Test instructions: S is the point that is stripped out of the set and M is united! To find out the last piece of Unicom
Idea: Is and check the set of delete operation! Open an array to record the mapping position of the current point! There is no other algorithm that can separate the nodes of the set and not affect its structure. So the point of separation should be mapped to the point that did not appear before, the merging operation is also to operate the map array!
So the AC code:
#include <cstdio> #include <cstring> #include <algorithm> #include <set>using namespace std; const int Maxn=100000+10;int f[maxn*10],a[maxn];int Find (int x) {if (x!=f[x]) F[x]=find (f[x]); return f[x];} void Union (int x,int y) {x=find (x); Y=find (y); if (x==y) return; F[x]=y;} Set<int>s;int Main () {#ifndef Online_judge freopen ("In.cpp", "R", stdin); Freopen ("Out.cpp", "w", stdout); #endif//Online_judge int n,m; Char str[2]; int Cas=1; while (scanf ("%d%d", &n,&m) ==2&& (n+m)) {s.clear (); for (int i=0;i<=n;i++) {f[i]=i; A[i]=i; } int num=n; while (m--) {scanf ("%s", str); if (str[0]== ' S ') {int k; scanf ("%d", &k); a[k]=num++; F[A[K]]=A[K]; } else {int x, y; scanf ("%d%d",&x,&y); Union (A[x],a[y]); }} for (int i=0;i<n;i++) {S.insert (Find (A[i])); } printf ("Case #%d:%d\n", cas++,s.size ()); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2473 Junk-mail Filter (and the delete operation of the check set)