每个区间拆成r和l-1两个端点,若之内有偶数个1,则这两个端点对应的前缀的奇偶性必须相同,否则必须相反。

于是可以用带权并查集维护,每个结点储存其与其父节点的奇偶性是否相同,并且在路径压缩以及Union时进行分类讨论即可。

由于n太大,要对两个端点进行离散化。

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. int fa[10010];
  5. bool rel[10010];
  6. int findroot(int x){
  7. if(x==fa[x]){
  8. return x;
  9. }
  10. int Fa=findroot(fa[x]);
  11. if(rel[x]==0 && rel[fa[x]]==0){
  12. rel[x]=0;
  13. }
  14. else if(rel[x]==0 && rel[fa[x]]==1){
  15. rel[x]=1;
  16. }
  17. else if(rel[x]==1 && rel[fa[x]]==0){
  18. rel[x]=1;
  19. }
  20. else if(rel[x]==1 && rel[fa[x]]==1){
  21. rel[x]=0;
  22. }
  23. return fa[x]=Fa;
  24. }
  25. struct Data{
  26. int v,p;
  27. }t[10010];
  28. bool cmp(const Data &a,const Data &b){
  29. return a.v<b.v;
  30. }
  31. int n,m,a[10010];
  32. char op[5010][7];
  33. int main(){
  34. // freopen("vijos1112.in","r",stdin);
  35. int e=0;
  36. scanf("%d%d",&n,&m);
  37. for(int i=1;i<=m;++i){
  38. scanf("%d%d%s",&t[(i<<1)-1].v,&t[i<<1].v,op[i]);
  39. --t[(i<<1)-1].v;
  40. t[(i<<1)-1].p=(i<<1)-1;
  41. t[i<<1].p=(i<<1);
  42. }
  43. sort(t+1,t+(m<<1|1),cmp);
  44. a[t[1].p]=++e;
  45. for(int i=2;i<=(m<<1);++i){
  46. if(t[i].v!=t[i-1].v){
  47. ++e;
  48. }
  49. a[t[i].p]=e;
  50. }
  51. for(int i=1;i<=e;++i){
  52. fa[i]=i;
  53. }
  54. for(int i=1;i<=m;++i){
  55. int x=a[(i<<1)-1],y=a[i<<1];
  56. int f1=findroot(x),f2=findroot(y);
  57. if(f1==f2){
  58. if(op[i][0]=='o'){
  59. if(rel[x]==rel[y]){
  60. printf("%d\n",i-1);
  61. return 0;
  62. }
  63. }
  64. else{
  65. if(rel[x]!=rel[y]){
  66. printf("%d\n",i-1);
  67. return 0;
  68. }
  69. }
  70. }
  71. else{
  72. fa[f1]=f2;
  73. if(op[i][0]=='o'){
  74. if(rel[x]==0 && rel[y]==0){
  75. rel[f1]=1;
  76. }
  77. else if(rel[x]==0 && rel[y]==1){
  78. rel[f1]=0;
  79. }
  80. else if(rel[x]==1 && rel[y]==0){
  81. rel[f1]=0;
  82. }
  83. else if(rel[x]==1 && rel[y]==1){
  84. rel[f1]=1;
  85. }
  86. }
  87. else{
  88. if(rel[x]==0 && rel[y]==0){
  89. rel[f1]=0;
  90. }
  91. else if(rel[x]==0 && rel[y]==1){
  92. rel[f1]=1;
  93. }
  94. else if(rel[x]==1 && rel[y]==0){
  95. rel[f1]=1;
  96. }
  97. else if(rel[x]==1 && rel[y]==1){
  98. rel[f1]=0;
  99. }
  100. }
  101. }
  102. }
  103. printf("%d\n",m);
  104. return 0;
  105. }

【带权并查集】【离散化】vijos P1112 小胖的奇偶的更多相关文章

  1. poj 1733 Parity game(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 题目大意:有一个很长很长含有01的字符串,长度可达1000000000,首先告诉你字符串的长度n,再给一个m,表示给你m条信息, ...

  2. poj1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 题意:给定由0.1组成的数串长度n,询问次数m,每次询问给出a,b,s,表示区间[a,b]内1的数量为s(odd-奇数或even ...

  3. Parity game(带权并查集+离散化)

    题目链接  //kuangbin 题意: 现在你和你的朋友正在玩一种游戏. 你的朋友写下一串0和1的序列,然后你选择其中一串子序列(如[3,5])并且问他这个序列是包含奇数个1还是偶数个1(和是奇数还 ...

  4. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  5. POJ 1733 Parity game(离散化+带权并查集)

    离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...

  6. POJ 1733 Parity game 【带权并查集】+【离散化】

    <题目链接> 题目大意: 一个由0,1组成的序列,每次给出一段区间的奇偶,问哪一条信息不合法. 解题分析: 我们用s[i]表示前i个数的前缀和,那么a b even意味着s[b]和s[a- ...

  7. POJ1733:Parity Game(离散化+带权并查集)

    Parity Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12853   Accepted: 4957 题目链接 ...

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

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

  9. POJ 1773 Parity game 带权并查集

    分析:带权并查集,就是维护一堆关系 然后就是带权并查集的三步 1:首先确定权值数组,sum[i]代表父节点到子节点之间的1的个数(当然路径压缩后代表到根节点的个数) 1代表是奇数个,0代表偶数个 2: ...

随机推荐

  1. MS16-032提权正确方法

    原版MS16-032提权会Spawn一个System Shell出来,只能通过Remote Desktop获取.这里修改exploit,直接反弹Shell.注意MS16-032依赖 thread ha ...

  2. linux编程之多线程编程

    我们知道,进程在各自独立的地址空间中运行,进程之间共享数据需要用mmap或者进程间通信机制,有些情况需要在一个进程中同时执行多个控制流程,这时候线程就派上了用场,比如实现一个图形界面的下载软件,一方面 ...

  3. wait与waitpid

    1. 函数原型: #include <sys/wait.h> pid_t wait(int *statloc); pid_t waitpid(pid_t pid, int *statloc ...

  4. nfs 文件共享 服务

    需要rpc服务: [root@xujiaxuan ftp]# service rpcbind start[root@xujiaxuan ftp]# chkconfig rpcbind on 设置开机自 ...

  5. JDK1.8特性实现jdk动态代理,使业务解耦

    首先,先创建一个interface IHello 目标接口类 interface IHello { void sayHello(); } 然后再写一个目标类的实现类 class HelloImpl i ...

  6. Zookeeper 入门第一篇

    转载原文地址: ZooKeeper学习总结 第一篇:ZooKeeper快速入门 ZooKeeper学习总结 第二篇:ZooKeeper深入探讨 ZooKeeper学习第一期---Zookeeper简单 ...

  7. Guice2.0的变化——第一部分 新的特性(上)

    http://superleo.iteye.com/blog/314816 Private Modules PrivateModules 用于创建并不需要对外可见的绑定对象.当然,这样会使得封装变得更 ...

  8. 玩转RaspberryPi

    step1:烧制树莓派内存卡 可以用[Linux系统烧制]http://www.williamsang.com/archives/1764.html 如果用windows烧制的话,就用Win32 Di ...

  9. [转载] 更改pip源至国内镜像,显著提升下载速度

    原文地址: https://blog.csdn.net/lambert310/article/details/52412059 经常在使用python的时候需要安装各种模块,而pip是很强大的模块安装 ...

  10. NET牛人应该知道些什么?(瞬间觉得自己弱爆了)

    任何一个使用.NET的人 描述线程与进程的区别? 什么是Windows服务,它的生命周期与标准的EXE程序有什么不同 Windows上的单个进程所能访问的最大内存量是多少?它与系统的最大虚拟内存一样吗 ...