[BOI2003]团伙

[BOI2003]团伙

也是并查集的扩展域写法.

这里的域相对于食物链要少一个,因为只有两种关系:敌人,朋友.

唯一需要注意的是 (:) 朋友的敌人没说是敌人 (...)

然后就没了...

(Code:)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define pii pair < int , int >
#define one first
#define two second
#define rint read<int>
#define pb push_back
#define db double
#define ull unsigned long long
#define lowbit(x) ( x & ( - x ) )

using std::queue ;
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;

template < class T >
    inline T read () {
        T x = 0 , f = 1 ; char ch = getchar () ;
        while ( ch < '0' || ch > '9' ) {
            if ( ch == '-' ) f = - 1 ;
            ch = getchar () ;
        }
       while ( ch >= '0' && ch <= '9' ) {
            x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
            ch = getchar () ;
       }
       return f * x ;
    }


const int N = 1e3 + 100 ;

int n , m , f[N*2] ;

inline int getf (int x) { return f[x] == x ? x : f[x] = getf ( f[x] ) ; }

inline void merge (int x , int y) {
    x = getf ( x ) ; y = getf ( y ) ;
    if ( x != y ) f[y] = x ; return ;
}

signed main (int argc , char * argv[]) {
    n = rint () ; m = rint () ;
    rep ( i , 1 , n * 2 ) f[i] = i ;
    char opt[2] ; int u , v ;
    while ( m -- ) {
        scanf ("%s" , opt ) ;
        u = rint () ; v = rint () ;
        if ( opt[0] == 'E' ) {
            merge ( u , v + n ) ;
            merge ( v , u + n ) ;
        } else merge ( u , v ) ;
    }
    rep ( i , 1 , n ) if ( f[i] == i ) ++ f[0] ;
    printf ("%d
" , f[0] ) ;
    #ifndef ONLINE_JUDGE
    system ("pause") ;
    #endif
    return 0 ;
}
原文地址:https://www.cnblogs.com/Equinox-Flower/p/11773806.html