HDU 2473 junk-mail Filter (and check node delete)

Source: Internet
Author: User

Junk-mail Filter

Time limit:15000/8000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 7254 Accepted Submission (s): 2307


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 Input5 6M 0 1M 1 2M 1 3S 1M 1 2S 1M 1 20 0

Sample outputcase #1:3Case #2:2

Source2008 Asia Regional Hangzhou test Instructions: There are n points, you can do two operations on the point, M u v is to merge the two sets of point u,v, S U is to remove the point u from the interval in which it is located, Ask how many sets of analysis are in the end: If you merge the collection of two points, you can do it, but the question is, how do you remove the node from the collection? You might think of pointing the root node to yourself, but how do you deal with relationships with other points? So it's not going to work. Traditional and check set on the basis of supporting the merger does not carry out a node from the collection of operations, so to find another way, how to do it? We can set up the virtual node. This means that if a bit is deleted, you can set a new node instead of it for example: there are 4 points {1,2,3,4} has two sets a={1,2,3} b={4}, if the set of a 3 is removed from the set, then we can set a new node 5来 instead of Node 3, and then get a={ B={4} c={5} after "point 3" for the operation of Point 5 is used in the Vir[i] array to denote the actual number of nodes numbered I is vir[i], then the Union (Vir[u],vir[v]) is the point you refer to point u vir[u] and point v Point Vir[v] to merge, Del (U) is the "delete" in the collection of points u, relatively equivalent to generate a new node. The number of the last set is the number of root nodes.
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<string>#include<iostream>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<vector>#include<map>#include<stdlib.h>#include<algorithm>#defineLL __int64using namespacestd;Const intmaxn=1e6+1e5+5;intP[MAXN];intVIS[MAXN];intVIR[MAXN];//stores the number of virtual pointsintn,m,cnt;intinit () { for(intI=0; i<=maxn;i++) p[i]=i;  for(intI=0; i<n;i++) vir[i]=i; memset (Vis,0,sizeof(VIS)); CNT=N;}intFINDFA (intx) {    returnp[x]==x?x:p[x]=Findfa (p[x]);}voidUnion (intUintv) {    intx=FINDFA (U); inty=Findfa (v); if(x!=y) p[x]=y;}voidDelintu) {Vir[u]=cnt++;}intMain () {Freopen ("In.txt","R", stdin); intCase=0;  while(SCANF ("%d%d", &n,&m) && (n| |m)) {init ();  while(m--)        {            Charopt[Ten]; scanf ("%s", opt); if(opt[0]=='M')            {                intu,v; scanf ("%d%d",&u,&v);            Union (Vir[u],vir[v]); }            Else            {                intu; scanf ("%d",&T);            Del (U); }        }         for(intI=0; i<n;i++) Vis[findfa (Vir[i])=1; intans=0;  for(intI=0; i<cnt;i++)            if(Vis[i]) ans++; printf ("Case #%d:%d\n",++Case,ans); }    return 0;}
View Code

HDU 2473 junk-mail Filter (and check node delete)

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.