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