• 题意:你和朋友玩游戏,有个一\(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.奇偶游戏 (带权并查集/种类并查集)的更多相关文章

  1. acwing 239. 奇偶游戏 并查集

    地址  https://www.acwing.com/problem/content/241/ 小A和小B在玩一个游戏. 首先,小A写了一个由0和1组成的序列S,长度为N. 然后,小B向小A提出了M个 ...

  2. AcWing 239. 奇偶游戏

    小A和小B在玩一个游戏. 首先,小A写了一个由0和1组成的序列S,长度为N. 然后,小B向小A提出了M个问题. 在每个问题中,小B指定两个数 l 和 r,小A回答 S[l~r] 中有奇数个1还是偶数个 ...

  3. 浅谈并查集&种类并查集&带权并查集

    并查集&种类并查集&带权并查集 前言: 因为是学习记录,所以知识讲解+例题推荐+练习题解都是放在一起的qvq 目录 并查集基础知识 并查集基础题目 种类并查集知识 种类并查集题目 并查 ...

  4. AcWing:239. 奇偶游戏(前缀和 + 离散化 + 带权并查集 + 异或性质 or 扩展域并查集 + 离散化)

    小A和小B在玩一个游戏. 首先,小A写了一个由0和1组成的序列S,长度为N. 然后,小B向小A提出了M个问题. 在每个问题中,小B指定两个数 l 和 r,小A回答 S[l~r] 中有奇数个1还是偶数个 ...

  5. Cogs 1070. [焦作一中2012] 玻璃球游戏 带权并查集,逆序处理

    题目: http://cojs.tk/cogs/problem/problem.php?pid=1070 1070. [焦作一中2012] 玻璃球游戏 ★   输入文件:marbles.in   输出 ...

  6. 洛谷P5092 [USACO2004OPEN]Cube Stacking 方块游戏 (带权并查集)

    题目描述 约翰和贝茜在玩一个方块游戏.编号为 1\ldots n 1-n 的 n n ( 1 \leq n \leq 30000 1≤n≤30000 )个方块正放在地上,每个构成一个立方柱. 游戏开始 ...

  7. CDOJ 1070 秋实大哥打游戏 带权并查集

    链接 F - 秋实大哥打游戏 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submit ...

  8. bzoj3376/poj1988[Usaco2004 Open]Cube Stacking 方块游戏 — 带权并查集

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3376 题目大意: 编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方 ...

  9. 【BZOJ 3376】[Usaco2004 Open]Cube Stacking 方块游戏 带权并查集

    这道题一开始以为是平衡树结果发现复杂度过不去,然后发现我们一直合并而且只是记录到最低的距离,那么就是带权并查集了,带权并查集的权一般是到根的距离,因为不算根要好打,不过还有一些其他的,具体的具体打. ...

随机推荐

  1. 网络之HTTPS

    文章目录 HTTPS的基本概念 HTTP和HTTPS的区别 HTTPS的优点 对称加密和非对称加密 对称加密 非对称加密 HTTPS采用的加密方式 认证 证书的组成 使用openssl怎么制造证书 H ...

  2. docker 创建数据卷容器

    数据卷容器 --volumes-from 容器名/id 先起一个容器 docker run -it --name docker01 centos 然后同步 docker01 的数据卷 --volume ...

  3. 【Linux】linux rinetd 端口转发部署

    linux下简单好用的工具rinetd,实现端口映射/转发/重定向 Rinetd是为在一个Unix和Linux操作系统中为重定向传输控制协议(TCP)连接的一个工具.Rinetd是单一过程的服务器,它 ...

  4. 图像分割论文 | DRN膨胀残差网络 | CVPR2017

    文章转自:同作者个人微信公众号[机器学习炼丹术].欢迎交流沟通,共同进步,作者微信:cyx645016617 论文名称:'Dilated Residual Networks' 论文链接:https:/ ...

  5. Vue案例之todoLIst实现

    使用Vue实现todolist案例,如有不对敬请大佬多多指教 功能: 1.增加功能:在新增版块里面的输入框内输入数据,点击后面的"添加"按钮,将输入的数据添加到列表中,默认是未完成 ...

  6. uni-app开发经验分享十二: Android平台应用启动时读写手机存储、访问设备信息(如IMEI)等权限策略及提示信息

    Android平台从6.0(API23)开始系统对权限的管理更加严格,所有涉及敏感权限都需要用户授权允许才能获取.因此一些应用基础业务逻辑需要的权限会在应用启动时申请,并引导用户允许. 读写手机存储权 ...

  7. python多线程和GIL全局解释器锁

    1.线程     线程被称为轻量级进程,是最小执行单元,系统调度的单位.线程切换需要的资源一般,效率一般.  2.多线程         在单个程序中同时运行多个线程完成不同的工作,称为多线程 3.并 ...

  8. Linux 通过端口终止进程

    以下命令可用于杀死占用某端口的所有进程. root 用户: kill -9 $(lsof -i tcp:进程号 -t) 非 root 用户: kill -9 $(sudo lsof -i tcp:进程 ...

  9. 《Effective C#》之减少装箱和拆箱

    <Effective C#>之减少装箱和拆箱_天极网 http://dev.yesky.com/msdn/359/3486359.shtml <Effective C#>之减少 ...

  10. 判断2个list中是否有相同的数据(相交)Collections.disjoint

    https://blog.csdn.net/yang_niuxxx/article/details/85092490 private void initData() { for (int i = 0; ...