BZOJ 1370: [Baltic2003]Gang团伙 [并查集 拆点 | 种类并查集WA]
题意:
朋友的朋友是朋友,敌人的敌人是朋友;朋友形成团伙,求最多有多少团伙
种类并查集WA了一节课,原因是,只有那两种关系才成立,诸如朋友的敌人是朋友之类的都不成立!
所以拆点做吧
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int n, m, fa[N], val[N], x, y, cc[N][]; char s[];
inline int find(int x) {
if(x == fa[x]) return x;
int root = find(fa[x]);
val[x] ^= val[fa[x]];
return fa[x] = root;
}
inline void Union(int x, int y, int p) {
int f1 = find(x), f2 = find(y);
if(f1 != f2) {
fa[f1] = f2;
val[f1] = val[x]^val[y]^p;
} else {
if( (val[x]^val[y]) != p) while();
}
} int main() {
freopen("in","r",stdin);
n=read(); m=read();
for(int i=; i<=n; i++) fa[i]=i;
for(int i=; i<=m; i++) {
scanf("%s",s); x=read(), y=read();
Union(x, y, s[] == 'F' ? : );
}
int ans=;
for(int i=; i<=n; i++) cc[find(i)][val[i]] = ;
for(int i=; i<=n; i++) ans += cc[i][] + cc[i][];
printf("%d",ans);
}
种类并查集
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int n, m, fa[N], x, y, a[N], ans; char s[];
inline int find(int x) {return x==fa[x] ? x : fa[x]=find(fa[x]);}
inline void Union(int x, int y) {
x = find(x), y = find(y);
if(x != y) fa[x] = y;
} int main() {
freopen("in","r",stdin);
n=read(); m=read();
for(int i=; i<=n*; i++) fa[i]=i;
for(int i=; i<=m; i++) {
scanf("%s",s); x=read(), y=read();
if(s[]=='F') Union(x, y);
else Union(x, y+n), Union(x+n, y);
}
for(int i=; i<=n; i++) a[i]=find(i);
sort(a+, a++n); ans=unique(a+, a++n) - a - ;
printf("%d",ans);
}
BZOJ 1370: [Baltic2003]Gang团伙 [并查集 拆点 | 种类并查集WA]的更多相关文章
- BZOJ 1370: [Baltic2003]Gang团伙(luogu 1892)(种类并查集)
题面: bzoj题面有误,还是看luogu的吧 https://www.luogu.org/problemnew/show/P1892 题解: 种类并查集.. 因为有敌人的敌人是朋友这个条件,所以需要 ...
- BZOJ 1370 [Baltic2003]Gang团伙:并查集【虚点】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1370 题意: 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: (1)我 ...
- [BZOJ1370][Baltic2003]Gang团伙
[BZOJ1370][Baltic2003]Gang团伙 试题描述 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: ...
- [BZOJ1370][Baltic2003]Gang团伙 并查集+拆点
Description 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: 所有是朋友的人组成一个团伙.告诉你关于这n个 ...
- POJ1417 True Liars 并查集 动态规划 (种类并查集)
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ1417 题意概括 有一群人,p1个好人,p2个坏人. 他们说了n句话.(p1+p2<=600,n ...
- 并查集例题01. 种类并查集(poj1733)
题目: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第y个 ...
- 【bzoj1370】[Baltic2003]Gang团伙 并查集
题目描述 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: 所有是朋友的人组成一个团伙.告诉你关于这n个人的m条信息, ...
- 洛谷 P2024 [NOI2001]食物链(种类并查集,加权并查集)
传送门 解题思路 加权并查集: 什么是加权并查集? 就是记录着每个节点到它的父亲的信息(权值等). 难点:在路径压缩和合并节点时把本节点到父亲的权值转化为到根节点的权值 怎么转化呢? 每道题都不一样Q ...
- 浅谈并查集&种类并查集&带权并查集
并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...
随机推荐
- hdu_3483A Very Simple Problem(C(m,n)+快速幂矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3483 A Very Simple Problem Time Limit: 4000/2000 MS ( ...
- UVa 725 简单枚举+整数转换为字符串
Division Write a program that finds and displays all pairs of 5-digit numbers that between them use ...
- use ambiguous的错误——编译错误
出现这样的问题是因为namespace std里面已经有一个count了,而 using namespace std;语句把该namespace 打开了,这导致了后面的引用不明确: 不过这里也可以把u ...
- UEP-时间
时间戳转化为Date(or String) SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ...
- oracle游标的使用
--游标的使用 步骤1.声明游标 .打开游标 .获取数据 .关闭游标 declare cursor cur is select * from emp; t_sal cur%rowtype; begin ...
- 用于 C♯ 图像识别的轮廓分析技术
用于 C♯ 图像识别的轮廓分析技术 供稿:Conmajia 标题:Contour Analysis for Image Recognition in C# 作者:Pavel Torgashov 此中文 ...
- 自定义alert弹框
/**************** UIAlertControllerStyleAlert *************************/ /*创建一个 可以自定义文字颜色和字体大小的IAler ...
- LAMP LNMP 和 LNMPA
LAMP指的是:Linux+Apache+MySQL+Perl/PHP/Python LAMP是一个缩写,它指一组通常一起使用来运行动态网站或者服务器的自由软件: Linux,操作系统: Apach ...
- Varnish的vcl子程序
Varnish的VCL子程序 以下内容参考: http://book.varnish-software.com/4.0/ VCL子进程,在其中定制Varnish的行为.VCL子例程可用于:添加自定义标 ...
- DALI 2.0解码模块
DALI2.0调光解码模块使用手册 一.概述(联系人:张先生,电话:13923882807,QQ:813267849) 欢迎使用本公司的DALI 2.0解码模块,该模块支持"DALI第二套协 ...