当前位置:网站首页>Luogu p1892 [boi2003] Gang (and search for variant anti set)
Luogu p1892 [boi2003] Gang (and search for variant anti set)
2022-07-02 10:58:00 【Morgannr】
[BOI2003] gangs
Title Description
Now there is n n n personal , There are two relationships between them : Friends and enemies . We know :
- A person's friend's friend is a friend
- A man's enemy is his friend
Now we need to form a group of these people . Two people in a group if and only if they are friends . Ask for the maximum number of possible groups of these people .
Input format
Enter an integer in the first line n n n Number of Representatives .
On the second line, enter an integer m m m It means to list m m m A relationship .
Next m m m That's ok , One character per line o p t opt opt And two integers p , q p,q p,q, They represent the relationship ( Friends or enemies ), The first and second of the two people involved . among o p t opt opt There are two possibilities :
- If o p t opt opt by
F, It means p p p and q q q It's a friend. . - If o p t opt opt by
E, It means p p p and q q q It's the enemy .
Output format
One integer per line represents the largest number of groups .
Examples #1
The sample input #1
6
4
E 1 4
F 3 5
F 4 6
E 1 2
Sample output #1
3
Tips
about 100 % 100\% 100% The data of , 2 ≤ n ≤ 1000 2 \le n \le 1000 2≤n≤1000, 1 ≤ m ≤ 5000 1 \le m \le 5000 1≤m≤5000, 1 ≤ p , q ≤ n 1 \le p,q \le n 1≤p,q≤n.
The question :
My friend's friend is my friend , My enemy's enemy is my friend , The final output Number of gangs . Be careful , Two people in a group if and only if they are friends .
Ideas :
It is easy to think of this problem as side weighted and search set , But it's not , According to the original problem surface of this problem , In fact, there can be no relationship between two people : May have never met , Or the police department doesn't know their relationship . The translation on Luo Gu said that everyone has a relationship .
Experience that can be accumulated in this topic :
- For the relationship between two people , There can be no relationship , Record relationships that cannot be merged , Merge people who can be merged when you find them again .
- For the relationship between two people , There must be a relationship , use Weighted and search set .
For this question , We introduce a new approach : Anti set of union search set .
a Express a My friends ,a + n Express a Enemy set of
- If
aandbIt's a friend. , that takeaandbMy friends Just connect :p[Find(a)] = Find(b); - If
aandbIt's the enemy , be takeaThe enemy of sets withbMy friends Connected to a , takebThe enemy of sets withaMy friends Connected to a , namely :p[Find(a + n)] = Find(b),p[Find(b + n)] = Find(a)
Finally, count the number of gangs ,1 ~ n in ( Friends collection ) all p[i] == i The number of people is the answer . Because according to the meaning of the title , All gangs must be distributed in the concentration of friends , therefore as long as Count the number of nodes without ancestors in the friend set .
Code :
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
//#define int long long
int n, m;
const int N = 1010;
int p[N << 1];
int Find(int x)
{
if (p[x] != x) p[x] = Find(p[x]);
return p[x];
}
signed main()
{
int T = 1; //cin >> T;
while (T--)
{
cin >> n >> m;
int res = 0;
for (int i = 1; i < N<<1; ++i) p[i] = i;
for (int i = 1; i <= m; ++i)
{
char op[2]; int a, b;
cin >> op >> a >> b;
int pa = Find(a), pb = Find(b);
int pan = Find(a + n), pbn = Find(b + n);
if (*op == 'F')
{
p[pb] = pa;
}
else
{
p[pbn] = pa;
p[pan] = pb;
}
}
for (int i = 1; i <= n; ++i) if (p[i] == i) ++res;
cout << res << '\n';
}
return 0;
}
边栏推荐
- Pywin32 opens the specified window
- "Matching" is true love, a new attitude for young people to make friends
- PCL之K-d树与八叉树
- 【ARK UI】HarmonyOS ETS的启动页的实现
- Overview of integrated learning
- PCL 点云转深度图像
- Mongodb quickly get started with some simple operations of mongodb command line
- Oracle 笔记
- js promise. all
- 6种单例模式的实现方式
猜你喜欢

13. Semaphore critical zone protection

Use WinDbg to statically analyze dump files (summary of practical experience)

二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)

"Matching" is true love, a new attitude for young people to make friends

Sus system availability scale

MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
![[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)](/img/9f/4265f1e3927fcf66602f0fc9e7a9d9.jpg)
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)

Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application

Ks009 implement pet management system based on SSH

The nanny level tutorial of flutter environment configuration makes the doctor green to the end
随机推荐
二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)
Overview of integrated learning
14. Code implementation of semaphore
axis设备的rtsp setup头中的url不能带参
[TS] 1368 seconds understand typescript generic tool types!
2.hacking-lab脚本关[详细writeup]
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
Learn open62541 -- [66] UA_ Generation method of string
全网显示 IP 归属地,是怎么实现的?
华为游戏初始化init失败,返回错误码907135000
如何用list组件实现tabbar标题栏
从.bag文件中读取并保存.jpg图片和.pcd点云
JSP webshell免杀——webshell免杀
Retrofit's callback hell is really vulnerable in kotlin synergy mode!
二叉树专题--AcWing 1589. 构建二叉搜索树
Open the encrypted SQLite method with sqlcipher
MySQL lethal serial question 3 -- are you familiar with MySQL locks?
【ARK UI】HarmonyOS ETS的启动页的实现
软件产品管理系统有哪些?12个最佳产品管理工具盘点