Fork me on GitHub

[BOI2003]团伙

[BOI2003]团伙

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

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

唯一需要注意的是 $:$ 朋友的敌人没说是敌人 $…$

然后就没了…

$Code:$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#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\n" , f[0] ) ;
#ifndef ONLINE_JUDGE
system ("pause") ;
#endif
return 0 ;
}