Click to open link
Test instructions: Two operations are given, M U v connects u to V, S u takes u out of its collection, but does not change the existing relationship of other elements in the collection
Idea: is directly and check set, and then have a delete point of operation, which with the previous write Zoj 3789 delete operation is the same, find the number of groups instead of can, simple ~ ~ ~ PS: Add a path compression run or so slow, org
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <
Algorithm> using namespace std;
typedef long Long LL;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const LL INT=0X3F3F3F3F3F3F3F3FLL;
const int maxn=1100010;
int F[MAXN],FF[MAXN];
int find1 (int x) {int k,r,j;
R=x;
while (R!=f[r]) r=f[r];
K=x;
while (k!=r) {j=f[k];
F[k]=r;
K=j;
} return R;
} void Unite (int a,int b) {int aa=find1 (a);
int Bb=find1 (b);
if (AA==BB) return;
F[AA]=BB;
} int VIS[MAXN];
int main () {int n,m,u,v,cas=1;
Char ch[10];
while (scanf ("%d%d", &n,&m)!=-1) {if (n==0&&m==0) break;
for (int i=0;i<=n+m;i++) {f[i]=ff[i]=i;vis[i]=0;
} int ans=n,sum=0;
for (int i=0;i<m;i++) {scanf ("%s", ch);
if (ch[0]== ' M ') {scanf ("%d%d", &u,&v); Unite (Ff[u],ff[v]);
}else if (ch[0]== ' S ') {scanf ("%d", &u);
Ff[u]=n++;f[n]=n;
}} for (int i=0;i<ans;i++) {int t=find1 (ff[i]);
Vis[t]=1;
} for (int i=0;i<n;i++) if (vis[i]) sum++;
printf ("Case #%d:%d\n", cas++,sum);
} return 0;
}