HDU 2473 and check the collection

Source: Internet
Author: User
Tags cas

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;
 }

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.