团伙
题目背景:
分析:并查集,对于两个敌人,将x与y + n,y与x + n,合并,然后对于朋友,将x与y合并,然后就可以符合题目要求了,敌人的敌人是朋友,朋友的朋友是朋友,注意对于朋友,x + n与y + n不能合并,因为没有说朋友的敌人一定是敌人。最后统计1 ~ n中有多少集合就可以了
Source:
/*
created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
inline char read() {
static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}
///*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}
inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}
/*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
if (c == '-') iosig = true;
for (x = 0; isdigit(c); c = getchar())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int MAXN = 1000 + 10;
int n, m, x, y;
int p[MAXN << 1], father[MAXN << 1];
bool exist[MAXN << 1];
char c;
inline int get_father(int x) {
return (father[x] == x) ? x : (father[x] = get_father(father[x]));
}
inline void unite(int x, int y) {
int fa1 = get_father(x), fa2 = get_father(y);
if (fa1 != fa2) father[fa2] = fa1;
}
int main() {
R(n), R(m);
for (int i = 1; i <= (n << 1); ++i) father[i] = i;
for (int i = 1; i <= m; ++i) {
c = read(), R(x), R(y);
if (c == 'E') unite(x, y + n), unite(y, x + n);
else unite(x, y);
}
int cnt = 0;
for (int i = 1; i <= n; ++i)
if (!exist[x = get_father(i)]) exist[x] = true, ++cnt;
std::cout << cnt;
}