POJ1733:Parity Game(离散化+带权并查集)
Parity Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12853 | Accepted: 4957 |
题目链接:http://poj.org/problem?id=1733
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 either `even' or `odd' (the answer, i.e. the parity of the number of ones in the chosen subsequence, where `even' means an even number of ones and `odd' 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:
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd
Sample Output:
3
题意:
给出一个01串,然后给出一些信息,是关于[x,y]区间中有奇数个1还是偶数个1,求出X,使得1-X条信息都可以满足。
题解:
如果不考虑数据范围,很明显就是带权并查集,用一个数组v来维护当前结点与其父亲结点的关系:v[x]=1 <==>(x,f[x]]有奇数个1;否则v[x]=0就是偶数个个1。
再来看一下数据范围,区间很大, 如果直接开数组那么肯定存不下的;再来看下信息个数,只有5000个,也就是最多也只有10000个区间端点。
所以我们考虑离散化(区间端点的具体值我们并不关心,主要是相对大小),把原来范围很大的区间变小。
我用的是map。代码如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <map> using namespace std; typedef long long ll; const int N = 15005; int f[N],v[N]; map<ll,int> mp; ll n,x,y; int q,tot; int find(int x){ if(x==f[x]) return x; int tmp = f[x]; f[x]=find(f[x]); v[x]+=v[tmp]; if(v[x]>=2) v[x]-=2; return f[x]; } int main(){ cin>>n>>q; int flag = 0,ans = q; for(int i=0;i<=10001;i++) f[i]=i,v[i]=0; for(int i=1;i<=q;i++){ char s[10]; scanf("%lld%lld %s",&x,&y,s); if(flag) continue ; if(!mp[x]) mp[x]=++tot; if(!mp[y+1])mp[y+1]=++tot; int fx=find(mp[x]),fy=find(mp[y+1]),pd; if(s[0]=='e') pd=1;else pd=0; if(fx==fy){ int now = (v[mp[x]]+v[mp[y+1]])%2; if((pd && !now) || (!pd &&now)) continue; else{ ans=i-1; flag=1; continue; } } f[fx]=fy; if(pd) v[fx]=(v[mp[x]]+v[mp[y+1]])%2; else if(!pd) v[fx]=(v[mp[x]]+v[mp[y+1]]+1)%2; } printf("%d",ans); return 0; }
重要的是自信,一旦有了自信,人就会赢得一切。