POJ 1733 Parity game(带权并查集)
题目链接: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(带权并查集)的更多相关文章
- POJ 1733 Parity game (带权并查集)
题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...
- POJ 1773 Parity game 带权并查集
分析:带权并查集,就是维护一堆关系 然后就是带权并查集的三步 1:首先确定权值数组,sum[i]代表父节点到子节点之间的1的个数(当然路径压缩后代表到根节点的个数) 1代表是奇数个,0代表偶数个 2: ...
- URAL - 1003:Parity (带权并查集&2-sat)
Now and then you play the following game with your friend. Your friend writes down a sequence consis ...
- POJ 1182 食物链 【带权并查集】
<题目链接> 题目大意: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我 ...
- POJ 1182 食物链 (带权并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 78551 Accepted: 23406 Description ...
- POJ 1182 食物链 【带权并查集/补集法】
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...
- poj 1182 食物链【带权并查集】
设相等的边权为0,吃的边权为,被吃的边权为2,然后用带权并查集在%3的意义下做加法即可 关系为简单环的基本都可以用模环长的方式是用带权并查集 #include<iostream> #inc ...
- A Bug's Life POJ - 2492 (种类或带权并查集)
这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...
- 【poj1733】Parity game--边带权并查集
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15776 Accepted: 5964 Description Now ...
- POJ 3228 Gold Transportation(带权并查集,好题)
参考链接:http://www.cnblogs.com/jiaohuang/archive/2010/11/13/1876418.html 题意:地图上某些点有金子,有些点有房子,还有一些带权路径,问 ...
随机推荐
- BZOJ4872:[SHOI2017]分手是祝愿——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=4872 https://www.luogu.org/problemnew/show/P3750 Zei ...
- BZOJ1596 [Usaco2008 Jan]电话网络 【树形dp】
题目链接 BZOJ1596 题解 先抽成有根树 设\(f[i][0|1][0|1]\)表示以\(i\)为根,儿子都覆盖了,父亲是否覆盖,父亲是否建塔的最少建塔数 转移一下即可 #include< ...
- POJ. 2253 Frogger (Dijkstra )
POJ. 2253 Frogger (Dijkstra ) 题意分析 首先给出n个点的坐标,其中第一个点的坐标为青蛙1的坐标,第二个点的坐标为青蛙2的坐标.给出的n个点,两两双向互通,求出由1到2可行 ...
- UVA.10474 Where is the Marble ( 排序 二分查找 )
UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...
- 双向数据绑定实现之Object.defineProperty
vue.js利用的是es5的 defineproperty 特性实现的双向数据绑定,了解一下基本原理. 举例 var person= {}; Object.defineProperty(person, ...
- String StrigBuffer StringBuilder 浅解
1.String是最基本的字符串类,用于表示字符串. 特点:对象内容不可变,但可以通过指向不同的对象来“表示”不同的内容. 使用场景:如果不涉及到内容改变,可以使用String. 注意:如果想将Str ...
- vs 统计有效代码行数
1.Visual Studio中,crtl+Shift+F,输入b*[^:b#/]+.*$ ,查找范围:选择整个解决方案,查找选项:使用正则表达式,文件类型:*.cs;*.cshtml 选择查找全部
- aio 爬虫,去重,入库
#aio 爬虫,去重,入库 import asyncio import aiohttp import aiomysql import re from pyquery import PyQuery st ...
- outer的使用
outer就是一个标签,java语言中根本没有此关键字,因此outer也可以用其它的词来代替 java中的标签就是一个紧跟着英文冒号(:)的标识符.与其他语言不同的是,java中的标签只有放在循环语言 ...
- IntentServicce;Looper;long-running task
7. If you want to carry on a long-running task, what do you need to do? IntentService:Service Servic ...