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 题意:地图上某些点有金子,有些点有房子,还有一些带权路径,问 ...
随机推荐
- BZOJ5248:[九省联考2018]一双木棋——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5248 https://www.luogu.org/problemnew/show/P4363#su ...
- HDU4757:Tree——题解
http://acm.hdu.edu.cn/showproblem.php?pid=4757 给一棵有点值的树,每次询问u~v的最短路当中的一个点的点权异或z最大值. 前置技能:HDU4825 前置技 ...
- HDU3065:病毒侵袭持续中——题解
http://acm.hdu.edu.cn/showproblem.php?pid=3065 ———————————————————————————— Problem Description 小t非常 ...
- HDU.1285 确定比赛名次 (拓扑排序 TopSort)
HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...
- JavaScript身份证号码有效性验证
最近需要对身份证合法性进行验证,实名验证是不指望了,不过原来的验证规则太过简单,只是简单的验证了身份证长度,现在业务需要加强下身份证验证规则,网上找到了不少资料,不过都不合偶的心意,无奈只好直接写一个 ...
- Codeforces Round #344 (Div. 2) B
B. Print Check time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #514 (Div. 2):D. Nature Reserve(二分+数学)
D. Nature Reserve 题目链接:https://codeforces.com/contest/1059/problem/D 题意: 在二维坐标平面上给出n个数的点,现在要求一个圆,能够容 ...
- TP-LINK TL-WN725N V2 / rtl8188eu Linux驱动安装
https://github.com/lwfinger/rtl8188eu 驱动下载地址 安装: make all make install 参考一下把 http://devillived.net/f ...
- DOM基本代码一
dom学习基本代码第一部分 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...
- ES6中函数的扩展
一.设置默认参数 ES6之前,给函数设置默认参数是这样做的: function fn(a) { if(typeof y === undefined){ a = a || 'hello'; } cons ...