AcWing 239.奇偶游戏 (带权并查集/种类并查集)

题意:你和朋友玩游戏,有个一\(01\)序列,你每次给出一个区间,朋友会回答这个区间中的\(1\)的个数是奇数还是偶数,但是你亲爱的朋友可能在撒谎,问在哪个询问你能确定你的朋友在撒谎,输出回合数.
题解:假如区间\([l,r]\)所含的奇数个数为偶数的话,那么其前缀和\(s_{l-1}\)和\(s_r\)所含的\(1\)的个数一定同奇同偶,如果\([l,r]\)所含奇数个数为奇数,\(s_{l-1}\)和\(s_r\)奇偶性一定不同.
所以我们对前缀和\(s\)进行维护,如果\([l,r]\)为偶数,那么我们可以将区间\(s_{l-1}\)和\(s_r\)合并,并且它们之间的权值应该为\(0\),若为奇数则权值为\(1\),传递关系可以用异或来操作,核心思想依然是带权并查集,但是这题还需要离散化.
此处顺便附上种类并查集的做法
代码:
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define db double
#define fi first
#define se second
#define pb push_back
#define me memset
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define per(a,b,c) for(int a=b;a>=c;--a)
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;} inline int read()
{
int X=0; bool flag=1; char ch=getchar();
while(ch<'0'|ch>'9') {if(ch=='-') flag=0; ch=getchar();}
while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
if(flag) return X;
return ~(X-1);
} int n;
int m;
int a,b;
string op;
int p[N];
int d[N];
unordered_map<int,int> S; int get(int x){
if(S.count(x)==0) S[x]=++n;
return S[x];
} int find(int x){
if(p[x]!=x){
int root=find(p[x]);
d[x]^=d[p[x]];
p[x]=root;
}
return p[x];
} int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); rep(i,1,10010) p[i]=i; cin>>n>>m;
n=0;
int ans=m; rep(i,1,m){
cin>>a>>b>>op;
a=get(a-1),b=get(b);
int fa=find(a);
int fb=find(b); int t=0;
if(op=="odd") t=1; if(fa==fb){
if((d[a]^d[b])!=t){
ans=i-1;
break;
}
}
else{
p[fa]=fb;
d[fa]=d[a]^d[b]^t;
}
} cout<<ans<<'\n'; return 0;
} ************************************************************** #include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define db double
#define fi first
#define se second
#define pb push_back
#define me memset
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define per(a,b,c) for(int a=b;a>=c;--a)
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;} inline int read()
{
int X=0; bool flag=1; char ch=getchar();
while(ch<'0'|ch>'9') {if(ch=='-') flag=0; ch=getchar();}
while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
if(flag) return X;
return ~(X-1);
} int n,m;
int p[N];
int a,b;
string op;
unordered_map<int,int> S; int get(int x){
if(S.count(x)==0) S[x]=++n;
return S[x];
} int find(int x){
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
} int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
int cnt=10010/2;
n=0;
rep(i,1,10010) p[i]=i;
int ans=m;
rep(i,1,m){
cin>>a>>b>>op; //p[x]存偶数,p[x+n]存奇数
a=get(a-1),b=get(b);
if(op=="even"){
if(find(a+cnt)==find(b)){
ans=i-1;
break;
}
p[find(a)]=find(b);
p[find(a+cnt)]=find(b+cnt);
}
else{
if(find(a)==find(b)){
ans=i-1;
break;
}
p[find(a)]=find(b+cnt);
p[find(a+cnt)]=find(b);
}
} cout<<ans<<'\n'; return 0;
}
AcWing 239.奇偶游戏 (带权并查集/种类并查集)的更多相关文章
- acwing 239. 奇偶游戏 并查集
地址 https://www.acwing.com/problem/content/241/ 小A和小B在玩一个游戏. 首先,小A写了一个由0和1组成的序列S,长度为N. 然后,小B向小A提出了M个 ...
- AcWing 239. 奇偶游戏
小A和小B在玩一个游戏. 首先,小A写了一个由0和1组成的序列S,长度为N. 然后,小B向小A提出了M个问题. 在每个问题中,小B指定两个数 l 和 r,小A回答 S[l~r] 中有奇数个1还是偶数个 ...
- 浅谈并查集&种类并查集&带权并查集
并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...
- AcWing:239. 奇偶游戏(前缀和 + 离散化 + 带权并查集 + 异或性质 or 扩展域并查集 + 离散化)
小A和小B在玩一个游戏. 首先,小A写了一个由0和1组成的序列S,长度为N. 然后,小B向小A提出了M个问题. 在每个问题中,小B指定两个数 l 和 r,小A回答 S[l~r] 中有奇数个1还是偶数个 ...
- Cogs 1070. [焦作一中2012] 玻璃球游戏 带权并查集,逆序处理
题目: http://cojs.tk/cogs/problem/problem.php?pid=1070 1070. [焦作一中2012] 玻璃球游戏 ★ 输入文件:marbles.in 输出 ...
- 洛谷P5092 [USACO2004OPEN]Cube Stacking 方块游戏 (带权并查集)
题目描述 约翰和贝茜在玩一个方块游戏.编号为 1\ldots n 1-n 的 n n ( 1 \leq n \leq 30000 1≤n≤30000 )个方块正放在地上,每个构成一个立方柱. 游戏开始 ...
- CDOJ 1070 秋实大哥打游戏 带权并查集
链接 F - 秋实大哥打游戏 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
- bzoj3376/poj1988[Usaco2004 Open]Cube Stacking 方块游戏 — 带权并查集
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3376 题目大意: 编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方 ...
- 【BZOJ 3376】[Usaco2004 Open]Cube Stacking 方块游戏 带权并查集
这道题一开始以为是平衡树结果发现复杂度过不去,然后发现我们一直合并而且只是记录到最低的距离,那么就是带权并查集了,带权并查集的权一般是到根的距离,因为不算根要好打,不过还有一些其他的,具体的具体打. ...
随机推荐
- Linux find 命令的初步实现(C++)
Implement a myfind command following the find command in UNIX operating system. The myfind command s ...
- Nginx Consul nginx-upsync-module
nginx consul nginx-upsync-module 依赖包: yum -y install libpcre3 libpcre3-dev ruby zlib1g-dev patch 下载n ...
- 根据业务摸索出的一个selenium代码模版(python)
前言 总算入行上班几个月了,不得不说业务是真的不消停啊.. 本人工作上经常遇到一种场景:为甲方做自动化接口处理工具,登录需要短信验证码,, 嘛算是摸索出了一套selenium代码模板,主要解决如下痛点 ...
- docker 镜像导入load、导出save以及重命名
docker 导入导出操作 save 保存(导出)镜像 # 把镜像打包成 .tar # -o 要保存路径.tar # > 要保存路径.tar # docker save 镜像id > /存 ...
- 【Oracle】instr()函数详解
1)instr()函数的格式 (俗称:字符查找函数) 格式一:instr( string1, string2 ) / instr(源字符串, 目标字符串) 格式二:instr( strin ...
- Arduino—学习笔记—基础语法
图解 函数具体讲解 pinMode(工作接脚,模式) 工作接脚 工作接脚编号(0--13与A0--A5) 模式 工作模式:INPUT或OUTPUT 例子 将8接口设置为输出模式 pinMode(8,O ...
- AmoebaNet:经费在燃烧,谷歌提出基于aging evolution的神经网络搜索 | AAAI 2019
论文提出aging evolution,一个锦标赛选择的变种来优化进化算法,在NASNet搜索空间上,对比强化学习和随机搜索,该算法足够简洁,而且能够更快地搜索到更高质量的模型,论文搜索出的Amoeb ...
- 【一天一个知识点系列】- Http之状态码
状态码 简介 HTTP 状态码负责表示客户端 HTTP 请求的返回结果. 标记服务器端的处理是否正常. 通知出现的错误等工作 作用及类别 作用:状态码告知从服务器端返回的请求结果 状态码的类别 注意: ...
- Mysql 中写操作时保驾护航的三兄弟!
这期的文章主要是讲述写操作过程中涉及到的三个日志文件,看过前几期的话可能你或多或少已经有些了解了(或者从别的地方也了解过).比如整个写操作过程中用到的两阶段提交,又或者是操作过程中涉及到的日志文件,但 ...
- Jmeter的Cookie管理器调试与参数化
默认系统都是需要登录,才能操作其它接口,所以需要添加一个HTTP Cookie 管理器,默认Cookie管理器是关闭的,需要修改jmeter配置文件jmeter.properties,该文件在jme ...