HDU 2473 junk-mail Filter and check Set delete

Source: Internet
Author: User

Click the Open link http://acm.hdu.edu.cn/showproblem.php?pid=2473

Test instructions: Gives n operations, M a b indicates that A and B are the same and look up the set, S A is in and check the focus delete A, note that if 1 and 2 is a and check set, 2 and 3 is a and check set, then 1 and 3 is also a check set, even if the deletion of 2, we can still get 1 and 3 or a collection.

Idea: Because and check set is a tree structure, unable to delete a point after the tree species will continue to maintain the previous appearance, so we delete is to take the action is to use the map, the beginning of all points of the mapping is self, and then delete the operation is to map this point to another nonexistent point, So the original point is still in the original collection, used to maintain the various properties of the original collection, after which all operations on the point is the point of its mapping operation. Unlike the general and look-up set, finding the father's operation is not the father of the point itself, but the mapping of all points to the father.


<pre name= "code" class= "CPP" > #include <cstdio> #include <cstring> #include <iostream>using    namespace Std;const int maxn=2000100;int f[maxn],id[maxn];bool a[maxn];int fin (int x) {if (x==f[x]) return x;    F[x]=fin (F[x]); return f[x];}    int main () {int cas=1;    int n,m;        while (cin>>n>>m) {if (!n&&!m) break;            for (int i=0;i<maxn;i++) {f[i]=i;            Id[i]=i;        a[i]=0;        } char S;        int b,c;        int num=n;            for (int i=0;i<m;i++) {getchar ();            scanf ("%c", &s);                if (s== ' M ') {scanf ("%d%d", &b,&c);                int Fb=fin (id[b]);                int Fc=fin (id[c]);            if (FB!=FC) F[FB]=FC;                } else{scanf ("%d", &b);            id[b]=num++;        }} int ans=0;  for (int i=0;i<n;i++) if (A[fin (Id[i])]==0) {          ans++;        A[fin (Id[i])]=1;    } printf ("Case #%d:%d\n", Cas++,ans); } return 0;}



HDU 2473 junk-mail Filter and check Set 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.