P1892

酷狗没会员了QAQ

#include<bits/stdc++.h>
using namespace std;
int n,m,f[1001],enm[1001];  
int find(int x)           {
    if(f[x]!=x) x=find(f[x]);
    return x;
}
void hebing(int x,int y)    {
    x=find(x);y=find(y); 
    if(x==y) return;
    f[y]=x;
    return;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)f[i]=i;
    for(int i=1;i<=m;i++){
        int p,q;
        char c;
        cin>>c>>p>>q;
        if(c=='F') hebing(p,q);   
        else {
            if(enm[p]==0) enm[p]=find(q);
            else hebing(q,enm[p]); 
            if(enm[q]==0) enm[q]=find(p);
            else hebing(p,enm[q]);
        } 
    }
    int count[1001]={0};
    for(int i=1;i<=n;i++)count[find(i)]++;
    int cnt=0;
    for(int i=1;i<=n;i++)if(count[i]) cnt++; 
    cout<<cnt;
 }
posted @ 2024-10-22 11:16  yzc_is_SadBee  阅读(1)  评论(0编辑  收藏  举报