题目链接:http://poj.org/problem?id=1733

题目大意:给你m条信息,每条信息告诉你区间l~r的1的个数是奇数还是偶数,如果后面出现信息跟前面矛盾则这条信息是错误的,问在第一条错误信息之前的正确信息数。

解题思路:对于l~r之间的1的个数的奇偶性,其实可以理解成r比l-1多的1的个数的奇偶,这样刚好可以跟前面一段区间连起来。分两个种类,奇数1,偶数0,同样满足0->1>0的关系像食物链一样。跟How Many Answers Are Wrong差不多。

代码:

 #include<cstdio>
#include<cmath>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#define LC(a) (a<<1)
#define RC(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int N=2e4+; int root[N],val[N]; struct node{
int l,r,cla;
}arr[N];
int X[N]; int find(int x){
if(root[x]==x)
return x;
int tmp=find(root[x]);
val[x]=(val[x]+val[root[x]])%;
return root[x]=tmp;
} int bin_search(int l,int r,int x){
while(l<=r){
int mid=(l+r)/;
if(X[mid]==x)
return mid;
else if(X[mid]<x)
l=mid+;
else
r=mid-;
}
return -;
} int main(){
int n,m;
while(~scanf("%d",&n)){
scanf("%d",&m);
int m1=,m2=,ans=;
memset(val,,sizeof(val));
for(int i=;i<=m;i++){
char s[];
scanf("%d%d%s",&arr[i].l,&arr[i].r,s);
if(s[]=='e')
arr[i].cla=;
else
arr[i].cla=;
X[++m1]=arr[i].l;
X[++m1]=arr[i].r;
}
//去重
sort(X+,X++m1);
for(int i=;i<=m1;i++){
if(X[i]!=X[i-])
X[++m2]=X[i];
}
for(int i=;i<=m2;i++){
root[i]=i;
}
for(int i=;i<=m;i++){
int l=bin_search(,m2,arr[i].l)-;
int r=bin_search(,m2,arr[i].r);
int c=arr[i].cla;
int t1=find(l);
int t2=find(r);
if(t1==t2){
if((val[l]+c)%!=val[r]){
ans=i-;
break;
}
}
else{
root[t2]=t1;
val[t2]=(val[l]-val[r]+c+)%;
}
}
if(!ans)
ans=m;
printf("%d\n",ans);
}
return ;
}

POJ 1733 Parity game(带权并查集)的更多相关文章

  1. POJ 1733 Parity game (带权并查集)

    题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...

  2. POJ 1773 Parity game 带权并查集

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

  3. URAL - 1003:Parity (带权并查集&2-sat)

    Now and then you play the following game with your friend. Your friend writes down a sequence consis ...

  4. POJ 1182 食物链 【带权并查集】

    <题目链接> 题目大意: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我 ...

  5. POJ 1182 食物链 (带权并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78551   Accepted: 23406 Description ...

  6. POJ 1182 食物链 【带权并查集/补集法】

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...

  7. poj 1182 食物链【带权并查集】

    设相等的边权为0,吃的边权为,被吃的边权为2,然后用带权并查集在%3的意义下做加法即可 关系为简单环的基本都可以用模环长的方式是用带权并查集 #include<iostream> #inc ...

  8. A Bug's Life POJ - 2492 (种类或带权并查集)

    这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...

  9. 【poj1733】Parity game--边带权并查集

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15776   Accepted: 5964 Description Now ...

  10. POJ 3228 Gold Transportation(带权并查集,好题)

    参考链接:http://www.cnblogs.com/jiaohuang/archive/2010/11/13/1876418.html 题意:地图上某些点有金子,有些点有房子,还有一些带权路径,问 ...

随机推荐

  1. BZOJ2002:[HNOI2010]弹飞绵羊——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2002 https://www.luogu.org/problemnew/show/P3203 某天, ...

  2. oracle语法

    执行计划: 1.1 设置autotrace 序号 命令 解释 1 SET AUTOTRACE OFF 此为默认值,即关闭Autotrace 2 SET AUTOTRACE ON EXPLAIN 只显示 ...

  3. 使用canvas画一个雷达效果图的特效代码

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. Uva-oj Palindromes 暴力

     Palindromes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Statu ...

  5. 三大linux系统对比

    概述: centos作为服务器部署是第一选择.CentOS去除很多与服务器功能无关的应用,系统简单但非常稳定,命令行操作可以方便管理系统和应用,丰富的帮助文档和社区的支持. ubuntu最佳的应用领域 ...

  6. Caused by: java.io.IOException: Filesystem closed的处理

    org.apache.hadoop.hive.ql.metadata.HiveException: Unable to rename output from: hdfs://nameservice/u ...

  7. 解决oracle数据库 ora-00054:resource busy and acquire with NOWAIT specified 错误

    解决oracle数据库 ora-00054:resource busy and acquire with NOWAIT specified 错误 本人在使用pl/sql developer 客户端调用 ...

  8. linux查找进程id端口占用和杀死进程

    linux 查找进程id端口占用和杀死进程 ps 命令用于查看当前正在运行的进程 辅助上grep 用于搜索匹配ps -ef | grep java ps ax : 显示当前系统进程的列表 ps aux ...

  9. BZOJ2733:使用并查集维护连通性之后用线段树维护+线段树合并(动态开点)

    可以说是线段树合并的裸题吧 题意就是给你两个操作 一个操作是合并两个集合,这两个集合都是用权值线段树维护的,便于查询第k小元素 另一个操作就是查询区间极值了 #include<cstdio> ...

  10. 【C++ STL】Queue

    1.定义 class queue<>实作为一个queue(也成为FIFO,先进先出).可以使用push()将任意数量的元素置入queue中,也可以使用pop()将元素以其插入顺序从容器中移 ...