3034: 【普及+/提高】【P1892】团伙

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:0 解决:0

题目描述

现在有 lns="http://www.w3.org/1998/Math/MathML"> 个人,他们之间有两种关系:朋友和敌人。我们知道:

  • 一个人的朋友的朋友是朋友
  • 一个人的敌人的敌人是朋友

现在要对这些人进行组团。两个人在一个团体内当且仅当这两个人是朋友。请求出这些人中最多可能有的团体数。

输入

第一行输入一个整数 lns="http://www.w3.org/1998/Math/MathML"> 代表人数。

第二行输入一个整数 lns="http://www.w3.org/1998/Math/MathML"> 表示接下来要列出 lns="http://www.w3.org/1998/Math/MathML"> 个关系。

接下来 lns="http://www.w3.org/1998/Math/MathML"> 行,每行一个字符 lns="http://www.w3.org/1998/Math/MathML"> 和两个整数 lns="http://www.w3.org/1998/Math/MathML">,,分别代表关系(朋友或敌人),有关系的两个人之中的第一个人和第二个人。其中 lns="http://www.w3.org/1998/Math/MathML"> 有两种可能:

  • 如果 lns="http://www.w3.org/1998/Math/MathML"> 为 F,则表明 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML"> 是朋友。
  • 如果 lns="http://www.w3.org/1998/Math/MathML"> 为 E,则表明 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML"> 是敌人。

输出

一行一个整数代表最多的团体数。

样例输入 复制

6
4
E 1 4
F 3 5
F 4 6
E 1 2

样例输出 复制

3

提示

对于 lns="http://www.w3.org/1998/Math/MathML">100% 的数据,lns="http://www.w3.org/1998/Math/MathML">21000lns="http://www.w3.org/1998/Math/MathML">15000lns="http://www.w3.org/1998/Math/MathML">1,

#include<bits/stdc++.h>
using namespace std;
int f[2005];
int find(int x) {
	if (f[x] != x ) f[x] = find(f[x]);
	return f[x];
}
int main(){
    int n,m,p,q,ans=0;
	char opt;
	cin >> n >> m;
	for (int i = 1; i <= 2*n ; i++) f[i] = i;
	for (int i = 0; i < m; i++) {
		cin >> opt >> p >> q;
		int t1=find(p), t2=find(q);
		if (opt == 'F' ) {
			f[t1] = t2;
		}else {
		    int t3=find(p+n),t4=find(q+n);
		    f[t3] = t2;
		    f[t4] = t1;
		}
	}
	for (int i = 1; i <=n ; i++) {
		if (i == find(i)) ans++;
	}
	cout << ans;
	return 0;
}