Parity game POJ - 1733

Parity game

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.
 
题目:给定若干个区间,在给定这个区间'1'个数的奇偶性,问第几个与前面的答案冲突,假设第X个冲突,则输出X-1,如果都没冲突,则输出询问的个数。
思路:我们可以用带权并查集解决,因为范围1e9,于是我们需要离散化一下。权值分别为:区间为偶数0,区间为奇数1,细节问题,(x,y)区间则需要查询(x-1,y)。这样我们可以把并查集初始权值为0,因为自身无意义,需要与其他相关联才有区间的意义。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <vector>
 6 #include <cmath>
 7 
 8 using namespace std;
 9 
10 #define ll long long
11 #define pb push_back
12 #define fi first
13 #define se second
14 
15 
16 const int N = 5010 << 1;
17 struct node
18 {
19     int rt, v;
20 }fa[N];
21 struct info
22 {
23     int x, y, d;
24 };
25 vector<info > a;
26 vector<int > id;
27 int n, m;
28 
29 int Find(int x)
30 {
31     if(fa[x].rt == x) return x;
32     else{
33         int tmp = fa[x].rt;
34         fa[x].rt = Find(tmp);
35         fa[x].v = (fa[x].v + fa[tmp].v) % 2;
36         return fa[x].rt;
37     }
38 }
39 
40 bool Union(int x, int y, int d)
41 {
42     int fax = Find(x);
43     int fay = Find(y);
44     if(fax != fay){
45         fa[fay].rt = fax;
46         fa[fay].v = (fa[x].v + d - fa[y].v + 2) % 2;
47         return true;
48     }else{
49         if(0 == (fa[x].v + d - fa[y].v + 2) % 2) return true;
50         else return false;
51     }
52 }
53 
54 void solve()
55 {   
56     scanf("%d%d", &n, &m);
57     int x, y;
58     char op[10];
59     for(int i = 1; i <= m; ++i){
60         scanf("%d%d%s", &x, &y, op);
61         id.pb(x);
62         id.pb(y);
63         a.pb({x, y, op[0] == 'e' ? 0 : 1});
64     }
65     sort(id.begin(), id.end());
66     id.erase(unique(id.begin(), id.end()), id.end());
67     int n = id.size();
68 
69     for(int i = 0; i <= n; ++i){
70         fa[i].rt = i;
71         fa[i].v = 0;
72     }
73 
74     int inx = -1;
75     for(int i = 0; i < m; ++i){
76         int x = lower_bound(id.begin(), id.end(), a[i].x) - id.begin() + 1;
77         int y = lower_bound(id.begin(), id.end(), a[i].y) - id.begin() + 1;
78         if(!Union(x - 1, y, a[i].d)){
79             inx = i;
80             break;
81         } 
82     }
83     
84     //printf("ans = %d\n", inx == -1 ? m : inx);
85     printf("%d\n", inx == -1 ? m : inx);
86 }
87 
88 int main()
89 {
90 
91     solve();
92 
93     return 0;
94 }

 

posted @ 2020-07-10 00:26  SummerMingQAQ  阅读(199)  评论(0编辑  收藏  举报