Hdu 2473 junk-mail Filter (and check Set _ virtual node)

Source: Internet
Author: User

Feel some difficult problem, just started to think of the establishment of virtual node, but the implementation is always wrong, because each time the virtual node is established, the original node and virtual node can not be separated, resulting in virtual node is meaningless.

The above is purely nonsense, can ignore ...

Test instructions--

Given n points (0, 1, 2, ..., n-1), two operations can be performed: 1. Merges two points into a set; 2. Remove a point from the original collection. Ask the last point there are several sets.

It is obvious that the check set, including merge, delete operation.

However, when you delete a node, you need to ensure that the collection, in addition to the other nodes of the deleted node, is not changed, which is somewhat difficult to handle.

We know that, and check set is actually a tree, we will delete a node in the tree, but also to ensure that the structure of the tree is not changed, you can use a method, that is, "virtual node", that is, we do not delete the node, but will need to delete the node stored in a new node, the node is independent of this tree. In this way, when we query that node, we will query to the new node, and the original node's function is only to keep the tree structure.

Therefore, we need two arrays, an array is fm[n+m], an array is fle[n], where the fle[i of the fle[] array represents the value of the I-node, and the fm[] array, fm[fle[i]] is used to hold the parent node of the first node, the fle[i].

The point is that this fle[], each time I delete node I, we do not change fm[fle[i]], so that the structure of the tree will not change, and we give Fle[i] a new value has never been used, so that the actual I node changes.

So every time I delete the I node, I only need to give fle[i] a new value never used, similar to the I node for a new vest, you can solve the problem.

We can start with a value of N, the value assigned to the node is n++, so that, because up to M operations, then a maximum of M vest, therefore, the size of the FM is m+n.

On the code--

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 7 Const intN =100010;8 Const intM =1000010;9 Ten BOOLvis[n+M]; One intfm[n+M], fle[n]; A intN, M, A, B, pt, ans, TM; - Chars[Ten]; -  the voidInit () - { -      for(inti =0; I < n; i++) -     { +Fm[i] =i; -Fle[i] =i; +     } Amemset (Vis,0,sizeof(Vis)); atPT =N; -Ans =0; - } -  - intMfind (inta) - { in     intFA =A; -  to      while(FA! = Fm[fa]) FA =FM[FA]; +      while(A! =Fm[a]) -     { the         intMID =Fm[a]; *Fm[a] =FA; $A =mid;Panax Notoginseng     } -     returnFA; the } +  A voidMmerge () the { +     inthan=Mfind (Fm[fle[a]); -     intFB =Mfind (Fm[fle[b]); $     if(FA! = FB) FM[FA] =FB; $ } -  - voidDmerge () the { -Fle[a] = pt;//Change the vest .WuyiFM[PT] = pt;//initializes the parent node to this new value thept++;//prepare for the new vest. - } Wu  - voidWork () About { $      while(m--) -     { -scanf"%s", s); -         if(s[0] =='M') A         { +scanf"%d%d", &a, &b); the Mmerge (); -         } $         Else if(s[0] =='S') the         { thescanf"%d", &a); the Dmerge (); the         } -     } in  the      for(inti =0; I < n; i++) the     { About         intance =Mfind (Fle[i]); the         if(!Vis[ance]) the         { theans++; +Vis[ance] =1; -         } the     }Bayiprintf"Case #%d:%d\n", tm++, ans); the } the  -  -  the intMain () the { the     //freopen ("test.in", "R", stdin); theTM =1; -      while(~SCANF ("%d%d", &n, &m) && (n+m)) the     { the init (); the Work ();94     } the}
View Code

PS: These days the state has been bad, but today suddenly saw a word, feeling from the heart, finally resistant to the heart to understand the problem--if the heart does not inhabit the place, where are wandering

Come on, let's go.

Hdu 2473 junk-mail Filter (and check Set _ virtual node)

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.