【tyvj1252/poj1733】小胖的奇偶/Parity game 并查集
2015-10-13 20:27
323 查看
Description
Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers.You suspect some of your friend’s answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.
Input
The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1000000000. In the second line, there is one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is eithereven' orodd’ (the answer, i.e. the parity of the number of ones in the chosen subsequence, where
even' means an even number of ones andodd’ means an odd number).
Output
There is only one line in output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X+1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.Sample Input
105
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
Sample Output
3扩展域并查集。
若[a,b]中出现了偶数个1,则表示[0,a-1]和[0,b]的1的奇偶性相同。
若[a,b]中出现了奇数个1,则表示[0,a-1]和[0,b]的1的奇偶性不同。
这样就可以用并查集来维护:
fa[i]代表[0,i]有偶数个1,fa[i+len]代表[0,i]有奇数个1。
若[a,b]有偶数个1,则合并fa[a-1]和fa[b],fa[a-1+len]和fa[b+len]。
若[a,b]有奇数个1,则合并fa[a-1]和fa[b+len],fa[a-1]和fa[b+len]。
对于每次操作,提前查询一下另一种回答所对应的集合是否是同一个,来判断是否矛盾。
这个题需要离散化,因为查询[a,b]时要使用的是a-1,不能离散化后再减1,因为这时候减的1实际可能代表的并不是一个单位,有可能是一个区间,因为你离散掉了。要做的就是在读入的时候就
a--,这样就可以了。因为这个我WA了好几遍……
代码:
[code]#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int size=1000010; int fa[size]; int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); } int lsh[size]; struct input{ int l,r,d; }l[size]; int len,n; int solve() { for(int i=1;i<=n;i++) { int x=find(l[i].l); int x1=find(l[i].l+len); int y=find(l[i].r); int y1=find(l[i].r+len); if(l[i].d==0) { if(x==y1) return i-1; fa[x]=y; fa[x1]=y1; } else { if(x==y) return i-1; fa[x]=y1; fa[x1]=y; } } return n; } int main() { scanf("%d%d",&len,&n); for(int i=1;i<=n;i++) { scanf("%d%d",&l[i].l,&l[i].r); l[i].l--; string s; cin>>s; if(s=="even") l[i].d=0; if(s=="odd") l[i].d=1; lsh[++lsh[0]]=l[i].l; lsh[++lsh[0]]=l[i].r; } sort(lsh+1,lsh+1+lsh[0]); len=unique(lsh+1,lsh+1+lsh[0])-lsh-1; for(int i=0;i<=len*3;i++) fa[i]=i; for(int i=1;i<=n;i++) { l[i].l=lower_bound(lsh+1,lsh+1+len,l[i].l)-lsh+1; l[i].r=lower_bound(lsh+1,lsh+1+len,l[i].r)-lsh+1; } printf("%d",solve()); return 0; }
相关文章推荐
- Oracle性能优化
- Service(一):认识service、绑定Service
- CF #325 div1 C + D + hdu 5458
- 在Anaconda中安装python包seaborn
- 【贪心】Bzoj 2457:[BeiJing2011]双端队列
- 严蔚敏数据结构习题3.32
- Android M版本和非M版本动态权限适配方案
- JAVA上机——5.26
- SVN-团队开发常见问题
- 通知和单例
- Ant编译问题四:****使用或覆盖已过时的API
- iOS序列化与反序列化
- iOS之OC随笔-属性修饰符
- lwip【5】 lwIP配置文件opt.h和lwipopts.h初步分析之二
- lwip【4】 lwIP配置文件opt.h和lwipopts.h初步分析之一
- 使用MPMoviePlayerControll播放视频
- iOS 常用名词
- vim tab设置为4个空格
- iOS第三方库
- Android 4.2 Wifi Display 之 Settings 源码分析(一)