Gang

Title Description

1920 Chicago, there was a group of bandits. If the two robbers met, then they are either friends or enemies. And one thing is certain, that is:

My friend's friend is my friend;

My enemy's enemy is my friend.

Two robbers condition is the same gang if and only if they are friends. Now to give you some information about the robbers, you have to ask the maximum number of bandit gangs.

Input and output formats

Input formats:

 

The first line of the input file is an integer gangs.in N (2 <= N <= 1000), represents the number of robbers (numbered from 1 to N). The second line M (1 <= M <= 5000), indicates the number of pieces of information about the robbers. The following M rows, each row may be F pq or E pq (1 <= p q <= N), F represents p and q are friends, E is p and q are the enemy. Input data to ensure that no conflicting information.

 

Output formats:

 

Output file gangs.out only one line that indicates the maximum number of groups possible.

 

Sample input and output

Input Sample # 1:  Copy
6
4
E 1 4
F 3 5
F 4 6
E 1 2
Output Sample # 1:  Copy
3 


words disjoint-set templates do really, why find three questions of () are not the same 2333333

#include<cstdio>
#include<iostream>

using namespace std;

int n,m;

int father[250005];

int find(int x){
    if(father[x]!=x) 
    father[x]=find(father[x]);    
    return father[x];
}

int main(){
    scanf("%d",&n);
    scanf("%d",&m);
    for(int k=1;k<=n*2;k++){
        father[k]=k;
    }
    for(int i=1;i<=m;i++){
        char z;
        int x,y;
        cin>>z>>x>>y;
        if(z=='F'){
            father[find(x)]=find(y);
        }
        if(z=='E'){
            father[find(x+n)]=find(y);
            father[find(y+n)]=find(x); 
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        if(father[i]==i){
            ans++;
        }
    }
    printf("%d",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hrj1/p/11129772.html